1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22: 
 23:  24:  25:  26:  27:  28:  29: 
 30: class Component extends Object {
 31:  32:  33:  34:  35:  36: 
 37:     var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
 38:  39:  40:  41:  42:  43: 
 44:     var $_loaded = array();
 45:  46:  47:  48:  49:  50:  51: 
 52:     var $_primary = array();
 53:  54:  55:  56:  57:  58: 
 59:     var $__settings = array();
 60:  61:  62:  63:  64:  65:  66: 
 67:     function init(&$controller) {
 68:         if (!is_array($controller->components)) {
 69:             return;
 70:         }
 71:         $this->__controllerVars = array(
 72:             'plugin' => $controller->plugin, 'name' => $controller->name,
 73:             'base' => $controller->base
 74:         );
 75: 
 76:         $this->_loadComponents($controller);
 77:     }
 78:  79:  80:  81:  82:  83:  84:  85: 
 86:     function initialize(&$controller) {
 87:         foreach (array_keys($this->_loaded) as $name) {
 88:             $component =& $this->_loaded[$name];
 89: 
 90:             if (method_exists($component,'initialize') && $component->enabled === true) {
 91:                 $settings = array();
 92:                 if (isset($this->__settings[$name])) {
 93:                     $settings = $this->__settings[$name];
 94:                 }
 95:                 $component->initialize($controller, $settings);
 96:             }
 97:         }
 98:     }
 99: 100: 101: 102: 103: 104: 105: 106: 
107:     function startup(&$controller) {
108:         foreach ($this->_primary as $name) {
109:             $component =& $this->_loaded[$name];
110:             if ($component->enabled === true && method_exists($component, 'startup')) {
111:                 $component->startup($controller);
112:             }
113:         }
114:     }
115: 116: 117: 118: 119: 120: 121: 122: 
123:     function beforeRender(&$controller) {
124:         foreach ($this->_primary as $name) {
125:             $component =& $this->_loaded[$name];
126:             if ($component->enabled === true && method_exists($component,'beforeRender')) {
127:                 $component->beforeRender($controller);
128:             }
129:         }
130:     }
131: 132: 133: 134: 135: 136: 137: 
138:     function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
139:         $response = array();
140: 
141:         foreach ($this->_primary as $name) {
142:             $component =& $this->_loaded[$name];
143: 
144:             if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
145:                 $resp = $component->beforeRedirect($controller, $url, $status, $exit);
146:                 if ($resp === false) {
147:                     return false;
148:                 }
149:                 $response[] = $resp;
150:             }
151:         }
152:         return $response;
153:     }
154: 155: 156: 157: 158: 159: 160: 
161:     function shutdown(&$controller) {
162:         foreach ($this->_primary as $name) {
163:             $component =& $this->_loaded[$name];
164:             if (method_exists($component,'shutdown') && $component->enabled === true) {
165:                 $component->shutdown($controller);
166:             }
167:         }
168:     }
169: 170: 171: 172: 173: 174: 175: 176: 
177:     function _loadComponents(&$object, $parent = null) {
178:         $base = $this->__controllerVars['base'];
179:         $normal = Set::normalize($object->components);
180:         if ($parent == null) {
181:             $normal = Set::merge(array('Session' => null), $normal);
182:         }
183:         foreach ((array)$normal as $component => $config) {
184:             $plugin = null;
185: 
186:             if (isset($this->__controllerVars['plugin'])) {
187:                 $plugin = $this->__controllerVars['plugin'] . '.';
188:             }
189: 
190:             if (strpos($component, '.') !== false) {
191:                 list($plugin, $component) = explode('.', $component);
192:                 $plugin = $plugin . '.';
193:             }
194:             $componentCn = $component . 'Component';
195: 
196:             if (!class_exists($componentCn)) {
197:                 if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
198:                     if (!App::import('Component', $component)) {
199:                         $this->cakeError('missingComponentFile', array(array(
200:                             'className' => $this->__controllerVars['name'],
201:                             'component' => $component,
202:                             'file' => Inflector::underscore($component) . '.php',
203:                             'base' => $base,
204:                             'code' => 500
205:                         )));
206:                         return false;
207:                     }
208:                 }
209: 
210:                 if (!class_exists($componentCn)) {
211:                     $this->cakeError('missingComponentClass', array(array(
212:                         'className' => $this->__controllerVars['name'],
213:                         'component' => $component,
214:                         'file' => Inflector::underscore($component) . '.php',
215:                         'base' => $base,
216:                         'code' => 500
217:                     )));
218:                     return false;
219:                 }
220:             }
221: 
222:             if ($parent === null) {
223:                 $this->_primary[] = $component;
224:             }
225: 
226:             if (isset($this->_loaded[$component])) {
227:                 $object->{$component} =& $this->_loaded[$component];
228: 
229:                 if (!empty($config) && isset($this->__settings[$component])) {
230:                     $this->__settings[$component] = array_merge($this->__settings[$component], $config);
231:                 } elseif (!empty($config)) {
232:                     $this->__settings[$component] = $config;
233:                 }
234:             } else {
235:                 if ($componentCn === 'SessionComponent') {
236:                     $object->{$component} =& new $componentCn($base);
237:                 } else {
238:                     $object->{$component} =& new $componentCn();
239:                 }
240:                 $object->{$component}->enabled = true;
241:                 $this->_loaded[$component] =& $object->{$component};
242:                 if (!empty($config)) {
243:                     $this->__settings[$component] = $config;
244:                 }
245:             }
246: 
247:             if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
248:                 $this->_loadComponents($object->{$component}, $component);
249:             }
250:         }
251:     }
252: }
253: 
254: ?>