HEX
Server: nginx/1.18.0
System: Linux proba.drlaca.appboxes.co 6.1.0-28-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.119-1 (2024-11-22) x86_64
User: appbox (1000)
PHP: 7.4.3-4ubuntu2.29
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //usr/share/php/Zend/Code/Generic/Prototype/PrototypeClassFactory.php
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Code\Generic\Prototype;

use Zend\Code\Reflection\Exception;

use function str_replace;

/**
 * This is a factory for classes which are identified by name.
 *
 * All classes that this factory can supply need to
 * be registered before (prototypes). This prototypes need to implement
 * an interface which ensures every prototype has a name.
 *
 * If the factory can not supply the class someone is asking for
 * it tries to fallback on a generic default prototype, which would
 * have need to be set before.
 */
class PrototypeClassFactory
{
    /**
     * @var array
     */
    protected $prototypes = [];

    /**
     * @var PrototypeGenericInterface
     */
    protected $genericPrototype;

    /**
     * @param PrototypeInterface[] $prototypes
     * @param PrototypeGenericInterface $genericPrototype
     */
    public function __construct($prototypes = [], PrototypeGenericInterface $genericPrototype = null)
    {
        foreach ((array) $prototypes as $prototype) {
            $this->addPrototype($prototype);
        }

        if ($genericPrototype) {
            $this->setGenericPrototype($genericPrototype);
        }
    }

    /**
     * @param PrototypeInterface $prototype
     * @throws Exception\InvalidArgumentException
     */
    public function addPrototype(PrototypeInterface $prototype)
    {
        $prototypeName = $this->normalizeName($prototype->getName());

        if (isset($this->prototypes[$prototypeName])) {
            throw new Exception\InvalidArgumentException('A prototype with this name already exists in this manager');
        }

        $this->prototypes[$prototypeName] = $prototype;
    }

    /**
     * @param PrototypeGenericInterface $prototype
     * @throws Exception\InvalidArgumentException
     */
    public function setGenericPrototype(PrototypeGenericInterface $prototype)
    {
        if (isset($this->genericPrototype)) {
            throw new Exception\InvalidArgumentException('A default prototype is already set');
        }

        $this->genericPrototype = $prototype;
    }

    /**
     * @param string $name
     * @return string
     */
    protected function normalizeName($name)
    {
        return str_replace(['-', '_'], '', $name);
    }

    /**
     * @param string $name
     * @return bool
     */
    public function hasPrototype($name)
    {
        $name = $this->normalizeName($name);
        return isset($this->prototypes[$name]);
    }

    /**
     * @param  string $prototypeName
     * @return PrototypeInterface
     * @throws Exception\RuntimeException
     */
    public function getClonedPrototype($prototypeName)
    {
        $prototypeName = $this->normalizeName($prototypeName);

        if (! $this->hasPrototype($prototypeName) && ! isset($this->genericPrototype)) {
            throw new Exception\RuntimeException('This tag name is not supported by this tag manager');
        }

        if (! $this->hasPrototype($prototypeName)) {
            $newPrototype = clone $this->genericPrototype;
            $newPrototype->setName($prototypeName);
        } else {
            $newPrototype = clone $this->prototypes[$prototypeName];
        }

        return $newPrototype;
    }
}