PageRenderTime 165ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/msadouni/cakephp2x
PHP | 254 lines | 129 code | 23 blank | 102 comment | 31 complexity | a417eb706730f8a195ee1f7adc2ef7e7 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * PHP Version 5.x
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package cake
  15. * @subpackage cake.cake.libs.controller
  16. * @since CakePHP(tm) v TBD
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Handler for Controller::$components
  21. *
  22. * @package cake
  23. * @subpackage cake.cake.libs.controller
  24. * @link http://book.cakephp.org/view/62/Components
  25. */
  26. class Component extends Object {
  27. /**
  28. * Contains various controller variable information (plugin, name, base).
  29. *
  30. * @var object
  31. * @access private
  32. */
  33. private $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
  34. /**
  35. * List of loaded components.
  36. *
  37. * @var object
  38. * @access protected
  39. */
  40. protected $_loaded = array();
  41. /**
  42. * List of components attached directly to the controller, which callbacks
  43. * should be executed on.
  44. *
  45. * @var object
  46. * @access protected
  47. */
  48. protected $_primary = array();
  49. /**
  50. * Settings for loaded components.
  51. *
  52. * @var array
  53. * @access private
  54. */
  55. private $__settings = array();
  56. /**
  57. * Used to initialize the components for current controller.
  58. *
  59. * @param object $controller Controller with components to load
  60. * @return void
  61. * @access public
  62. */
  63. public function init(&$controller) {
  64. if (!is_array($controller->components)) {
  65. return;
  66. }
  67. $this->__controllerVars = array(
  68. 'plugin' => $controller->plugin, 'name' => $controller->name,
  69. 'base' => $controller->base
  70. );
  71. $this->_loadComponents($controller);
  72. }
  73. /**
  74. * Called before the Controller::beforeFilter().
  75. *
  76. * @param object $controller Controller with components to initialize
  77. * @return void
  78. * @access public
  79. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  80. */
  81. public function initialize(&$controller) {
  82. foreach (array_keys($this->_loaded) as $name) {
  83. $component = $this->_loaded[$name];
  84. if (method_exists($component,'initialize') && $component->enabled === true) {
  85. $settings = array();
  86. if (isset($this->__settings[$name])) {
  87. $settings = $this->__settings[$name];
  88. }
  89. $component->initialize($controller, $settings);
  90. }
  91. }
  92. }
  93. /**
  94. * Called after the Controller::beforeFilter() and before the controller action
  95. *
  96. * @param object $controller Controller with components to startup
  97. * @return void
  98. * @access public
  99. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  100. */
  101. public function startup(&$controller) {
  102. foreach ($this->_primary as $name) {
  103. $component = $this->_loaded[$name];
  104. if ($component->enabled === true && method_exists($component, 'startup')) {
  105. $component->startup($controller);
  106. }
  107. }
  108. }
  109. /**
  110. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  111. * Controller::render()
  112. *
  113. * @param object $controller Controller with components to beforeRender
  114. * @return void
  115. * @access public
  116. */
  117. public function beforeRender(&$controller) {
  118. foreach ($this->_primary as $name) {
  119. $component = $this->_loaded[$name];
  120. if ($component->enabled === true && method_exists($component,'beforeRender')) {
  121. $component->beforeRender($controller);
  122. }
  123. }
  124. }
  125. /**
  126. * Called before Controller::redirect().
  127. *
  128. * @param object $controller Controller with components to beforeRedirect
  129. * @return void
  130. * @access public
  131. */
  132. public function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
  133. $response = array();
  134. foreach ($this->_primary as $name) {
  135. $component = $this->_loaded[$name];
  136. if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
  137. $resp = $component->beforeRedirect($controller, $url, $status, $exit);
  138. if ($resp === false) {
  139. return false;
  140. }
  141. $response[] = $resp;
  142. }
  143. }
  144. return $response;
  145. }
  146. /**
  147. * Called after Controller::render() and before the output is printed to the browser.
  148. *
  149. * @param object $controller Controller with components to shutdown
  150. * @return void
  151. * @access public
  152. */
  153. public function shutdown(&$controller) {
  154. foreach ($this->_primary as $name) {
  155. $component = $this->_loaded[$name];
  156. if (method_exists($component,'shutdown') && $component->enabled === true) {
  157. $component->shutdown($controller);
  158. }
  159. }
  160. }
  161. /**
  162. * Loads components used by this component.
  163. *
  164. * @param object $object Object with a Components array
  165. * @param object $parent the parent of the current object
  166. * @return void
  167. * @access protected
  168. */
  169. protected function _loadComponents(&$object, $parent = null) {
  170. $base = $this->__controllerVars['base'];
  171. $normal = Set::normalize($object->components);
  172. if ($parent == null) {
  173. $normal = Set::merge(array('Session' => null), $normal);
  174. }
  175. foreach ((array)$normal as $component => $config) {
  176. $plugin = isset($this->__controllerVars['plugin']) ? $this->__controllerVars['plugin'] . '.' : null;
  177. list($plugin, $component) = pluginSplit($component, true, $plugin);
  178. $componentCn = $component . 'Component';
  179. if (!class_exists($componentCn)) {
  180. if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
  181. if (!App::import('Component', $component)) {
  182. $this->cakeError('missingComponentFile', array(array(
  183. 'className' => $this->__controllerVars['name'],
  184. 'component' => $component,
  185. 'file' => Inflector::underscore($component) . '.php',
  186. 'base' => $base,
  187. 'code' => 500
  188. )));
  189. return false;
  190. }
  191. }
  192. if (!class_exists($componentCn)) {
  193. $this->cakeError('missingComponentClass', array(array(
  194. 'className' => $this->__controllerVars['name'],
  195. 'component' => $component,
  196. 'file' => Inflector::underscore($component) . '.php',
  197. 'base' => $base,
  198. 'code' => 500
  199. )));
  200. return false;
  201. }
  202. }
  203. if ($parent === null) {
  204. $this->_primary[] = $component;
  205. }
  206. if (isset($this->_loaded[$component])) {
  207. $object->{$component} = $this->_loaded[$component];
  208. if (!empty($config) && isset($this->__settings[$component])) {
  209. $this->__settings[$component] = array_merge($this->__settings[$component], $config);
  210. } elseif (!empty($config)) {
  211. $this->__settings[$component] = $config;
  212. }
  213. } else {
  214. if ($componentCn === 'SessionComponent') {
  215. $object->{$component} = new $componentCn($base);
  216. } else {
  217. $object->{$component} = new $componentCn();
  218. }
  219. $object->{$component}->enabled = true;
  220. $this->_loaded[$component] = $object->{$component};
  221. if (!empty($config)) {
  222. $this->__settings[$component] = $config;
  223. }
  224. }
  225. if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
  226. $this->_loadComponents($object->{$component}, $component);
  227. }
  228. }
  229. }
  230. }
  231. ?>