PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Controller/Component.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 160 lines | 30 code | 13 blank | 117 comment | 4 complexity | 59803b64f8e522bedf6229ff3ec6ea1c MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * PHP 5
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Controller
  15. * @since CakePHP(tm) v 1.2
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('ComponentCollection', 'Controller');
  19. /**
  20. * Base class for an individual Component. Components provide reusable bits of
  21. * controller logic that can be composed into a controller. Components also
  22. * provide request life-cycle callbacks for injecting logic at specific points.
  23. *
  24. * ## Life cycle callbacks
  25. *
  26. * Components can provide several callbacks that are fired at various stages of the request
  27. * cycle. The available callbacks are:
  28. *
  29. * - `initialize()` - Fired before the controller's beforeFilter method.
  30. * - `startup()` - Fired after the controller's beforeFilter method.
  31. * - `beforeRender()` - Fired before the view + layout are rendered.
  32. * - `shutdown()` - Fired after the action is complete and the view has been rendered
  33. * but before Controller::afterFilter().
  34. * - `beforeRedirect()` - Fired before a redirect() is done.
  35. *
  36. * @package Cake.Controller
  37. * @link http://book.cakephp.org/view/993/Components
  38. * @see Controller::$components
  39. */
  40. class Component extends Object {
  41. /**
  42. * Component collection class used to lazy load components.
  43. *
  44. * @var ComponentCollection
  45. */
  46. protected $_Collection;
  47. /**
  48. * Settings for this Component
  49. *
  50. * @var array
  51. */
  52. public $settings = array();
  53. /**
  54. * Other Components this component uses.
  55. *
  56. * @var array
  57. */
  58. public $components = array();
  59. /**
  60. * A component lookup table used to lazy load component objects.
  61. *
  62. * @var array
  63. */
  64. protected $_componentMap = array();
  65. /**
  66. * Constructor
  67. *
  68. * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
  69. * @param array $settings Array of configuration settings.
  70. */
  71. public function __construct(ComponentCollection $collection, $settings = array()) {
  72. $this->_Collection = $collection;
  73. $this->settings = $settings;
  74. $this->_set($settings);
  75. if (!empty($this->components)) {
  76. $this->_componentMap = ComponentCollection::normalizeObjectArray($this->components);
  77. }
  78. }
  79. /**
  80. * Magic method for lazy loading $components.
  81. *
  82. * @param string $name Name of component to get.
  83. * @return mixed A Component object or null.
  84. */
  85. public function __get($name) {
  86. if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
  87. $settings = array_merge((array)$this->_componentMap[$name]['settings'], array('enabled' => false));
  88. $this->{$name} = $this->_Collection->load($this->_componentMap[$name]['class'], $settings);
  89. }
  90. if (isset($this->{$name})) {
  91. return $this->{$name};
  92. }
  93. }
  94. /**
  95. * Called before the Controller::beforeFilter().
  96. *
  97. * @param Controller $controller Controller with components to initialize
  98. * @return void
  99. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
  100. */
  101. public function initialize($controller) { }
  102. /**
  103. * Called after the Controller::beforeFilter() and before the controller action
  104. *
  105. * @param Controller $controller Controller with components to startup
  106. * @return void
  107. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::startup
  108. */
  109. public function startup($controller) { }
  110. /**
  111. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  112. * Controller::render()
  113. *
  114. * @param Controller $controller Controller with components to beforeRender
  115. * @return void
  116. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRender
  117. */
  118. public function beforeRender($controller) { }
  119. /**
  120. * Called after Controller::render() and before the output is printed to the browser.
  121. *
  122. * @param Controller $controller Controller with components to shutdown
  123. * @return void
  124. * @link @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::shutdown
  125. */
  126. public function shutdown($controller) { }
  127. /**
  128. * Called before Controller::redirect(). Allows you to replace the url that will
  129. * be redirected to with a new url. The return of this method can either be an array or a string.
  130. *
  131. * If the return is an array and contains a 'url' key. You may also supply the following:
  132. *
  133. * - `status` The status code for the redirect
  134. * - `exit` Whether or not the redirect should exit.
  135. *
  136. * If your response is a string or an array that does not contain a 'url' key it will
  137. * be used as the new url to redirect to.
  138. *
  139. * @param Controller $controller Controller with components to beforeRedirect
  140. * @param string|array $url Either the string or url array that is being redirected to.
  141. * @param integer $status The status code of the redirect
  142. * @param boolean $exit Will the script exit.
  143. * @return array|null Either an array or null.
  144. * @link @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRedirect
  145. */
  146. public function beforeRedirect($controller, $url, $status = null, $exit = true) {}
  147. }