<commit_before><?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Loader
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * @namespace
 */
namespace Zend\Loader;

use ArrayIterator,
    Traversable;

/**
 * Plugin class locater interface
 *
 * @category   Zend
 * @package    Zend_Loader
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class PluginClassLoader implements PluginClassLocater
{
    /**
     * List of plugin name => class name pairs
     * @var array
     */
    protected $plugins = array();

    /**
     * Constructor
     * 
     * @param  null|array|Traversable $map If provided, seeds the loader with a map
     * @return void
     */
    public function __construct($map = null)
    {
        if ($map !== null) {
            $this->registerPlugins($map);
        }
    }

    /**
     * Register a class to a given short name
     * 
     * @param  string $shortName 
     * @param  string $className 
     * @return PluginClassLoader
     */
    public function registerPlugin($shortName, $className)
    {
        $this->plugins[strtolower($shortName)] = $className;
        return $this;
    }

    /**
     * Register many plugins at once
     *
     * If $map is a string, assumes that the map is the class name of a 
     * Traversable object (likely a ShortNameLocater); it will then instantiate
     * this class and use it to register plugins.
     *
     * If $map is an array or Traversable object, it will iterate it to 
     * register plugin names/classes.
     *
     * For all other arguments, or if the string $map is not a class or not a 
     * Traversable class, an exception will be raised.
     * 
     * @param  string|array|Traversable $map 
     * @return PluginClassLoader
     * @throws InvalidArgumentException
     */
    public function registerPlugins($map)
    {
        if (is_string($map)) {
            if (!class_exists($map)) {
                throw new InvalidArgumentException('Map class provided is invalid');
            }
            $map = new $map;
        }
        if (is_array($map)) {
            $map = new ArrayIterator($map);
        }
        if (!$map instanceof Traversable) {
            throw new InvalidArgumentException('Map provided is invalid; must be traversable');
        }

        // iterator_apply is faster than foreach
        $loader = $this;
        iterator_apply($map, function() use ($loader, $map) {
            $plugin = $map->key();
            $class  = $map->current();
            $loader->registerPlugin($plugin, $class);
        });

        return $this;
    }

    /**
     * Unregister a short name lookup
     * 
     * @param mixed $shortName 
     * @return PluginClassLoader
     */
    public function unregisterPlugin($shortName)
    {
        $lookup = strtolower($shortName);
        if (array_key_exists($lookup, $this->plugins)) {
            unset($this->plugins[$lookup]);<commit_msg>Minor cleanup for readability<commit_after><?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Loader
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * @namespace
 */
namespace Zend\Loader;

use ArrayIterator,
    Traversable;

/**
 * Plugin class locater interface
 *
 * @category   Zend
 * @package    Zend_Loader
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class PluginClassLoader implements PluginClassLocater
{
    /**
     * List of plugin name => class name pairs
     * @var array
     */
    protected $plugins = array();

    /**
     * Constructor
     * 
     * @param  null|array|Traversable $map If provided, seeds the loader with a map
     * @return void
     */
    public function __construct($map = null)
    {
        if ($map !== null) {
            $this->registerPlugins($map);
        }
    }

    /**
     * Register a class to a given short name
     * 
     * @param  string $shortName 
     * @param  string $className 
     * @return PluginClassLoader
     */
    public function registerPlugin($shortName, $className)
    {
        $this->plugins[strtolower($shortName)] = $className;
        return $this;
    }

    /**
     * Register many plugins at once
     *
     * If $map is a string, assumes that the map is the class name of a 
     * Traversable object (likely a ShortNameLocater); it will then instantiate
     * this class and use it to register plugins.
     *
     * If $map is an array or Traversable object, it will iterate it to 
     * register plugin names/classes.
     *
     * For all other arguments, or if the string $map is not a class or not a 
     * Traversable class, an exception will be raised.
     * 
     * @param  string|array|Traversable $map 
     * @return PluginClassLoader
     * @throws InvalidArgumentException
     */
    public function registerPlugins($map)
    {
        if (is_string($map)) {
            if (!class_exists($map)) {
                throw new InvalidArgumentException('Map class provided is invalid');
            }
            $map = new $map;
        }
        if (is_array($map)) {
            $map = new ArrayIterator($map);
        }
        if (!$map instanceof Traversable) {
            throw new InvalidArgumentException('Map provided is invalid; must be traversable');
        }

        // iterator_apply is faster than foreach
        $loader = $this;
        iterator_apply($map, function() use ($loader, $map) {
            $loader->registerPlugin($map->key(), $map->current());
        });

        return $this;
    }

    /**
     * Unregister a short name lookup
     * 
     * @param mixed $shortName 
     * @return PluginClassLoader
     */
    public function unregisterPlugin($shortName)
    {
        $lookup = strtolower($shortName);
        if (array_key_exists($lookup, $this->plugins)) {
            unset($this->plugins[$lookup]);
{'fraction_non_alphanumeric': 0.10642150717370107, 'fraction_numerical': 0.004457445326647165, 'mean_word_length': 2.2830361225422955, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1328496', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1982, 'n_words': 668}