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.1.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\View;
16:
17: use Cake\Event\EventManager;
18: use Cake\Http\Response;
19: use Cake\Http\ServerRequest;
20: use RuntimeException;
21:
22: /**
23: * Parent class for view classes generating serialized outputs like JsonView and XmlView.
24: */
25: abstract class SerializedView extends View
26: {
27: /**
28: * Response type.
29: *
30: * @var string
31: */
32: protected $_responseType;
33:
34: /**
35: * Constructor
36: *
37: * @param \Cake\Http\ServerRequest|null $request Request instance.
38: * @param \Cake\Http\Response|null $response Response instance.
39: * @param \Cake\Event\EventManager|null $eventManager EventManager instance.
40: * @param array $viewOptions An array of view options
41: */
42: public function __construct(
43: ServerRequest $request = null,
44: Response $response = null,
45: EventManager $eventManager = null,
46: array $viewOptions = []
47: ) {
48: if ($response && $response instanceof Response) {
49: $response = $response->withType($this->_responseType);
50: }
51: parent::__construct($request, $response, $eventManager, $viewOptions);
52: }
53:
54: /**
55: * Load helpers only if serialization is disabled.
56: *
57: * @return $this
58: */
59: public function loadHelpers()
60: {
61: if (empty($this->viewVars['_serialize'])) {
62: parent::loadHelpers();
63: }
64:
65: return $this;
66: }
67:
68: /**
69: * Serialize view vars.
70: *
71: * @param array|string $serialize The name(s) of the view variable(s) that
72: * need(s) to be serialized
73: * @return string The serialized data
74: */
75: abstract protected function _serialize($serialize);
76:
77: /**
78: * Render view template or return serialized data.
79: *
80: * ### Special parameters
81: * `_serialize` To convert a set of view variables into a serialized form.
82: * Its value can be a string for single variable name or array for multiple
83: * names. If true all view variables will be serialized. If unset normal
84: * view template will be rendered.
85: *
86: * @param string|bool|null $view The view being rendered.
87: * @param string|null $layout The layout being rendered.
88: * @return string|null The rendered view.
89: */
90: public function render($view = null, $layout = null)
91: {
92: $serialize = false;
93: if (isset($this->viewVars['_serialize'])) {
94: $serialize = $this->viewVars['_serialize'];
95: }
96:
97: if ($serialize !== false) {
98: $result = $this->_serialize($serialize);
99: if ($result === false) {
100: throw new RuntimeException('Serialization of View data failed.');
101: }
102:
103: return (string)$result;
104: }
105: if ($view !== false && $this->_getViewFileName($view)) {
106: return parent::render($view, false);
107: }
108: }
109: }
110: