PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/samanz/cakecart
PHP | 232 lines | 131 code | 9 blank | 92 comment | 34 complexity | 0fca0f770be23a1e77a0420ace97c3b3 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id: component.php 7296 2008-06-27 09:09:03Z 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.
  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: 7296 $
  22. * @modifiedby $LastChangedBy: gwoo $
  23. * @lastmodified $Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 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. * @access public
  59. */
  60. function init(&$controller) {
  61. if ($controller->components !== false && is_array($controller->components)) {
  62. $this->__controllerVars = array(
  63. 'plugin' => $controller->plugin, 'name' => $controller->name, 'base' => $controller->base
  64. );
  65. if (!in_array('Session', $controller->components)) {
  66. array_unshift($controller->components, 'Session');
  67. }
  68. $this->_loadComponents($controller);
  69. }
  70. }
  71. /**
  72. * Called before the Controller::beforeFilter()
  73. *
  74. * @param object $controller Controller with components to initialize
  75. * @access public
  76. */
  77. function initialize(&$controller) {
  78. foreach (array_keys($this->__loaded) as $name) {
  79. $component =& $this->__loaded[$name];
  80. if (method_exists($component,'initialize') && $component->enabled === true) {
  81. $settings = array();
  82. if (isset($this->__settings[$name])) {
  83. $settings = $this->__settings[$name];
  84. }
  85. $component->initialize($controller, $settings);
  86. }
  87. }
  88. }
  89. /**
  90. * Called after the Controller::beforeFilter() and before the controller action
  91. *
  92. * @param object $controller Controller with components to startup
  93. * @access public
  94. */
  95. function startup(&$controller) {
  96. foreach (array_keys($this->__loaded) as $name) {
  97. $component =& $this->__loaded[$name];
  98. if (method_exists($component,'startup') && $component->enabled === true) {
  99. $component->startup($controller);
  100. }
  101. }
  102. }
  103. /**
  104. * Called after the Controller::beforeRender(), after the view class is loaded, and before the Controller::render()
  105. *
  106. * @param object $controller Controller with components to beforeRender
  107. * @access public
  108. */
  109. function beforeRender(&$controller) {
  110. foreach (array_keys($this->__loaded) as $name) {
  111. $component =& $this->__loaded[$name];
  112. if (method_exists($component,'beforeRender') && $component->enabled === true) {
  113. $component->beforeRender($controller);
  114. }
  115. }
  116. }
  117. /**
  118. * Called before Controller::redirect();
  119. *
  120. * @param object $controller Controller with components to beforeRedirect
  121. * @access public
  122. */
  123. function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
  124. $response = array();
  125. foreach (array_keys($this->__loaded) as $name) {
  126. $component =& $this->__loaded[$name];
  127. if (method_exists($component,'beforeRedirect') && $component->enabled === true) {
  128. $resp = $component->beforeRedirect($controller, $url, $status, $exit);
  129. if ($resp === false) {
  130. return false;
  131. }
  132. $response[] = $resp;
  133. }
  134. }
  135. return $response;
  136. }
  137. /**
  138. * Called after Controller::render() and before the output is printed to the browser
  139. *
  140. * @param object $controller Controller with components to shutdown
  141. * @access public
  142. */
  143. function shutdown(&$controller) {
  144. foreach (array_keys($this->__loaded) as $name) {
  145. $component =& $this->__loaded[$name];
  146. if (method_exists($component,'shutdown') && $component->enabled === true) {
  147. $component->shutdown($controller);
  148. }
  149. }
  150. }
  151. /**
  152. * Load components used by this component.
  153. *
  154. * @param object $object Object with a Components array
  155. * @param object $parent the parent of the current object
  156. * @return void
  157. * @access protected
  158. */
  159. function _loadComponents(&$object, $parent = null) {
  160. $components = $object->components;
  161. $base = $this->__controllerVars['base'];
  162. if (is_array($object->components)) {
  163. $normal = Set::normalize($object->components);
  164. foreach ($normal as $component => $config) {
  165. $parts = preg_split('/\/|\./', $component);
  166. if (count($parts) === 1) {
  167. $plugin = $this->__controllerVars['plugin'] . '.';
  168. } else {
  169. $plugin = Inflector::underscore($parts['0']) . '.';
  170. $component = array_pop($parts);
  171. }
  172. $componentCn = $component . 'Component';
  173. if (!class_exists($componentCn)) {
  174. if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
  175. if (!App::import('Component', $component)) {
  176. $this->cakeError('missingComponentFile', array(array(
  177. 'className' => $this->__controllerVars['name'],
  178. 'component' => $component,
  179. 'file' => Inflector::underscore($component) . '.php',
  180. 'base' => $base,
  181. 'code' => 500
  182. )));
  183. return false;
  184. }
  185. }
  186. if (!class_exists($componentCn)) {
  187. $this->cakeError('missingComponentClass', array(array(
  188. 'className' => $this->__controllerVars['name'],
  189. 'component' => $component,
  190. 'file' => Inflector::underscore($component) . '.php',
  191. 'base' => $base,
  192. 'code' => 500
  193. )));
  194. return false;
  195. }
  196. }
  197. if (isset($this->__loaded[$component])) {
  198. $object->{$component} =& $this->__loaded[$component];
  199. if (!empty($config) && isset($this->__settings[$component])) {
  200. $this->__settings[$component] = array_merge($this->__settings[$component], $config);
  201. } elseif (!empty($config)) {
  202. $this->__settings[$component] = $config;
  203. }
  204. } else {
  205. if ($componentCn == 'SessionComponent') {
  206. $object->{$component} =& new $componentCn($base);
  207. } else {
  208. $object->{$component} =& new $componentCn();
  209. }
  210. $object->{$component}->enabled = true;
  211. $this->__loaded[$component] =& $object->{$component};
  212. if (!empty($config)) {
  213. $this->__settings[$component] = $config;
  214. }
  215. }
  216. if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
  217. $this->_loadComponents($object->{$component}, $component);
  218. }
  219. }
  220. }
  221. }
  222. }
  223. ?>