Packages

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

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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