1: <?php
2: /**
3: * ManiaLive - TrackMania dedicated server manager in PHP
4: *
5: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
6: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
7: * @version $Revision: 294 $:
8: * @author $Author: martin.gwendal $:
9: * @date $Date: 2011-12-08 19:20:04 +0100 (jeu., 08 déc. 2011) $:
10: */
11:
12: namespace ManiaLive\Gui;
13:
14: use ManiaLib\Gui\Drawable;
15:
16: /**
17: * Contains objects. Those implementing the Containable interface will be informed about it.
18: */
19: abstract class Container extends \ManiaLib\Gui\Component
20: {
21: private $components = array();
22:
23: /**
24: * Adds a new Component to the Container.
25: * @param Drawable $component
26: */
27: function addComponent(Drawable $component)
28: {
29: if($component instanceof Containable)
30: $component->onIsAdded($this);
31: $this->components[spl_object_hash($component)] = $component;
32: }
33:
34: /**
35: * Retrieve a list of the components that are currently stored in the Container.
36: * @return array[Drawable]
37: */
38: function getComponents()
39: {
40: return $this->components;
41: }
42:
43: /**
44: * Count recursively how many basic elements are contained
45: * @return integer
46: */
47: function countElements()
48: {
49: $count = 0;
50: foreach($this->components as $component)
51: {
52: if($component instanceof Container)
53: $count += $component->countComponents();
54: else
55: ++$count;
56: }
57: }
58:
59: /**
60: * Removes a single component from the container.
61: * @param Drawable $component
62: */
63: function removeComponent(Drawable $component)
64: {
65: $hash = spl_object_hash($component);
66:
67: if(isset($this->components[$hash]))
68: {
69: if($this->components[$hash] instanceof Containable)
70: $this->components[$hash]->onIsRemoved($this);
71: unset($this->components[$hash]);
72: }
73: }
74:
75: /**
76: * Removes all objects from the container.
77: */
78: function clearComponents()
79: {
80: foreach($this->components as $component)
81: {
82: if($component instanceof Containable)
83: $component->onIsRemoved($this);
84: }
85: $this->components = array();
86: }
87: }
88:
89: ?>