[ Index ] |
PHP Cross Reference of phpBB-3.3.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Code\Generic\Prototype; 11 12 use Zend\Code\Reflection\Exception; 13 14 use function str_replace; 15 16 /** 17 * This is a factory for classes which are identified by name. 18 * 19 * All classes that this factory can supply need to 20 * be registered before (prototypes). This prototypes need to implement 21 * an interface which ensures every prototype has a name. 22 * 23 * If the factory can not supply the class someone is asking for 24 * it tries to fallback on a generic default prototype, which would 25 * have need to be set before. 26 */ 27 class PrototypeClassFactory 28 { 29 /** 30 * @var array 31 */ 32 protected $prototypes = []; 33 34 /** 35 * @var PrototypeGenericInterface 36 */ 37 protected $genericPrototype; 38 39 /** 40 * @param PrototypeInterface[] $prototypes 41 * @param PrototypeGenericInterface $genericPrototype 42 */ 43 public function __construct($prototypes = [], PrototypeGenericInterface $genericPrototype = null) 44 { 45 foreach ((array) $prototypes as $prototype) { 46 $this->addPrototype($prototype); 47 } 48 49 if ($genericPrototype) { 50 $this->setGenericPrototype($genericPrototype); 51 } 52 } 53 54 /** 55 * @param PrototypeInterface $prototype 56 * @throws Exception\InvalidArgumentException 57 */ 58 public function addPrototype(PrototypeInterface $prototype) 59 { 60 $prototypeName = $this->normalizeName($prototype->getName()); 61 62 if (isset($this->prototypes[$prototypeName])) { 63 throw new Exception\InvalidArgumentException('A prototype with this name already exists in this manager'); 64 } 65 66 $this->prototypes[$prototypeName] = $prototype; 67 } 68 69 /** 70 * @param PrototypeGenericInterface $prototype 71 * @throws Exception\InvalidArgumentException 72 */ 73 public function setGenericPrototype(PrototypeGenericInterface $prototype) 74 { 75 if (isset($this->genericPrototype)) { 76 throw new Exception\InvalidArgumentException('A default prototype is already set'); 77 } 78 79 $this->genericPrototype = $prototype; 80 } 81 82 /** 83 * @param string $name 84 * @return string 85 */ 86 protected function normalizeName($name) 87 { 88 return str_replace(['-', '_'], '', $name); 89 } 90 91 /** 92 * @param string $name 93 * @return bool 94 */ 95 public function hasPrototype($name) 96 { 97 $name = $this->normalizeName($name); 98 return isset($this->prototypes[$name]); 99 } 100 101 /** 102 * @param string $prototypeName 103 * @return PrototypeInterface 104 * @throws Exception\RuntimeException 105 */ 106 public function getClonedPrototype($prototypeName) 107 { 108 $prototypeName = $this->normalizeName($prototypeName); 109 110 if (! $this->hasPrototype($prototypeName) && ! isset($this->genericPrototype)) { 111 throw new Exception\RuntimeException('This tag name is not supported by this tag manager'); 112 } 113 114 if (! $this->hasPrototype($prototypeName)) { 115 $newPrototype = clone $this->genericPrototype; 116 $newPrototype->setName($prototypeName); 117 } else { 118 $newPrototype = clone $this->prototypes[$prototypeName]; 119 } 120 121 return $newPrototype; 122 } 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jun 23 12:25:44 2024 | Cross-referenced by PHPXref 0.7.1 |