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) {
82: $tmp[$name] = $component;
83: }
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: return $this;
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: $component = $this->$method($name);
180: if (!$component instanceof IComponent && !isset($this->components[$name])) {
181: $class = get_class($this);
182: throw new UnexpectedValueException("Method $class::$method() did not return or create the desired component.");
183: }
184: return $component;
185: }
186: }
187:
188:
189:
190: 191: 192: 193: 194: 195:
196: final public function getComponents($deep = FALSE, $filterType = NULL)
197: {
198: $iterator = new NRecursiveComponentIterator($this->components);
199: if ($deep) {
200: $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
201: $iterator = new RecursiveIteratorIterator($iterator, $deep);
202: }
203: if ($filterType) {
204: $iterator = new NInstanceFilterIterator($iterator, $filterType);
205: }
206: return $iterator;
207: }
208:
209:
210:
211: 212: 213: 214: 215:
216: protected function validateChildComponent(IComponent $child)
217: {
218: }
219:
220:
221:
222:
223:
224:
225:
226: 227: 228:
229: public function __clone()
230: {
231: if ($this->components) {
232: $oldMyself = reset($this->components)->getParent();
233: $oldMyself->cloning = $this;
234: foreach ($this->components as $name => $component) {
235: $this->components[$name] = clone $component;
236: }
237: $oldMyself->cloning = NULL;
238: }
239: parent::__clone();
240: }
241:
242:
243:
244: 245: 246: 247: 248:
249: public function _isCloning()
250: {
251: return $this->cloning;
252: }
253:
254: }
255: