PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/yisraeldov/iPhPhoto
PHP | 255 lines | 135 code | 13 blank | 107 comment | 33 complexity | 5c44285ee88b4d559d977fb02eda02a3 MD5 | raw file
Possible License(s): Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id: component.php 8120 2009-03-19 20:25:10Z gwoo $ */
  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: 8120 $
  20. * @modifiedby $LastChangedBy: gwoo $
  21. * @lastmodified $Date: 2009-03-19 22:25:10 +0200 (ה', 19 מרץ 2009) $
  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. $this->_loadComponents($controller);
  77. }
  78. /**
  79. * Called before the Controller::beforeFilter().
  80. *
  81. * @param object $controller Controller with components to initialize
  82. * @return void
  83. * @access public
  84. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  85. */
  86. function initialize(&$controller) {
  87. foreach (array_keys($this->_loaded) as $name) {
  88. $component =& $this->_loaded[$name];
  89. if (method_exists($component,'initialize') && $component->enabled === true) {
  90. $settings = array();
  91. if (isset($this->__settings[$name])) {
  92. $settings = $this->__settings[$name];
  93. }
  94. $component->initialize($controller, $settings);
  95. }
  96. }
  97. }
  98. /**
  99. * Called after the Controller::beforeFilter() and before the controller action
  100. *
  101. * @param object $controller Controller with components to startup
  102. * @return void
  103. * @access public
  104. * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
  105. */
  106. function startup(&$controller) {
  107. foreach ($this->_primary as $name) {
  108. $component =& $this->_loaded[$name];
  109. if ($component->enabled === true && method_exists($component, 'startup')) {
  110. $component->startup($controller);
  111. }
  112. }
  113. }
  114. /**
  115. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  116. * Controller::render()
  117. *
  118. * @param object $controller Controller with components to beforeRender
  119. * @return void
  120. * @access public
  121. */
  122. function beforeRender(&$controller) {
  123. foreach ($this->_primary as $name) {
  124. $component =& $this->_loaded[$name];
  125. if ($component->enabled === true && method_exists($component,'beforeRender')) {
  126. $component->beforeRender($controller);
  127. }
  128. }
  129. }
  130. /**
  131. * Called before Controller::redirect().
  132. *
  133. * @param object $controller Controller with components to beforeRedirect
  134. * @return void
  135. * @access public
  136. */
  137. function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
  138. $response = array();
  139. foreach ($this->_primary as $name) {
  140. $component =& $this->_loaded[$name];
  141. if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
  142. $resp = $component->beforeRedirect($controller, $url, $status, $exit);
  143. if ($resp === false) {
  144. return false;
  145. }
  146. $response[] = $resp;
  147. }
  148. }
  149. return $response;
  150. }
  151. /**
  152. * Called after Controller::render() and before the output is printed to the browser.
  153. *
  154. * @param object $controller Controller with components to shutdown
  155. * @return void
  156. * @access public
  157. */
  158. function shutdown(&$controller) {
  159. foreach ($this->_primary as $name) {
  160. $component =& $this->_loaded[$name];
  161. if (method_exists($component,'shutdown') && $component->enabled === true) {
  162. $component->shutdown($controller);
  163. }
  164. }
  165. }
  166. /**
  167. * Loads components used by this component.
  168. *
  169. * @param object $object Object with a Components array
  170. * @param object $parent the parent of the current object
  171. * @return void
  172. * @access protected
  173. */
  174. function _loadComponents(&$object, $parent = null) {
  175. $base = $this->__controllerVars['base'];
  176. $normal = Set::normalize($object->components);
  177. if ($parent == null) {
  178. $normal = Set::merge(array('Session' => null), $normal);
  179. }
  180. foreach ((array)$normal as $component => $config) {
  181. $plugin = null;
  182. if (isset($this->__controllerVars['plugin'])) {
  183. $plugin = $this->__controllerVars['plugin'] . '.';
  184. }
  185. if (strpos($component, '.') !== false) {
  186. list($plugin, $component) = explode('.', $component);
  187. $plugin = $plugin . '.';
  188. }
  189. $componentCn = $component . 'Component';
  190. if (!class_exists($componentCn)) {
  191. if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
  192. if (!App::import('Component', $component)) {
  193. $this->cakeError('missingComponentFile', 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 (!class_exists($componentCn)) {
  204. $this->cakeError('missingComponentClass', array(array(
  205. 'className' => $this->__controllerVars['name'],
  206. 'component' => $component,
  207. 'file' => Inflector::underscore($component) . '.php',
  208. 'base' => $base,
  209. 'code' => 500
  210. )));
  211. return false;
  212. }
  213. }
  214. if ($parent === null) {
  215. $this->_primary[] = $component;
  216. }
  217. if (isset($this->_loaded[$component])) {
  218. $object->{$component} =& $this->_loaded[$component];
  219. if (!empty($config) && isset($this->__settings[$component])) {
  220. $this->__settings[$component] = array_merge($this->__settings[$component], $config);
  221. } elseif (!empty($config)) {
  222. $this->__settings[$component] = $config;
  223. }
  224. } else {
  225. if ($componentCn === 'SessionComponent') {
  226. $object->{$component} =& new $componentCn($base);
  227. } else {
  228. $object->{$component} =& new $componentCn();
  229. }
  230. $object->{$component}->enabled = true;
  231. $this->_loaded[$component] =& $object->{$component};
  232. if (!empty($config)) {
  233. $this->__settings[$component] = $config;
  234. }
  235. }
  236. if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
  237. $this->_loadComponents($object->{$component}, $component);
  238. }
  239. }
  240. }
  241. }
  242. ?>