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