Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Component
  • Container

Interfaces

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