1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21:
22: class ComponentContainer extends Component implements IComponentContainer
23: {
24:
25: private $components = array();
26:
27:
28: private $cloning;
29:
30:
31:
32:
33:
34:
35:
36: 37: 38: 39: 40: 41: 42: 43:
44: public function addComponent(IComponent $component, $name, $insertBefore = NULL)
45: {
46: if ($name === NULL) {
47: $name = $component->getName();
48: }
49:
50: if (is_int($name)) {
51: $name = (string) $name;
52:
53: } elseif (!is_string($name)) {
54: throw new InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
55:
56: } elseif (!preg_match('#^[a-zA-Z0-9_]+$#', $name)) {
57: throw new InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
58: }
59:
60: if (isset($this->components[$name])) {
61: throw new InvalidStateException("Component with name '$name' already exists.");
62: }
63:
64: 65: $obj = $this;
66: do {
67: if ($obj === $component) {
68: throw new InvalidStateException("Circular reference detected while adding component '$name'.");
69: }
70: $obj = $obj->getParent();
71: } while ($obj !== NULL);
72:
73: 74: $this->validateChildComponent($component);
75:
76: try {
77: if (isset($this->components[$insertBefore])) {
78: $tmp = array();
79: foreach ($this->components as $k => $v) {
80: if ($k === $insertBefore) $tmp[$name] = $component;
81: $tmp[$k] = $v;
82: }
83: $this->components = $tmp;
84: } else {
85: $this->components[$name] = $component;
86: }
87: $component->setParent($this, $name);
88:
89: } catch (Exception $e) {
90: unset($this->components[$name]); 91: throw $e;
92: }
93: }
94:
95:
96:
97: 98: 99: 100: 101:
102: public function removeComponent(IComponent $component)
103: {
104: $name = $component->getName();
105: if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
106: throw new InvalidArgumentException("Component named '$name' is not located in this container.");
107: }
108:
109: unset($this->components[$name]);
110: $component->setParent(NULL);
111: }
112:
113:
114:
115: 116: 117: 118: 119: 120:
121: final public function getComponent($name, $need = TRUE)
122: {
123: if (is_int($name)) {
124: $name = (string) $name;
125:
126: } elseif (!is_string($name)) {
127: throw new InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
128:
129: } else {
130: $a = strpos($name, self::NAME_SEPARATOR);
131: if ($a !== FALSE) {
132: $ext = (string) substr($name, $a + 1);
133: $name = substr($name, 0, $a);
134: }
135:
136: if ($name === '') {
137: throw new InvalidArgumentException("Component or subcomponent name must not be empty string.");
138: }
139: }
140:
141: if (!isset($this->components[$name])) {
142: $component = $this->createComponent($name);
143: if ($component instanceof IComponent && $component->getParent() === NULL) {
144: $this->addComponent($component, $name);
145: }
146: }
147:
148: if (isset($this->components[$name])) {
149: if (!isset($ext)) {
150: return $this->components[$name];
151:
152: } elseif ($this->components[$name] instanceof IComponentContainer) {
153: return $this->components[$name]->getComponent($ext, $need);
154:
155: } elseif ($need) {
156: throw new InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
157: }
158:
159: } elseif ($need) {
160: throw new InvalidArgumentException("Component with name '$name' does not exist.");
161: }
162: }
163:
164:
165:
166: 167: 168: 169: 170:
171: protected function createComponent($name)
172: {
173: $ucname = ucfirst($name);
174: $method = 'createComponent' . $ucname;
175: if ($ucname !== $name && method_exists($this, $method) && $this->getReflection()->getMethod($method)->getName() === $method) {
176: return $this->$method($name);
177: }
178: }
179:
180:
181:
182: 183: 184: 185: 186: 187:
188: final public function getComponents($deep = FALSE, $filterType = NULL)
189: {
190: $iterator = new RecursiveComponentIterator($this->components);
191: if ($deep) {
192: $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
193: $iterator = new RecursiveIteratorIterator($iterator, $deep);
194: }
195: if ($filterType) {
196: if ($a = strrpos($filterType, '\\')) $filterType = substr($filterType, $a + 1); 197: $iterator = new InstanceFilterIterator($iterator, $filterType);
198: }
199: return $iterator;
200: }
201:
202:
203:
204: 205: 206: 207: 208: 209:
210: protected function validateChildComponent(IComponent $child)
211: {
212: }
213:
214:
215:
216:
217:
218:
219:
220: 221: 222:
223: public function __clone()
224: {
225: if ($this->components) {
226: $oldMyself = reset($this->components)->getParent();
227: $oldMyself->cloning = $this;
228: foreach ($this->components as $name => $component) {
229: $this->components[$name] = clone $component;
230: }
231: $oldMyself->cloning = NULL;
232: }
233: parent::__clone();
234: }
235:
236:
237:
238: 239: 240: 241: 242:
243: public function _isCloning()
244: {
245: return $this->cloning;
246: }
247:
248: }
249:
250:
251:
252: 253: 254: 255: 256:
257: class RecursiveComponentIterator extends RecursiveArrayIterator implements Countable
258: {
259:
260: 261: 262: 263:
264: public function hasChildren()
265: {
266: return $this->current() instanceof IComponentContainer;
267: }
268:
269:
270:
271: 272: 273: 274:
275: public function getChildren()
276: {
277: return $this->current()->getComponents();
278: }
279:
280:
281:
282: 283: 284: 285:
286: public function count()
287: {
288: return iterator_count($this);
289: }
290:
291: }
292: