00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 class Component extends Object {
00032
00033
00034
00035
00036
00037
00038 var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
00039
00040
00041
00042
00043
00044
00045 var $_loaded = array();
00046
00047
00048
00049
00050
00051
00052
00053 var $_primary = array();
00054
00055
00056
00057
00058
00059
00060 var $__settings = array();
00061
00062
00063
00064
00065
00066
00067
00068 function init(&$controller) {
00069 if (!is_array($controller->components)) {
00070 return;
00071 }
00072 $this->__controllerVars = array(
00073 'plugin' => $controller->plugin, 'name' => $controller->name,
00074 'base' => $controller->base
00075 );
00076
00077 $this->_loadComponents($controller);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 function initialize(&$controller) {
00088 foreach (array_keys($this->_loaded) as $name) {
00089 $component =& $this->_loaded[$name];
00090
00091 if (method_exists($component,'initialize') && $component->enabled === true) {
00092 $settings = array();
00093 if (isset($this->__settings[$name])) {
00094 $settings = $this->__settings[$name];
00095 }
00096 $component->initialize($controller, $settings);
00097 }
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108 function startup(&$controller) {
00109 foreach ($this->_primary as $name) {
00110 $component =& $this->_loaded[$name];
00111 if ($component->enabled === true && method_exists($component, 'startup')) {
00112 $component->startup($controller);
00113 }
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124 function beforeRender(&$controller) {
00125 foreach ($this->_primary as $name) {
00126 $component =& $this->_loaded[$name];
00127 if ($component->enabled === true && method_exists($component,'beforeRender')) {
00128 $component->beforeRender($controller);
00129 }
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
00140 $response = array();
00141
00142 foreach ($this->_primary as $name) {
00143 $component =& $this->_loaded[$name];
00144
00145 if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
00146 $resp = $component->beforeRedirect($controller, $url, $status, $exit);
00147 if ($resp === false) {
00148 return false;
00149 }
00150 $response[] = $resp;
00151 }
00152 }
00153 return $response;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 function shutdown(&$controller) {
00163 foreach ($this->_primary as $name) {
00164 $component =& $this->_loaded[$name];
00165 if (method_exists($component,'shutdown') && $component->enabled === true) {
00166 $component->shutdown($controller);
00167 }
00168 }
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178 function _loadComponents(&$object, $parent = null) {
00179 $base = $this->__controllerVars['base'];
00180 $normal = Set::normalize($object->components);
00181 if ($parent == null) {
00182 $normal = Set::merge(array('Session' => null), $normal);
00183 }
00184 foreach ((array)$normal as $component => $config) {
00185 $plugin = null;
00186
00187 if (isset($this->__controllerVars['plugin'])) {
00188 $plugin = $this->__controllerVars['plugin'] . '.';
00189 }
00190
00191 if (strpos($component, '.') !== false) {
00192 list($plugin, $component) = explode('.', $component);
00193 $plugin = $plugin . '.';
00194 }
00195 $componentCn = $component . 'Component';
00196
00197 if (!class_exists($componentCn)) {
00198 if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
00199 if (!App::import('Component', $component)) {
00200 $this->cakeError('missingComponentFile', array(array(
00201 'className' => $this->__controllerVars['name'],
00202 'component' => $component,
00203 'file' => Inflector::underscore($component) . '.php',
00204 'base' => $base,
00205 'code' => 500
00206 )));
00207 return false;
00208 }
00209 }
00210
00211 if (!class_exists($componentCn)) {
00212 $this->cakeError('missingComponentClass', array(array(
00213 'className' => $this->__controllerVars['name'],
00214 'component' => $component,
00215 'file' => Inflector::underscore($component) . '.php',
00216 'base' => $base,
00217 'code' => 500
00218 )));
00219 return false;
00220 }
00221 }
00222
00223 if ($parent === null) {
00224 $this->_primary[] = $component;
00225 }
00226
00227 if (isset($this->_loaded[$component])) {
00228 $object->{$component} =& $this->_loaded[$component];
00229
00230 if (!empty($config) && isset($this->__settings[$component])) {
00231 $this->__settings[$component] = array_merge($this->__settings[$component], $config);
00232 } elseif (!empty($config)) {
00233 $this->__settings[$component] = $config;
00234 }
00235 } else {
00236 if ($componentCn === 'SessionComponent') {
00237 $object->{$component} =& new $componentCn($base);
00238 } else {
00239 $object->{$component} =& new $componentCn();
00240 }
00241 $object->{$component}->enabled = true;
00242 $this->_loaded[$component] =& $object->{$component};
00243 if (!empty($config)) {
00244 $this->__settings[$component] = $config;
00245 }
00246 }
00247
00248 if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
00249 $this->_loadComponents($object->{$component}, $component);
00250 }
00251 }
00252 }
00253 }
00254
00255 ?>