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

  • 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 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 NComponentContainer  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_]+$#', $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 IComponentContainer.
103:      * @param  IComponent
104:      * @return void
105:      */
106:     public function removeComponent(IComponent $component)
107:     {
108:         $name = $component->getName();
109:         if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
110:             throw new InvalidArgumentException("Component named '$name' is not located in this container.");
111:         }
112: 
113:         unset($this->components[$name]);
114:         $component->setParent(NULL);
115:     }
116: 
117: 
118: 
119:     /**
120:      * Returns component specified by name or path.
121:      * @param  string
122:      * @param  bool   throw exception if component doesn't exist?
123:      * @return IComponent|NULL
124:      */
125:     final public function getComponent($name, $need = TRUE)
126:     {
127:         if (is_int($name)) {
128:             $name = (string) $name;
129: 
130:         } elseif (!is_string($name)) {
131:             throw new InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
132: 
133:         } else {
134:             $a = strpos($name, self::NAME_SEPARATOR);
135:             if ($a !== FALSE) {
136:                 $ext = (string) substr($name, $a + 1);
137:                 $name = substr($name, 0, $a);
138:             }
139: 
140:             if ($name === '') {
141:                 throw new InvalidArgumentException("Component or subcomponent name must not be empty string.");
142:             }
143:         }
144: 
145:         if (!isset($this->components[$name])) {
146:             $component = $this->createComponent($name);
147:             if ($component instanceof IComponent && $component->getParent() === NULL) {
148:                 $this->addComponent($component, $name);
149:             }
150:         }
151: 
152:         if (isset($this->components[$name])) {
153:             if (!isset($ext)) {
154:                 return $this->components[$name];
155: 
156:             } elseif ($this->components[$name] instanceof IComponentContainer) {
157:                 return $this->components[$name]->getComponent($ext, $need);
158: 
159:             } elseif ($need) {
160:                 throw new InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
161:             }
162: 
163:         } elseif ($need) {
164:             throw new InvalidArgumentException("Component with name '$name' does not exist.");
165:         }
166:     }
167: 
168: 
169: 
170:     /**
171:      * Component factory. Delegates the creation of components to a createComponent<Name> method.
172:      * @param  string      component name
173:      * @return IComponent  the created component (optionally)
174:      */
175:     protected function createComponent($name)
176:     {
177:         $ucname = ucfirst($name);
178:         $method = 'createComponent' . $ucname;
179:         if ($ucname !== $name && method_exists($this, $method) && $this->getReflection()->getMethod($method)->getName() === $method) {
180:             $component = $this->$method($name);
181:             if (!$component instanceof IComponent && !isset($this->components[$name])) {
182:                 $class = get_class($this);
183:                 throw new UnexpectedValueException("Method $class::$method() did not return or create the desired component.");
184:             }
185:             return $component;
186:         }
187:     }
188: 
189: 
190: 
191:     /**
192:      * Iterates over a components.
193:      * @param  bool    recursive?
194:      * @param  string  class types filter
195:      * @return ArrayIterator
196:      */
197:     final public function getComponents($deep = FALSE, $filterType = NULL)
198:     {
199:         $iterator = new NRecursiveComponentIterator($this->components);
200:         if ($deep) {
201:             $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
202:             $iterator = new RecursiveIteratorIterator($iterator, $deep);
203:         }
204:         if ($filterType) {
205:             $iterator = new NInstanceFilterIterator($iterator, $filterType);
206:         }
207:         return $iterator;
208:     }
209: 
210: 
211: 
212:     /**
213:      * Descendant can override this method to disallow insert a child by throwing an InvalidStateException.
214:      * @param  IComponent
215:      * @return void
216:      * @throws InvalidStateException
217:      */
218:     protected function validateChildComponent(IComponent $child)
219:     {
220:     }
221: 
222: 
223: 
224:     /********************* cloneable, serializable ****************d*g**/
225: 
226: 
227: 
228:     /**
229:      * Object cloning.
230:      */
231:     public function __clone()
232:     {
233:         if ($this->components) {
234:             $oldMyself = reset($this->components)->getParent();
235:             $oldMyself->cloning = $this;
236:             foreach ($this->components as $name => $component) {
237:                 $this->components[$name] = clone $component;
238:             }
239:             $oldMyself->cloning = NULL;
240:         }
241:         parent::__clone();
242:     }
243: 
244: 
245: 
246:     /**
247:      * Is container cloning now?
248:      * @return NULL|IComponent
249:      * @internal
250:      */
251:     public function _isCloning()
252:     {
253:         return $this->cloning;
254:     }
255: 
256: }
257: 
Nette Framework 2.0.0 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0