CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • AjaxView
  • Cell
  • Helper
  • HelperRegistry
  • JsonView
  • SerializedView
  • StringTemplate
  • View
  • ViewBlock
  • ViewBuilder
  • XmlView

Traits

  • CellTrait
  • StringTemplateTrait
  • ViewVarsTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\View;
 16: 
 17: use BadMethodCallException;
 18: use Cake\Cache\Cache;
 19: use Cake\Datasource\ModelAwareTrait;
 20: use Cake\Event\EventDispatcherTrait;
 21: use Cake\Event\EventManager;
 22: use Cake\Http\Response;
 23: use Cake\Http\ServerRequest;
 24: use Cake\ORM\Locator\LocatorAwareTrait;
 25: use Cake\Utility\Inflector;
 26: use Cake\View\Exception\MissingCellViewException;
 27: use Cake\View\Exception\MissingTemplateException;
 28: use Error;
 29: use Exception;
 30: use ReflectionException;
 31: use ReflectionMethod;
 32: 
 33: /**
 34:  * Cell base.
 35:  */
 36: abstract class Cell
 37: {
 38: 
 39:     use EventDispatcherTrait;
 40:     use LocatorAwareTrait;
 41:     use ModelAwareTrait;
 42:     use ViewVarsTrait;
 43: 
 44:     /**
 45:      * Instance of the View created during rendering. Won't be set until after
 46:      * Cell::__toString()/render() is called.
 47:      *
 48:      * @var \Cake\View\View
 49:      */
 50:     protected $View;
 51: 
 52:     /**
 53:      * An instance of a Cake\Http\ServerRequest object that contains information about the current request.
 54:      * This object contains all the information about a request and several methods for reading
 55:      * additional information about the request.
 56:      *
 57:      * @var \Cake\Http\ServerRequest
 58:      */
 59:     protected $request;
 60: 
 61:     /**
 62:      * An instance of a Response object that contains information about the impending response
 63:      *
 64:      * @var \Cake\Http\Response
 65:      */
 66:     protected $response;
 67: 
 68:     /**
 69:      * The cell's action to invoke.
 70:      *
 71:      * @var string
 72:      */
 73:     protected $action;
 74: 
 75:     /**
 76:      * Arguments to pass to cell's action.
 77:      *
 78:      * @var array
 79:      */
 80:     protected $args = [];
 81: 
 82:     /**
 83:      * These properties can be set directly on Cell and passed to the View as options.
 84:      *
 85:      * @var array
 86:      * @see \Cake\View\View
 87:      * @deprecated 3.7.0 Use ViewBuilder::setOptions() or any one of it's setter methods instead.
 88:      */
 89:     protected $_validViewOptions = [
 90:         'viewPath'
 91:     ];
 92: 
 93:     /**
 94:      * List of valid options (constructor's fourth arguments)
 95:      * Override this property in subclasses to whitelist
 96:      * which options you want set as properties in your Cell.
 97:      *
 98:      * @var array
 99:      */
100:     protected $_validCellOptions = [];
101: 
102:     /**
103:      * Caching setup.
104:      *
105:      * @var array|bool
106:      */
107:     protected $_cache = false;
108: 
109:     /**
110:      * Constructor.
111:      *
112:      * @param \Cake\Http\ServerRequest|null $request The request to use in the cell.
113:      * @param \Cake\Http\Response|null $response The response to use in the cell.
114:      * @param \Cake\Event\EventManager|null $eventManager The eventManager to bind events to.
115:      * @param array $cellOptions Cell options to apply.
116:      */
117:     public function __construct(
118:         ServerRequest $request = null,
119:         Response $response = null,
120:         EventManager $eventManager = null,
121:         array $cellOptions = []
122:     ) {
123:         if ($eventManager !== null) {
124:             $this->setEventManager($eventManager);
125:         }
126:         $this->request = $request;
127:         $this->response = $response;
128:         $this->modelFactory('Table', [$this->getTableLocator(), 'get']);
129: 
130:         $this->_validCellOptions = array_merge(['action', 'args'], $this->_validCellOptions);
131:         foreach ($this->_validCellOptions as $var) {
132:             if (isset($cellOptions[$var])) {
133:                 $this->{$var} = $cellOptions[$var];
134:             }
135:         }
136:         if (!empty($cellOptions['cache'])) {
137:             $this->_cache = $cellOptions['cache'];
138:         }
139: 
140:         $this->initialize();
141:     }
142: 
143:     /**
144:      * Initialization hook method.
145:      *
146:      * Implement this method to avoid having to overwrite
147:      * the constructor and calling parent::__construct().
148:      *
149:      * @return void
150:      */
151:     public function initialize()
152:     {
153:     }
154: 
155:     /**
156:      * Render the cell.
157:      *
158:      * @param string|null $template Custom template name to render. If not provided (null), the last
159:      * value will be used. This value is automatically set by `CellTrait::cell()`.
160:      * @return string The rendered cell.
161:      * @throws \Cake\View\Exception\MissingCellViewException When a MissingTemplateException is raised during rendering.
162:      */
163:     public function render($template = null)
164:     {
165:         $cache = [];
166:         if ($this->_cache) {
167:             $cache = $this->_cacheConfig($this->action, $template);
168:         }
169: 
170:         $render = function () use ($template) {
171:             try {
172:                 $reflect = new ReflectionMethod($this, $this->action);
173:                 $reflect->invokeArgs($this, $this->args);
174:             } catch (ReflectionException $e) {
175:                 throw new BadMethodCallException(sprintf(
176:                     'Class %s does not have a "%s" method.',
177:                     get_class($this),
178:                     $this->action
179:                 ));
180:             }
181: 
182:             $builder = $this->viewBuilder()->setLayout(false);
183: 
184:             if ($template !== null &&
185:                 strpos($template, '/') === false &&
186:                 strpos($template, '.') === false
187:             ) {
188:                 $template = Inflector::underscore($template);
189:             }
190:             if ($template !== null) {
191:                 $builder->setTemplate($template);
192:             }
193: 
194:             $className = get_class($this);
195:             $namePrefix = '\View\Cell\\';
196:             $name = substr($className, strpos($className, $namePrefix) + strlen($namePrefix));
197:             $name = substr($name, 0, -4);
198:             if (!$builder->getTemplatePath()) {
199:                 $builder->setTemplatePath('Cell' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $name));
200:             }
201:             $template = $builder->getTemplate();
202: 
203:             $this->View = $this->createView();
204:             try {
205:                 return $this->View->render($template);
206:             } catch (MissingTemplateException $e) {
207:                 throw new MissingCellViewException(['file' => $template, 'name' => $name], null, $e);
208:             }
209:         };
210: 
211:         if ($cache) {
212:             return Cache::remember($cache['key'], $render, $cache['config']);
213:         }
214: 
215:         return $render();
216:     }
217: 
218:     /**
219:      * Generate the cache key to use for this cell.
220:      *
221:      * If the key is undefined, the cell class and action name will be used.
222:      *
223:      * @param string $action The action invoked.
224:      * @param string|null $template The name of the template to be rendered.
225:      * @return array The cache configuration.
226:      */
227:     protected function _cacheConfig($action, $template = null)
228:     {
229:         if (empty($this->_cache)) {
230:             return [];
231:         }
232:         $template = $template ?: 'default';
233:         $key = 'cell_' . Inflector::underscore(get_class($this)) . '_' . $action . '_' . $template;
234:         $key = str_replace('\\', '_', $key);
235:         $default = [
236:             'config' => 'default',
237:             'key' => $key
238:         ];
239:         if ($this->_cache === true) {
240:             return $default;
241:         }
242: 
243:         return $this->_cache + $default;
244:     }
245: 
246:     /**
247:      * Magic method.
248:      *
249:      * Starts the rendering process when Cell is echoed.
250:      *
251:      * *Note* This method will trigger an error when view rendering has a problem.
252:      * This is because PHP will not allow a __toString() method to throw an exception.
253:      *
254:      * @return string Rendered cell
255:      * @throws \Error Include error details for PHP 7 fatal errors.
256:      */
257:     public function __toString()
258:     {
259:         try {
260:             return $this->render();
261:         } catch (Exception $e) {
262:             trigger_error(sprintf('Could not render cell - %s [%s, line %d]', $e->getMessage(), $e->getFile(), $e->getLine()), E_USER_WARNING);
263: 
264:             return '';
265:         } catch (Error $e) {
266:             throw new Error(sprintf('Could not render cell - %s [%s, line %d]', $e->getMessage(), $e->getFile(), $e->getLine()));
267:         }
268:     }
269: 
270:     /**
271:      * Magic accessor for removed properties.
272:      *
273:      * @param string $name Property name
274:      * @return mixed
275:      */
276:     public function __get($name)
277:     {
278:         $deprecated = [
279:             'template' => 'getTemplate',
280:             'plugin' => 'getPlugin',
281:             'helpers' => 'getHelpers',
282:         ];
283:         if (isset($deprecated[$name])) {
284:             $method = $deprecated[$name];
285:             deprecationWarning(sprintf(
286:                 'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
287:                 $name,
288:                 $method
289:             ));
290: 
291:             return $this->viewBuilder()->{$method}();
292:         }
293: 
294:         $protected = [
295:             'action',
296:             'args',
297:             'request',
298:             'response',
299:             'View',
300:         ];
301:         if (in_array($name, $protected, true)) {
302:             deprecationWarning(sprintf(
303:                 'Cell::$%s is now protected and shouldn\'t be accessed from outside a child class.',
304:                 $name
305:             ));
306:         }
307: 
308:         return $this->{$name};
309:     }
310: 
311:     /**
312:      * Magic setter for removed properties.
313:      *
314:      * @param string $name Property name.
315:      * @param mixed $value Value to set.
316:      * @return void
317:      */
318:     public function __set($name, $value)
319:     {
320:         $deprecated = [
321:             'template' => 'setTemplate',
322:             'plugin' => 'setPlugin',
323:             'helpers' => 'setHelpers',
324:         ];
325:         if (isset($deprecated[$name])) {
326:             $method = $deprecated[$name];
327:             deprecationWarning(sprintf(
328:                 'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
329:                 $name,
330:                 $method
331:             ));
332:             $this->viewBuilder()->{$method}($value);
333: 
334:             return;
335:         }
336: 
337:         $protected = [
338:             'action',
339:             'args',
340:             'request',
341:             'response',
342:             'View',
343:         ];
344:         if (in_array($name, $protected, true)) {
345:             deprecationWarning(sprintf(
346:                 'Cell::$%s is now protected and shouldn\'t be accessed from outside a child class.',
347:                 $name
348:             ));
349:         }
350: 
351:         $this->{$name} = $value;
352:     }
353: 
354:     /**
355:      * Debug info.
356:      *
357:      * @return array
358:      */
359:     public function __debugInfo()
360:     {
361:         return [
362:             'action' => $this->action,
363:             'args' => $this->args,
364:             'request' => $this->request,
365:             'response' => $this->response,
366:             'viewBuilder' => $this->viewBuilder(),
367:         ];
368:     }
369: }
370: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs