PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/libs/controller/component.php

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