Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • ArrayTools
  • Callback
  • Component
  • ComponentContainer
  • Configurator
  • DateTime
  • Debug
  • Environment
  • Framework
  • FreezableObject
  • GenericRecursiveIterator
  • Image
  • ImageMagick
  • InstanceFilterIterator
  • Object
  • ObjectMixin
  • Paginator
  • RecursiveComponentIterator
  • ServiceLocator
  • SmartCachingIterator
  • String
  • Tools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • AmbiguousServiceException
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * ComponentContainer is default implementation of IComponentContainer.
 20:  *
 21:  * @author     David Grudl
 22:  *
 23:  * @property-read \ArrayIterator $components
 24:  */
 25: class ComponentContainer extends Component implements IComponentContainer
 26: {
 27:     /** @var array of IComponent */
 28:     private $components = array();
 29: 
 30:     /** @var IComponent|NULL */
 31:     private $cloning;
 32: 
 33: 
 34: 
 35:     /********************* interface IComponentContainer ****************d*g**/
 36: 
 37: 
 38: 
 39:     /**
 40:      * Adds the specified component to the IComponentContainer.
 41:      * @param  IComponent
 42:      * @param  string
 43:      * @param  string
 44:      * @return void
 45:      * @throws \InvalidStateException
 46:      */
 47:     public function addComponent(IComponent $component, $name, $insertBefore = NULL)
 48:     {
 49:         if ($name === NULL) {
 50:             $name = $component->getName();
 51:         }
 52: 
 53:         if (is_int($name)) {
 54:             $name = (string) $name;
 55: 
 56:         } elseif (!is_string($name)) {
 57:             throw new \InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
 58: 
 59:         } elseif (!preg_match('#^[a-zA-Z0-9_]+$#', $name)) {
 60:             throw new \InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
 61:         }
 62: 
 63:         if (isset($this->components[$name])) {
 64:             throw new \InvalidStateException("Component with name '$name' already exists.");
 65:         }
 66: 
 67:         // check circular reference
 68:         $obj = $this;
 69:         do {
 70:             if ($obj === $component) {
 71:                 throw new \InvalidStateException("Circular reference detected while adding component '$name'.");
 72:             }
 73:             $obj = $obj->getParent();
 74:         } while ($obj !== NULL);
 75: 
 76:         // user checking
 77:         $this->validateChildComponent($component);
 78: 
 79:         try {
 80:             if (isset($this->components[$insertBefore])) {
 81:                 $tmp = array();
 82:                 foreach ($this->components as $k => $v) {
 83:                     if ($k === $insertBefore) $tmp[$name] = $component;
 84:                     $tmp[$k] = $v;
 85:                 }
 86:                 $this->components = $tmp;
 87:             } else {
 88:                 $this->components[$name] = $component;
 89:             }
 90:             $component->setParent($this, $name);
 91: 
 92:         } catch (\Exception $e) {
 93:             unset($this->components[$name]); // undo
 94:             throw $e;
 95:         }
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Removes a component from the IComponentContainer.
102:      * @param  IComponent
103:      * @return void
104:      */
105:     public function removeComponent(IComponent $component)
106:     {
107:         $name = $component->getName();
108:         if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
109:             throw new \InvalidArgumentException("Component named '$name' is not located in this container.");
110:         }
111: 
112:         unset($this->components[$name]);
113:         $component->setParent(NULL);
114:     }
115: 
116: 
117: 
118:     /**
119:      * Returns component specified by name or path.
120:      * @param  string
121:      * @param  bool   throw exception if component doesn't exist?
122:      * @return IComponent|NULL
123:      */
124:     final public function getComponent($name, $need = TRUE)
125:     {
126:         if (is_int($name)) {
127:             $name = (string) $name;
128: 
129:         } elseif (!is_string($name)) {
130:             throw new \InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
131: 
132:         } else {
133:             $a = strpos($name, self::NAME_SEPARATOR);
134:             if ($a !== FALSE) {
135:                 $ext = (string) substr($name, $a + 1);
136:                 $name = substr($name, 0, $a);
137:             }
138: 
139:             if ($name === '') {
140:                 throw new \InvalidArgumentException("Component or subcomponent name must not be empty string.");
141:             }
142:         }
143: 
144:         if (!isset($this->components[$name])) {
145:             $component = $this->createComponent($name);
146:             if ($component instanceof IComponent && $component->getParent() === NULL) {
147:                 $this->addComponent($component, $name);
148:             }
149:         }
150: 
151:         if (isset($this->components[$name])) {
152:             if (!isset($ext)) {
153:                 return $this->components[$name];
154: 
155:             } elseif ($this->components[$name] instanceof IComponentContainer) {
156:                 return $this->components[$name]->getComponent($ext, $need);
157: 
158:             } elseif ($need) {
159:                 throw new \InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
160:             }
161: 
162:         } elseif ($need) {
163:             throw new \InvalidArgumentException("Component with name '$name' does not exist.");
164:         }
165:     }
166: 
167: 
168: 
169:     /**
170:      * Component factory. Delegates the creation of components to a createComponent<Name> method.
171:      * @param  string      component name
172:      * @return IComponent  the created component (optionally)
173:      */
174:     protected function createComponent($name)
175:     {
176:         $ucname = ucfirst($name);
177:         $method = 'createComponent' . $ucname;
178:         if ($ucname !== $name && method_exists($this, $method) && $this->getReflection()->getMethod($method)->getName() === $method) {
179:             return $this->$method($name);
180:         }
181:     }
182: 
183: 
184: 
185:     /**
186:      * Iterates over a components.
187:      * @param  bool    recursive?
188:      * @param  string  class types filter
189:      * @return \ArrayIterator
190:      */
191:     final public function getComponents($deep = FALSE, $filterType = NULL)
192:     {
193:         $iterator = new RecursiveComponentIterator($this->components);
194:         if ($deep) {
195:             $deep = $deep > 0 ? \RecursiveIteratorIterator::SELF_FIRST : \RecursiveIteratorIterator::CHILD_FIRST;
196:             $iterator = new \RecursiveIteratorIterator($iterator, $deep);
197:         }
198:         if ($filterType) {
199:             $iterator = new InstanceFilterIterator($iterator, $filterType);
200:         }
201:         return $iterator;
202:     }
203: 
204: 
205: 
206:     /**
207:      * Descendant can override this method to disallow insert a child by throwing an \InvalidStateException.
208:      * @param  IComponent
209:      * @return void
210:      * @throws \InvalidStateException
211:      */
212:     protected function validateChildComponent(IComponent $child)
213:     {
214:     }
215: 
216: 
217: 
218:     /********************* cloneable, serializable ****************d*g**/
219: 
220: 
221: 
222:     /**
223:      * Object cloning.
224:      */
225:     public function __clone()
226:     {
227:         if ($this->components) {
228:             $oldMyself = reset($this->components)->getParent();
229:             $oldMyself->cloning = $this;
230:             foreach ($this->components as $name => $component) {
231:                 $this->components[$name] = clone $component;
232:             }
233:             $oldMyself->cloning = NULL;
234:         }
235:         parent::__clone();
236:     }
237: 
238: 
239: 
240:     /**
241:      * Is container cloning now?
242:      * @return NULL|IComponent
243:      * @internal
244:      */
245:     public function _isCloning()
246:     {
247:         return $this->cloning;
248:     }
249: 
250: }
251: 
252: 
253: 
254: /**
255:  * Recursive component iterator. See ComponentContainer::getComponents().
256:  *
257:  * @author     David Grudl
258:  */
259: class RecursiveComponentIterator extends \RecursiveArrayIterator implements \Countable
260: {
261: 
262:     /**
263:      * Has the current element has children?
264:      * @return bool
265:      */
266:     public function hasChildren()
267:     {
268:         return $this->current() instanceof IComponentContainer;
269:     }
270: 
271: 
272: 
273:     /**
274:      * The sub-iterator for the current element.
275:      * @return \RecursiveIterator
276:      */
277:     public function getChildren()
278:     {
279:         return $this->current()->getComponents();
280:     }
281: 
282: 
283: 
284:     /**
285:      * Returns the count of elements.
286:      * @return int
287:      */
288:     public function count()
289:     {
290:         return iterator_count($this);
291:     }
292: 
293: }
294: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0