Cell.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\Datasource\ModelAwareTrait;
  17. use Cake\Event\EventDispatcherTrait;
  18. use Cake\Event\EventManager;
  19. use Cake\Network\Request;
  20. use Cake\Network\Response;
  21. use Cake\Utility\Inflector;
  22. use Cake\View\Exception\MissingCellViewException;
  23. use Cake\View\Exception\MissingTemplateException;
  24. use Cake\View\ViewVarsTrait;
  25. /**
  26. * Cell base.
  27. *
  28. */
  29. abstract class Cell
  30. {
  31. use EventDispatcherTrait;
  32. use ModelAwareTrait;
  33. use ViewVarsTrait;
  34. /**
  35. * Instance of the View created during rendering. Won't be set until after
  36. * Cell::__toString() is called.
  37. *
  38. * @var \Cake\View\View
  39. */
  40. public $View;
  41. /**
  42. * Name of the template that will be rendered.
  43. * This property is inflected from the action name that was invoked.
  44. *
  45. * @var string
  46. */
  47. public $template;
  48. /**
  49. * Automatically set to the name of a plugin.
  50. *
  51. * @var string
  52. */
  53. public $plugin = null;
  54. /**
  55. * An instance of a Cake\Network\Request object that contains information about the current request.
  56. * This object contains all the information about a request and several methods for reading
  57. * additional information about the request.
  58. *
  59. * @var \Cake\Network\Request
  60. */
  61. public $request;
  62. /**
  63. * An instance of a Response object that contains information about the impending response
  64. *
  65. * @var \Cake\Network\Response
  66. */
  67. public $response;
  68. /**
  69. * The name of the View class this cell sends output to.
  70. *
  71. * @var string
  72. */
  73. public $viewClass = null;
  74. /**
  75. * The theme name that will be used to render.
  76. *
  77. * @var string
  78. */
  79. public $theme;
  80. /**
  81. * The helpers this cell uses.
  82. *
  83. * This property is copied automatically when using the CellTrait
  84. *
  85. * @var array
  86. */
  87. public $helpers = [];
  88. /**
  89. * These properties can be set directly on Cell and passed to the View as options.
  90. *
  91. * @var array
  92. * @see \Cake\View\View
  93. */
  94. protected $_validViewOptions = [
  95. 'viewVars', 'helpers', 'viewPath', 'plugin', 'theme'
  96. ];
  97. /**
  98. * List of valid options (constructor's fourth arguments)
  99. * Override this property in subclasses to whitelist
  100. * which options you want set as properties in your Cell.
  101. *
  102. * @var array
  103. */
  104. protected $_validCellOptions = [];
  105. /**
  106. * Caching setup.
  107. *
  108. * @var array|bool
  109. */
  110. protected $_cache = false;
  111. /**
  112. * Constructor.
  113. *
  114. * @param \Cake\Network\Request $request The request to use in the cell.
  115. * @param \Cake\Network\Response $response The response to use in the cell.
  116. * @param \Cake\Event\EventManager $eventManager The eventManager to bind events to.
  117. * @param array $cellOptions Cell options to apply.
  118. */
  119. public function __construct(
  120. Request $request = null,
  121. Response $response = null,
  122. EventManager $eventManager = null,
  123. array $cellOptions = []
  124. ) {
  125. $this->eventManager($eventManager);
  126. $this->request = $request;
  127. $this->response = $response;
  128. $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
  129. foreach ($this->_validCellOptions as $var) {
  130. if (isset($cellOptions[$var])) {
  131. $this->{$var} = $cellOptions[$var];
  132. }
  133. }
  134. if (!empty($cellOptions['cache'])) {
  135. $this->_cache = $cellOptions['cache'];
  136. }
  137. }
  138. /**
  139. * Render the cell.
  140. *
  141. * @param string|null $template Custom template name to render. If not provided (null), the last
  142. * value will be used. This value is automatically set by `CellTrait::cell()`.
  143. * @return string The rendered cell.
  144. * @throws \Cake\View\Exception\MissingCellViewException When a MissingTemplateException is raised during rendering.
  145. */
  146. public function render($template = null)
  147. {
  148. if ($template !== null &&
  149. strpos($template, '/') === false &&
  150. strpos($template, '.') === false
  151. ) {
  152. $template = Inflector::underscore($template);
  153. }
  154. if ($template === null) {
  155. $template = $this->template;
  156. }
  157. $this->View = null;
  158. $this->getView();
  159. $this->View->layout = false;
  160. $cache = [];
  161. if ($this->_cache) {
  162. $cache = $this->_cacheConfig($template);
  163. }
  164. $render = function () use ($template) {
  165. $className = explode('\\', get_class($this));
  166. $className = array_pop($className);
  167. $name = substr($className, 0, strrpos($className, 'Cell'));
  168. $this->View->subDir = 'Cell' . DS . $name;
  169. try {
  170. return $this->View->render($template);
  171. } catch (MissingTemplateException $e) {
  172. throw new MissingCellViewException(['file' => $template, 'name' => $name]);
  173. }
  174. };
  175. if ($cache) {
  176. return $this->View->cache(function () use ($render) {
  177. echo $render();
  178. }, $cache);
  179. }
  180. return $render();
  181. }
  182. /**
  183. * Generate the cache key to use for this cell.
  184. *
  185. * If the key is undefined, the cell class and template will be used.
  186. *
  187. * @param string $template The template being rendered.
  188. * @return array The cache configuration.
  189. */
  190. protected function _cacheConfig($template)
  191. {
  192. if (empty($this->_cache)) {
  193. return [];
  194. }
  195. $key = 'cell_' . Inflector::underscore(get_class($this)) . '_' . $template;
  196. $key = str_replace('\\', '_', $key);
  197. $default = [
  198. 'config' => 'default',
  199. 'key' => $key
  200. ];
  201. if ($this->_cache === true) {
  202. return $default;
  203. }
  204. return $this->_cache + $default;
  205. }
  206. /**
  207. * Magic method.
  208. *
  209. * Starts the rendering process when Cell is echoed.
  210. *
  211. * *Note* This method will trigger an error when view rendering has a problem.
  212. * This is because PHP will not allow a __toString() method to throw an exception.
  213. *
  214. * @return string Rendered cell
  215. */
  216. public function __toString()
  217. {
  218. try {
  219. return $this->render();
  220. } catch (\Exception $e) {
  221. trigger_error('Could not render cell - ' . $e->getMessage(), E_USER_WARNING);
  222. return '';
  223. }
  224. }
  225. /**
  226. * Debug info.
  227. *
  228. * @return array
  229. */
  230. public function __debugInfo()
  231. {
  232. return [
  233. 'plugin' => $this->plugin,
  234. 'template' => $this->template,
  235. 'viewClass' => $this->viewClass,
  236. 'request' => $this->request,
  237. 'response' => $this->response,
  238. ];
  239. }
  240. }