eventManager($eventManager); $this->request = $request; $this->response = $response; $this->modelFactory('Table', [$this->tableLocator(), 'get']); foreach ($this->_validCellOptions as $var) { if (isset($cellOptions[$var])) { $this->{$var} = $cellOptions[$var]; } } if (!empty($cellOptions['cache'])) { $this->_cache = $cellOptions['cache']; } } /** * Render the cell. * * @param string|null $template Custom template name to render. If not provided (null), the last * value will be used. This value is automatically set by `CellTrait::cell()`. * @return string The rendered cell. * @throws \Cake\View\Exception\MissingCellViewException When a MissingTemplateException is raised during rendering. */ public function render($template = null) { if ($template !== null && strpos($template, '/') === false && strpos($template, '.') === false ) { $template = Inflector::underscore($template); } if ($template === null) { $template = $this->template; } $builder = $this->viewBuilder(); $builder->layout(false); $builder->template($template); $cache = []; if ($this->_cache) { $cache = $this->_cacheConfig($template); } $this->View = $this->getView(); $render = function () use ($template) { $className = substr(strrchr(get_class($this), "\\"), 1); $name = substr($className, 0, -4); $this->_view->viewPath('Cell' . DS . $name); try { return $this->_view->render($template); } catch (MissingTemplateException $e) { throw new MissingCellViewException(['file' => $template, 'name' => $name]); } }; if ($cache) { return $this->_view->cache(function () use ($render) { echo $render(); }, $cache); } return $render(); } /** * Generate the cache key to use for this cell. * * If the key is undefined, the cell class and template will be used. * * @param string $template The template being rendered. * @return array The cache configuration. */ protected function _cacheConfig($template) { if (empty($this->_cache)) { return []; } $key = 'cell_' . Inflector::underscore(get_class($this)) . '_' . $template; $key = str_replace('\\', '_', $key); $default = [ 'config' => 'default', 'key' => $key ]; if ($this->_cache === true) { return $default; } return $this->_cache + $default; } /** * Magic method. * * Starts the rendering process when Cell is echoed. * * *Note* This method will trigger an error when view rendering has a problem. * This is because PHP will not allow a __toString() method to throw an exception. * * @return string Rendered cell */ public function __toString() { try { return $this->render(); } catch (\Exception $e) { trigger_error('Could not render cell - ' . $e->getMessage(), E_USER_WARNING); return ''; } } /** * Debug info. * * @return array */ public function __debugInfo() { return [ 'plugin' => $this->plugin, 'template' => $this->template, 'viewClass' => $this->viewClass, 'request' => $this->request, 'response' => $this->response, ]; } }