PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/controller/component.php

http://github.com/Datawalke/Coordino
PHP | 267 lines | 122 code | 23 blank | 122 comment | 27 complexity | e0ccc00e761ef2c4ca1298491ab04775 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * PHP versions 4 and 5
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2012, 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-2012, 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/993/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. var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
  34. /**
  35. * List of loaded components.
  36. *
  37. * @var object
  38. * @access protected
  39. */
  40. var $_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. var $_primary = array();
  49. /**
  50. * Settings for loaded components.
  51. *
  52. * @var array
  53. * @access private
  54. */
  55. var $__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. 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/998/MVC-Class-Access-Within-Components
  80. */
  81. 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/998/MVC-Class-Access-Within-Components
  100. * @deprecated See Component::triggerCallback()
  101. */
  102. function startup(&$controller) {
  103. $this->triggerCallback('startup', $controller);
  104. }
  105. /**
  106. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  107. * Controller::render()
  108. *
  109. * @param object $controller Controller with components to beforeRender
  110. * @return void
  111. * @access public
  112. * @deprecated See Component::triggerCallback()
  113. */
  114. function beforeRender(&$controller) {
  115. $this->triggerCallback('beforeRender', $controller);
  116. }
  117. /**
  118. * Called before Controller::redirect().
  119. *
  120. * @param object $controller Controller with components to beforeRedirect
  121. * @return void
  122. * @access public
  123. */
  124. function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
  125. $response = array();
  126. foreach ($this->_primary as $name) {
  127. $component =& $this->_loaded[$name];
  128. if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
  129. $resp = $component->beforeRedirect($controller, $url, $status, $exit);
  130. if ($resp === false) {
  131. return false;
  132. }
  133. $response[] = $resp;
  134. }
  135. }
  136. return $response;
  137. }
  138. /**
  139. * Called after Controller::render() and before the output is printed to the browser.
  140. *
  141. * @param object $controller Controller with components to shutdown
  142. * @return void
  143. * @access public
  144. * @deprecated See Component::triggerCallback()
  145. */
  146. function shutdown(&$controller) {
  147. $this->triggerCallback('shutdown', $controller);
  148. }
  149. /**
  150. * Trigger a callback on all primary components. Will fire $callback on all components
  151. * that have such a method. You can implement and fire custom callbacks in addition to the
  152. * standard ones.
  153. *
  154. * example use, from inside a controller:
  155. *
  156. * `$this->Component->triggerCallback('beforeFilter', $this);`
  157. *
  158. * will trigger the beforeFilter callback on all components that have implemented one. You
  159. * can trigger any method in this fashion.
  160. *
  161. * @param Controller $controller Controller instance
  162. * @param string $callback Callback to trigger.
  163. * @return void
  164. * @access public
  165. */
  166. function triggerCallback($callback, &$controller) {
  167. foreach ($this->_primary as $name) {
  168. $component =& $this->_loaded[$name];
  169. if (method_exists($component, $callback) && $component->enabled === true) {
  170. $component->{$callback}($controller);
  171. }
  172. }
  173. }
  174. /**
  175. * Loads components used by this component.
  176. *
  177. * @param object $object Object with a Components array
  178. * @param object $parent the parent of the current object
  179. * @return void
  180. * @access protected
  181. */
  182. function _loadComponents(&$object, $parent = null) {
  183. $base = $this->__controllerVars['base'];
  184. $normal = Set::normalize($object->components);
  185. foreach ((array)$normal as $component => $config) {
  186. $plugin = isset($this->__controllerVars['plugin']) ? $this->__controllerVars['plugin'] . '.' : null;
  187. list($plugin, $component) = pluginSplit($component, true, $plugin);
  188. $componentCn = $component . 'Component';
  189. if (!class_exists($componentCn)) {
  190. if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
  191. if (!App::import('Component', $component)) {
  192. $this->cakeError('missingComponentFile', array(array(
  193. 'className' => $this->__controllerVars['name'],
  194. 'component' => $component,
  195. 'file' => Inflector::underscore($component) . '.php',
  196. 'base' => $base,
  197. 'code' => 500
  198. )));
  199. return false;
  200. }
  201. }
  202. if (!class_exists($componentCn)) {
  203. $this->cakeError('missingComponentClass', array(array(
  204. 'className' => $this->__controllerVars['name'],
  205. 'component' => $component,
  206. 'file' => Inflector::underscore($component) . '.php',
  207. 'base' => $base,
  208. 'code' => 500
  209. )));
  210. return false;
  211. }
  212. }
  213. if ($parent === null) {
  214. $this->_primary[] = $component;
  215. }
  216. if (isset($this->_loaded[$component])) {
  217. $object->{$component} =& $this->_loaded[$component];
  218. if (!empty($config) && isset($this->__settings[$component])) {
  219. $this->__settings[$component] = array_merge($this->__settings[$component], $config);
  220. } elseif (!empty($config)) {
  221. $this->__settings[$component] = $config;
  222. }
  223. } else {
  224. if ($componentCn === 'SessionComponent') {
  225. $object->{$component} =& new $componentCn($base);
  226. } else {
  227. if (PHP5) {
  228. $object->{$component} = new $componentCn();
  229. } else {
  230. $object->{$component} =& new $componentCn();
  231. }
  232. }
  233. $object->{$component}->enabled = true;
  234. $this->_loaded[$component] =& $object->{$component};
  235. if (!empty($config)) {
  236. $this->__settings[$component] = $config;
  237. }
  238. if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
  239. $this->_loadComponents($object->{$component}, $component);
  240. }
  241. }
  242. }
  243. }
  244. }