Cell.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Core\App;
  17. use Cake\Event\EventManager;
  18. use Cake\Model\ModelAwareTrait;
  19. use Cake\Network\Request;
  20. use Cake\Network\Response;
  21. use Cake\Utility\Inflector;
  22. use Cake\View\ViewVarsTrait;
  23. /**
  24. * Cell base.
  25. *
  26. */
  27. abstract class Cell {
  28. use ModelAwareTrait;
  29. use ViewVarsTrait;
  30. /**
  31. * Instance of the View created during rendering. Won't be set until after
  32. * Cell::__toString() is called.
  33. *
  34. * @var \Cake\View\View
  35. */
  36. public $View;
  37. /**
  38. * Name of the action that was invoked.
  39. *
  40. * Action name will be inflected to get the template name when rendering.
  41. *
  42. * @var string
  43. */
  44. public $action;
  45. /**
  46. * Automatically set to the name of a plugin.
  47. *
  48. * @var string
  49. */
  50. public $plugin = null;
  51. /**
  52. * An instance of a Cake\Network\Request object that contains information about the current request.
  53. * This object contains all the information about a request and several methods for reading
  54. * additional information about the request.
  55. *
  56. * @var \Cake\Network\Request
  57. */
  58. public $request;
  59. /**
  60. * An instance of a Response object that contains information about the impending response
  61. *
  62. * @var \Cake\Network\Response
  63. */
  64. public $response;
  65. /**
  66. * The name of the View class this cell sends output to.
  67. *
  68. * @var string
  69. */
  70. public $viewClass = 'Cake\View\View';
  71. /**
  72. * Instance of the Cake\Event\EventManager this cell is using
  73. * to dispatch inner events.
  74. *
  75. * @var \Cake\Event\EventManager
  76. */
  77. protected $_eventManager = null;
  78. /**
  79. * These properties can be set directly on Cell and passed to the View as options.
  80. *
  81. * @var array
  82. * @see \Cake\View\View
  83. */
  84. protected $_validViewOptions = [
  85. 'viewVars', 'helpers', 'viewPath', 'plugin',
  86. ];
  87. /**
  88. * List of valid options (constructor's fourth arguments)
  89. * Override this property in subclasses to whitelist
  90. * which options you want set as properties in your Cell.
  91. *
  92. * @var array
  93. */
  94. protected $_validCellOptions = [];
  95. /**
  96. * Constructor.
  97. *
  98. * @param \Cake\Network\Request $request
  99. * @param \Cake\Network\Response $response
  100. * @param \Cake\Event\EventManager $eventManager
  101. * @param array $cellOptions
  102. */
  103. public function __construct(Request $request = null, Response $response = null,
  104. EventManager $eventManager = null, array $cellOptions = []) {
  105. $this->_eventManager = $eventManager;
  106. $this->request = $request;
  107. $this->response = $response;
  108. $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
  109. foreach ($this->_validCellOptions as $var) {
  110. if (isset($cellOptions[$var])) {
  111. $this->{$var} = $cellOptions[$var];
  112. }
  113. }
  114. }
  115. /**
  116. * Render the cell.
  117. *
  118. * @param string $action Custom template name to render. If not provided (null), the last
  119. * value will be used. This value is automatically set by `CellTrait::cell()`.
  120. * @return void
  121. */
  122. public function render($action = null) {
  123. if ($action !== null) {
  124. $this->action = $action;
  125. }
  126. $this->View = $this->createView();
  127. $this->View->layout = false;
  128. $className = explode('\\', get_class($this));
  129. $className = array_pop($className);
  130. $this->View->subDir = 'Cell' . DS . substr($className, 0, strpos($className, 'Cell'));
  131. return $this->View->render(Inflector::underscore($this->action));
  132. }
  133. /**
  134. * Magic method.
  135. *
  136. * Starts the rendering process when Cell is echoed.
  137. *
  138. * @return string Rendered cell
  139. */
  140. public function __toString() {
  141. return $this->render();
  142. }
  143. /**
  144. * Debug info.
  145. *
  146. * @return void
  147. */
  148. public function __debugInfo() {
  149. return [
  150. 'plugin' => $this->plugin,
  151. 'action' => $this->action,
  152. 'viewClass' => $this->viewClass,
  153. 'request' => $this->request,
  154. 'response' => $this->response,
  155. ];
  156. }
  157. /**
  158. * Returns the Cake\Event\EventManager manager instance for this cell.
  159. *
  160. * You can use this instance to register any new listeners or callbacks to the
  161. * cell events, or create your own events and trigger them at will.
  162. *
  163. * @return \Cake\Event\EventManager
  164. */
  165. public function getEventManager() {
  166. if (empty($this->_eventManager)) {
  167. $this->_eventManager = new EventManager();
  168. }
  169. return $this->_eventManager;
  170. }
  171. }