PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/mariuz/firetube
PHP | 254 lines | 135 code | 13 blank | 106 comment | 33 complexity | 96b124feb2e66b3fbccd1a98b3f98544 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.controller
  17. * @since CakePHP(tm) v TBD
  18. * @version $Revision$
  19. * @modifiedby $LastChangedBy$
  20. * @lastmodified $Date$
  21. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  22. */
  23. /**
  24. * Handler for Controller::$components
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.libs.controller
  28. * @link http://book.cakephp.org/view/62/Components
  29. */
  30. class Component extends Object {
  31. /**
  32. * Contains various controller variable information (plugin, name, base).
  33. *
  34. * @var object
  35. * @access private
  36. */
  37. var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
  38. /**
  39. * List of loaded components.
  40. *
  41. * @var object
  42. * @access protected
  43. */
  44. var $_loaded = array();
  45. /**
  46. * List of components attached directly to the controller, which callbacks
  47. * should be executed on.
  48. *
  49. * @var object
  50. * @access protected
  51. */
  52. var $_primary = array();
  53. /**
  54. * Settings for loaded components.
  55. *
  56. * @var array
  57. * @access private
  58. **/
  59. var $__settings = array();
  60. /**
  61. * Used to initialize the components for current controller.
  62. *
  63. * @param object $controller Controller with components to load
  64. * @return void
  65. * @access public
  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. $this->_loadComponents($controller);
  76. }
  77. /**
  78. * Called before the Controller::beforeFilter().
  79. *
  80. * @param object $controller Controller with components to initialize
  81. * @return void
  82. * @access public
  83. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  84. */
  85. function initialize(&$controller) {
  86. foreach (array_keys($this->_loaded) as $name) {
  87. $component =& $this->_loaded[$name];
  88. if (method_exists($component,'initialize') && $component->enabled === true) {
  89. $settings = array();
  90. if (isset($this->__settings[$name])) {
  91. $settings = $this->__settings[$name];
  92. }
  93. $component->initialize($controller, $settings);
  94. }
  95. }
  96. }
  97. /**
  98. * Called after the Controller::beforeFilter() and before the controller action
  99. *
  100. * @param object $controller Controller with components to startup
  101. * @return void
  102. * @access public
  103. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  104. */
  105. function startup(&$controller) {
  106. foreach ($this->_primary as $name) {
  107. $component =& $this->_loaded[$name];
  108. if ($component->enabled === true && method_exists($component, 'startup')) {
  109. $component->startup($controller);
  110. }
  111. }
  112. }
  113. /**
  114. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  115. * Controller::render()
  116. *
  117. * @param object $controller Controller with components to beforeRender
  118. * @return void
  119. * @access public
  120. */
  121. function beforeRender(&$controller) {
  122. foreach ($this->_primary as $name) {
  123. $component =& $this->_loaded[$name];
  124. if ($component->enabled === true && method_exists($component,'beforeRender')) {
  125. $component->beforeRender($controller);
  126. }
  127. }
  128. }
  129. /**
  130. * Called before Controller::redirect().
  131. *
  132. * @param object $controller Controller with components to beforeRedirect
  133. * @return void
  134. * @access public
  135. */
  136. function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
  137. $response = array();
  138. foreach ($this->_primary as $name) {
  139. $component =& $this->_loaded[$name];
  140. if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
  141. $resp = $component->beforeRedirect($controller, $url, $status, $exit);
  142. if ($resp === false) {
  143. return false;
  144. }
  145. $response[] = $resp;
  146. }
  147. }
  148. return $response;
  149. }
  150. /**
  151. * Called after Controller::render() and before the output is printed to the browser.
  152. *
  153. * @param object $controller Controller with components to shutdown
  154. * @return void
  155. * @access public
  156. */
  157. function shutdown(&$controller) {
  158. foreach ($this->_primary as $name) {
  159. $component =& $this->_loaded[$name];
  160. if (method_exists($component,'shutdown') && $component->enabled === true) {
  161. $component->shutdown($controller);
  162. }
  163. }
  164. }
  165. /**
  166. * Loads components used by this component.
  167. *
  168. * @param object $object Object with a Components array
  169. * @param object $parent the parent of the current object
  170. * @return void
  171. * @access protected
  172. */
  173. function _loadComponents(&$object, $parent = null) {
  174. $base = $this->__controllerVars['base'];
  175. $normal = Set::normalize($object->components);
  176. if ($parent == null) {
  177. $normal = Set::merge(array('Session' => null), $normal);
  178. }
  179. foreach ((array)$normal as $component => $config) {
  180. $plugin = null;
  181. if (isset($this->__controllerVars['plugin'])) {
  182. $plugin = $this->__controllerVars['plugin'] . '.';
  183. }
  184. if (strpos($component, '.') !== false) {
  185. list($plugin, $component) = explode('.', $component);
  186. $plugin = $plugin . '.';
  187. }
  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. $object->{$component} =& new $componentCn();
  228. }
  229. $object->{$component}->enabled = true;
  230. $this->_loaded[$component] =& $object->{$component};
  231. if (!empty($config)) {
  232. $this->__settings[$component] = $config;
  233. }
  234. }
  235. if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
  236. $this->_loadComponents($object->{$component}, $component);
  237. }
  238. }
  239. }
  240. }
  241. ?>