1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22: 23: 24:
25: class ComponentContainer extends Component implements IComponentContainer
26: {
27:
28: private $components = array();
29:
30:
31: private $cloning;
32:
33:
34:
35:
36:
37:
38:
39: 40: 41: 42: 43: 44: 45: 46:
47: public function addComponent(IComponent $component, $name, $insertBefore = NULL)
48: {
49: if ($name === NULL) {
50: $name = $component->getName();
51: }
52:
53: if (is_int($name)) {
54: $name = (string) $name;
55:
56: } elseif (!is_string($name)) {
57: throw new \InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
58:
59: } elseif (!preg_match('#^[a-zA-Z0-9_]+$#', $name)) {
60: throw new \InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
61: }
62:
63: if (isset($this->components[$name])) {
64: throw new \InvalidStateException("Component with name '$name' already exists.");
65: }
66:
67:
68: $obj = $this;
69: do {
70: if ($obj === $component) {
71: throw new \InvalidStateException("Circular reference detected while adding component '$name'.");
72: }
73: $obj = $obj->getParent();
74: } while ($obj !== NULL);
75:
76:
77: $this->validateChildComponent($component);
78:
79: try {
80: if (isset($this->components[$insertBefore])) {
81: $tmp = array();
82: foreach ($this->components as $k => $v) {
83: if ($k === $insertBefore) $tmp[$name] = $component;
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]);
94: throw $e;
95: }
96: }
97:
98:
99:
100: 101: 102: 103: 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: 120: 121: 122: 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: 171: 172: 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: return $this->$method($name);
180: }
181: }
182:
183:
184:
185: 186: 187: 188: 189: 190:
191: final public function getComponents($deep = FALSE, $filterType = NULL)
192: {
193: $iterator = new RecursiveComponentIterator($this->components);
194: if ($deep) {
195: $deep = $deep > 0 ? \RecursiveIteratorIterator::SELF_FIRST : \RecursiveIteratorIterator::CHILD_FIRST;
196: $iterator = new \RecursiveIteratorIterator($iterator, $deep);
197: }
198: if ($filterType) {
199: $iterator = new InstanceFilterIterator($iterator, $filterType);
200: }
201: return $iterator;
202: }
203:
204:
205:
206: 207: 208: 209: 210: 211:
212: protected function validateChildComponent(IComponent $child)
213: {
214: }
215:
216:
217:
218:
219:
220:
221:
222: 223: 224:
225: public function __clone()
226: {
227: if ($this->components) {
228: $oldMyself = reset($this->components)->getParent();
229: $oldMyself->cloning = $this;
230: foreach ($this->components as $name => $component) {
231: $this->components[$name] = clone $component;
232: }
233: $oldMyself->cloning = NULL;
234: }
235: parent::__clone();
236: }
237:
238:
239:
240: 241: 242: 243: 244:
245: public function _isCloning()
246: {
247: return $this->cloning;
248: }
249:
250: }
251:
252:
253:
254: 255: 256: 257: 258:
259: class RecursiveComponentIterator extends \RecursiveArrayIterator implements \Countable
260: {
261:
262: 263: 264: 265:
266: public function hasChildren()
267: {
268: return $this->current() instanceof IComponentContainer;
269: }
270:
271:
272:
273: 274: 275: 276:
277: public function getChildren()
278: {
279: return $this->current()->getComponents();
280: }
281:
282:
283:
284: 285: 286: 287:
288: public function count()
289: {
290: return iterator_count($this);
291: }
292:
293: }
294: