1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22:
23: class NComponentContainer extends NComponent implements IComponentContainer
24: {
25:
26: private $components = array();
27:
28:
29: private $cloning;
30:
31:
32:
33:
34:
35:
36:
37: 38: 39: 40: 41: 42: 43: 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:
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:
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) $tmp[$name] = $component;
82: $tmp[$k] = $v;
83: }
84: $this->components = $tmp;
85: } else {
86: $this->components[$name] = $component;
87: }
88: $component->setParent($this, $name);
89:
90: } catch (Exception $e) {
91: unset($this->components[$name]);
92: throw $e;
93: }
94: }
95:
96:
97:
98: 99: 100: 101: 102:
103: public function removeComponent(IComponent $component)
104: {
105: $name = $component->getName();
106: if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
107: throw new InvalidArgumentException("Component named '$name' is not located in this container.");
108: }
109:
110: unset($this->components[$name]);
111: $component->setParent(NULL);
112: }
113:
114:
115:
116: 117: 118: 119: 120: 121:
122: final public function getComponent($name, $need = TRUE)
123: {
124: if (is_int($name)) {
125: $name = (string) $name;
126:
127: } elseif (!is_string($name)) {
128: throw new InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
129:
130: } else {
131: $a = strpos($name, self::NAME_SEPARATOR);
132: if ($a !== FALSE) {
133: $ext = (string) substr($name, $a + 1);
134: $name = substr($name, 0, $a);
135: }
136:
137: if ($name === '') {
138: throw new InvalidArgumentException("Component or subcomponent name must not be empty string.");
139: }
140: }
141:
142: if (!isset($this->components[$name])) {
143: $component = $this->createComponent($name);
144: if ($component instanceof IComponent && $component->getParent() === NULL) {
145: $this->addComponent($component, $name);
146: }
147: }
148:
149: if (isset($this->components[$name])) {
150: if (!isset($ext)) {
151: return $this->components[$name];
152:
153: } elseif ($this->components[$name] instanceof IComponentContainer) {
154: return $this->components[$name]->getComponent($ext, $need);
155:
156: } elseif ($need) {
157: throw new InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
158: }
159:
160: } elseif ($need) {
161: throw new InvalidArgumentException("Component with name '$name' does not exist.");
162: }
163: }
164:
165:
166:
167: 168: 169: 170: 171:
172: protected function createComponent($name)
173: {
174: $ucname = ucfirst($name);
175: $method = 'createComponent' . $ucname;
176: if ($ucname !== $name && method_exists($this, $method) && $this->getReflection()->getMethod($method)->getName() === $method) {
177: return $this->$method($name);
178: }
179: }
180:
181:
182:
183: 184: 185: 186: 187: 188:
189: final public function getComponents($deep = FALSE, $filterType = NULL)
190: {
191: $iterator = new NRecursiveComponentIterator($this->components);
192: if ($deep) {
193: $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
194: $iterator = new RecursiveIteratorIterator($iterator, $deep);
195: }
196: if ($filterType) {
197: if ($a = strrpos($filterType, '\\')) $filterType = substr($filterType, $a + 1);
198: $iterator = new NInstanceFilterIterator($iterator, $filterType);
199: }
200: return $iterator;
201: }
202:
203:
204:
205: 206: 207: 208: 209: 210:
211: protected function validateChildComponent(IComponent $child)
212: {
213: }
214:
215:
216:
217:
218:
219:
220:
221: 222: 223:
224: public function __clone()
225: {
226: if ($this->components) {
227: $oldMyself = reset($this->components)->getParent();
228: $oldMyself->cloning = $this;
229: foreach ($this->components as $name => $component) {
230: $this->components[$name] = clone $component;
231: }
232: $oldMyself->cloning = NULL;
233: }
234: parent::__clone();
235: }
236:
237:
238:
239: 240: 241: 242: 243:
244: public function _isCloning()
245: {
246: return $this->cloning;
247: }
248:
249: }
250:
251:
252:
253: 254: 255: 256: 257: 258:
259: class NRecursiveComponentIterator 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: