Packages

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

Classes

  • NComponent
  • NComponentContainer

Interfaces

  • IComponent
  • IComponentContainer
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 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 IComponentContainer.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property-read ArrayIterator $components
 21:  * @package Nette\ComponentModel
 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) {
 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:     }
 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:             $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 NRecursiveComponentIterator($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 NInstanceFilterIterator($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:      * @param  IComponent
214:      * @return void
215:      * @throws InvalidStateException
216:      */
217:     protected function validateChildComponent(IComponent $child)
218:     {
219:     }
220: 
221: 
222: 
223:     /********************* cloneable, serializable ****************d*g**/
224: 
225: 
226: 
227:     /**
228:      * Object cloning.
229:      */
230:     public function __clone()
231:     {
232:         if ($this->components) {
233:             $oldMyself = reset($this->components)->getParent();
234:             $oldMyself->cloning = $this;
235:             foreach ($this->components as $name => $component) {
236:                 $this->components[$name] = clone $component;
237:             }
238:             $oldMyself->cloning = NULL;
239:         }
240:         parent::__clone();
241:     }
242: 
243: 
244: 
245:     /**
246:      * Is container cloning now?
247:      * @return NULL|IComponent
248:      * @internal
249:      */
250:     public function _isCloning()
251:     {
252:         return $this->cloning;
253:     }
254: 
255: }
256: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0