Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Component
  • ComponentContainer

Interfaces

  • IComponent
  • IComponentContainer
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://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\ComponentModel
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * ComponentContainer is default implementation of IContainer.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read ArrayIterator $components
 21:  * @package Nette\ComponentModel
 22:  */
 23: class ComponentContainer extends Component implements IComponentContainer
 24: {
 25:     /** @var IComponent[] */
 26:     private $components = array();
 27: 
 28:     /** @var IComponent|NULL */
 29:     private $cloning;
 30: 
 31: 
 32: 
 33:     /********************* interface IContainer ****************d*g**/
 34: 
 35: 
 36: 
 37:     /**
 38:      * Adds the specified component to the IContainer.
 39:      * @param  IComponent
 40:      * @param  string
 41:      * @param  string
 42:      * @return ComponentContainer  provides a fluent interface
 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_]+\z#', $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) {
 82:                         $tmp[$name] = $component;
 83:                     }
 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:         return $this;
 97:     }
 98: 
 99: 
100: 
101:     /**
102:      * Removes a component from the IContainer.
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:             $component = $this->$method($name);
180:             if (!$component instanceof IComponent && !isset($this->components[$name])) {
181:                 $class = get_class($this);
182:                 throw new UnexpectedValueException("Method $class::$method() did not return or create the desired component.");
183:             }
184:             return $component;
185:         }
186:     }
187: 
188: 
189: 
190:     /**
191:      * Iterates over a components.
192:      * @param  bool    recursive?
193:      * @param  string  class types filter
194:      * @return ArrayIterator
195:      */
196:     final public function getComponents($deep = FALSE, $filterType = NULL)
197:     {
198:         $iterator = new RecursiveComponentIterator($this->components);
199:         if ($deep) {
200:             $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
201:             $iterator = new RecursiveIteratorIterator($iterator, $deep);
202:         }
203:         if ($filterType) {
204:             $iterator = new InstanceFilterIterator($iterator, $filterType);
205:         }
206:         return $iterator;
207:     }
208: 
209: 
210: 
211:     /**
212:      * Descendant can override this method to disallow insert a child by throwing an InvalidStateException.
213:      * @return void
214:      * @throws InvalidStateException
215:      */
216:     protected function validateChildComponent(IComponent $child)
217:     {
218:     }
219: 
220: 
221: 
222:     /********************* cloneable, serializable ****************d*g**/
223: 
224: 
225: 
226:     /**
227:      * Object cloning.
228:      */
229:     public function __clone()
230:     {
231:         if ($this->components) {
232:             $oldMyself = reset($this->components)->getParent();
233:             $oldMyself->cloning = $this;
234:             foreach ($this->components as $name => $component) {
235:                 $this->components[$name] = clone $component;
236:             }
237:             $oldMyself->cloning = NULL;
238:         }
239:         parent::__clone();
240:     }
241: 
242: 
243: 
244:     /**
245:      * Is container cloning now?
246:      * @return NULL|IComponent
247:      * @internal
248:      */
249:     public function _isCloning()
250:     {
251:         return $this->cloning;
252:     }
253: 
254: }
255: 
Nette Framework 2.0.8 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.8.0