1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21:
22: class NComponentContainer extends NComponent 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: $component = $this->$method($name);
177: if (!$component instanceof IComponent && !isset($this->components[$name])) {
178: throw new UnexpectedValueException("Method {$this->reflection->name}::$method() did not return or create the desired component.");
179: }
180: return $component;
181: }
182: }
183:
184:
185:
186: 187: 188: 189: 190: 191:
192: final public function getComponents($deep = FALSE, $filterType = NULL)
193: {
194: $iterator = new NRecursiveComponentIterator($this->components);
195: if ($deep) {
196: $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
197: $iterator = new RecursiveIteratorIterator($iterator, $deep);
198: }
199: if ($filterType) {
200: if ($a = strrpos($filterType, '\\')) $filterType = substr($filterType, $a + 1); 201: $iterator = new NInstanceFilterIterator($iterator, $filterType);
202: }
203: return $iterator;
204: }
205:
206:
207:
208: 209: 210: 211: 212: 213:
214: protected function validateChildComponent(IComponent $child)
215: {
216: }
217:
218:
219:
220:
221:
222:
223:
224: 225: 226:
227: public function __clone()
228: {
229: if ($this->components) {
230: $oldMyself = reset($this->components)->getParent();
231: $oldMyself->cloning = $this;
232: foreach ($this->components as $name => $component) {
233: $this->components[$name] = clone $component;
234: }
235: $oldMyself->cloning = NULL;
236: }
237: parent::__clone();
238: }
239:
240:
241:
242: 243: 244: 245: 246:
247: public function _isCloning()
248: {
249: return $this->cloning;
250: }
251:
252: }
253: