PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

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