component.php
Go to the documentation of this file.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
00032 class Component extends Object {
00033
00034
00035
00036
00037
00038
00039 var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
00040
00041
00042
00043
00044
00045
00046 var $__loaded = array();
00047
00048
00049
00050
00051
00052
00053 var $__settings = array();
00054
00055
00056
00057
00058
00059
00060 function init(&$controller) {
00061 if ($controller->components !== false && is_array($controller->components)) {
00062 $this->__controllerVars = array(
00063 'plugin' => $controller->plugin, 'name' => $controller->name, 'base' => $controller->base
00064 );
00065
00066 if (!in_array('Session', $controller->components)) {
00067 array_unshift($controller->components, 'Session');
00068 }
00069 $this->_loadComponents($controller);
00070 }
00071 }
00072
00073
00074
00075
00076
00077
00078 function initialize(&$controller) {
00079 foreach (array_keys($this->__loaded) as $name) {
00080 $component =& $this->__loaded[$name];
00081 if (method_exists($component,'initialize') && $component->enabled === true) {
00082 $settings = array();
00083 if (isset($this->__settings[$name])) {
00084 $settings = $this->__settings[$name];
00085 }
00086 $component->initialize($controller, $settings);
00087 }
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096 function startup(&$controller) {
00097 foreach (array_keys($this->__loaded) as $name) {
00098 $component =& $this->__loaded[$name];
00099 if (method_exists($component,'startup') && $component->enabled === true) {
00100 $component->startup($controller);
00101 }
00102 }
00103 }
00104
00105
00106
00107
00108
00109
00110 function beforeRender(&$controller) {
00111 foreach (array_keys($this->__loaded) as $name) {
00112 $component =& $this->__loaded[$name];
00113 if (method_exists($component,'beforeRender') && $component->enabled === true) {
00114 $component->beforeRender($controller);
00115 }
00116 }
00117 }
00118
00119
00120
00121
00122
00123
00124 function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
00125 $response = array();
00126 foreach (array_keys($this->__loaded) as $name) {
00127 $component =& $this->__loaded[$name];
00128 if (method_exists($component,'beforeRedirect') && $component->enabled === true) {
00129 $resp = $component->beforeRedirect($controller, $url, $status, $exit);
00130 if ($resp === false) {
00131 return false;
00132 }
00133 $response[] = $resp;
00134 }
00135 }
00136 return $response;
00137 }
00138
00139
00140
00141
00142
00143
00144 function shutdown(&$controller) {
00145 foreach (array_keys($this->__loaded) as $name) {
00146 $component =& $this->__loaded[$name];
00147 if (method_exists($component,'shutdown') && $component->enabled === true) {
00148 $component->shutdown($controller);
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160 function _loadComponents(&$object, $parent = null) {
00161 $components = $object->components;
00162 $base = $this->__controllerVars['base'];
00163
00164 if (is_array($object->components)) {
00165 $normal = Set::normalize($object->components);
00166 foreach ($normal as $component => $config) {
00167 $parts = preg_split('/\/|\./', $component);
00168
00169 if (count($parts) === 1) {
00170 $plugin = $this->__controllerVars['plugin'] . '.';
00171 } else {
00172 $plugin = Inflector::underscore($parts['0']) . '.';
00173 $component = array_pop($parts);
00174 }
00175 $componentCn = $component . 'Component';
00176
00177 if (!class_exists($componentCn)) {
00178 if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
00179 if (!App::import('Component', $component)) {
00180 $this->cakeError('missingComponentFile', array(array(
00181 'className' => $this->__controllerVars['name'],
00182 'component' => $component,
00183 'file' => Inflector::underscore($component) . '.php',
00184 'base' => $base,
00185 'code' => 500
00186 )));
00187 return false;
00188 }
00189 }
00190
00191 if (!class_exists($componentCn)) {
00192 $this->cakeError('missingComponentClass', array(array(
00193 'className' => $this->__controllerVars['name'],
00194 'component' => $component,
00195 'file' => Inflector::underscore($component) . '.php',
00196 'base' => $base,
00197 'code' => 500
00198 )));
00199 return false;
00200 }
00201 }
00202
00203 if (isset($this->__loaded[$component])) {
00204 $object->{$component} =& $this->__loaded[$component];
00205
00206 if (!empty($config) && isset($this->__settings[$component])) {
00207 $this->__settings[$component] = array_merge($this->__settings[$component], $config);
00208 } elseif (!empty($config)) {
00209 $this->__settings[$component] = $config;
00210 }
00211 } else {
00212 if ($componentCn == 'SessionComponent') {
00213 $object->{$component} =& new $componentCn($base);
00214 } else {
00215 $object->{$component} =& new $componentCn();
00216 }
00217 $object->{$component}->enabled = true;
00218 $this->__loaded[$component] =& $object->{$component};
00219 if (!empty($config)) {
00220 $this->__settings[$component] = $config;
00221 }
00222 }
00223
00224 if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
00225 $this->_loadComponents($object->{$component}, $component);
00226 }
00227 }
00228 }
00229 }
00230 }
00231
00232 ?>