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 ComponentContainer extends Component 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:
106: public function removeComponent(IComponent $component)
107: {
108: $name = $component->getName();
109: if (!isset($this->components[$name]) || $this->components[$name] !== $component) {
110: throw new InvalidArgumentException("Component named '$name' is not located in this container.");
111: }
112:
113: unset($this->components[$name]);
114: $component->setParent(NULL);
115: }
116:
117:
118:
119: 120: 121: 122: 123: 124:
125: final public function getComponent($name, $need = TRUE)
126: {
127: if (is_int($name)) {
128: $name = (string) $name;
129:
130: } elseif (!is_string($name)) {
131: throw new InvalidArgumentException("Component name must be integer or string, " . gettype($name) . " given.");
132:
133: } else {
134: $a = strpos($name, self::NAME_SEPARATOR);
135: if ($a !== FALSE) {
136: $ext = (string) substr($name, $a + 1);
137: $name = substr($name, 0, $a);
138: }
139:
140: if ($name === '') {
141: throw new InvalidArgumentException("Component or subcomponent name must not be empty string.");
142: }
143: }
144:
145: if (!isset($this->components[$name])) {
146: $component = $this->createComponent($name);
147: if ($component instanceof IComponent && $component->getParent() === NULL) {
148: $this->addComponent($component, $name);
149: }
150: }
151:
152: if (isset($this->components[$name])) {
153: if (!isset($ext)) {
154: return $this->components[$name];
155:
156: } elseif ($this->components[$name] instanceof IComponentContainer) {
157: return $this->components[$name]->getComponent($ext, $need);
158:
159: } elseif ($need) {
160: throw new InvalidArgumentException("Component with name '$name' is not container and cannot have '$ext' component.");
161: }
162:
163: } elseif ($need) {
164: throw new InvalidArgumentException("Component with name '$name' does not exist.");
165: }
166: }
167:
168:
169:
170: 171: 172: 173: 174:
175: protected function createComponent($name)
176: {
177: $ucname = ucfirst($name);
178: $method = 'createComponent' . $ucname;
179: if ($ucname !== $name && method_exists($this, $method) && $this->getReflection()->getMethod($method)->getName() === $method) {
180: $component = $this->$method($name);
181: if (!$component instanceof IComponent && !isset($this->components[$name])) {
182: $class = get_class($this);
183: throw new UnexpectedValueException("Method $class::$method() did not return or create the desired component.");
184: }
185: return $component;
186: }
187: }
188:
189:
190:
191: 192: 193: 194: 195: 196:
197: final public function getComponents($deep = FALSE, $filterType = NULL)
198: {
199: $iterator = new RecursiveComponentIterator($this->components);
200: if ($deep) {
201: $deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
202: $iterator = new RecursiveIteratorIterator($iterator, $deep);
203: }
204: if ($filterType) {
205: $iterator = new InstanceFilterIterator($iterator, $filterType);
206: }
207: return $iterator;
208: }
209:
210:
211:
212: 213: 214: 215: 216: 217:
218: protected function validateChildComponent(IComponent $child)
219: {
220: }
221:
222:
223:
224:
225:
226:
227:
228: 229: 230:
231: public function __clone()
232: {
233: if ($this->components) {
234: $oldMyself = reset($this->components)->getParent();
235: $oldMyself->cloning = $this;
236: foreach ($this->components as $name => $component) {
237: $this->components[$name] = clone $component;
238: }
239: $oldMyself->cloning = NULL;
240: }
241: parent::__clone();
242: }
243:
244:
245:
246: 247: 248: 249: 250:
251: public function _isCloning()
252: {
253: return $this->cloning;
254: }
255:
256: }
257: