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: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright (c), Cake Software Foundation, Inc. (https://cakefoundation.org)
10: * @link https://cakephp.org CakePHP(tm) Project
11: * @since 3.0.0
12: * @license https://opensource.org/licenses/mit-license.php MIT License
13: */
14: namespace Cake\View;
15:
16: use Cake\Event\EventDispatcherInterface;
17:
18: /**
19: * Provides the set() method for collecting template context.
20: *
21: * Once collected context data can be passed to another object.
22: * This is done in Controller, TemplateTask and View for example.
23: *
24: * @property array $_validViewOptions
25: */
26: trait ViewVarsTrait
27: {
28:
29: /**
30: * The name of default View class.
31: *
32: * @var string|null
33: * @deprecated 3.1.0 Use `$this->viewBuilder()->getClassName()`/`$this->viewBuilder()->setClassName()` instead.
34: */
35: public $viewClass;
36:
37: /**
38: * Variables for the view
39: *
40: * @var array
41: * @deprecated 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.
42: */
43: public $viewVars = [];
44:
45: /**
46: * The view builder instance being used.
47: *
48: * @var \Cake\View\ViewBuilder
49: */
50: protected $_viewBuilder;
51:
52: /**
53: * Get the view builder being used.
54: *
55: * @return \Cake\View\ViewBuilder
56: */
57: public function viewBuilder()
58: {
59: if (!isset($this->_viewBuilder)) {
60: $this->_viewBuilder = new ViewBuilder();
61: }
62:
63: return $this->_viewBuilder;
64: }
65:
66: /**
67: * Constructs the view class instance based on the current configuration.
68: *
69: * @param string|null $viewClass Optional namespaced class name of the View class to instantiate.
70: * @return \Cake\View\View
71: * @throws \Cake\View\Exception\MissingViewException If view class was not found.
72: */
73: public function createView($viewClass = null)
74: {
75: $builder = $this->viewBuilder();
76: if ($viewClass === null && $builder->getClassName() === null) {
77: $builder->setClassName($this->viewClass);
78: $this->viewClass = null;
79: }
80: if ($viewClass) {
81: $builder->setClassName($viewClass);
82: }
83:
84: $validViewOptions = isset($this->_validViewOptions) ? $this->_validViewOptions : [];
85: $viewOptions = [];
86: foreach ($validViewOptions as $option) {
87: if (property_exists($this, $option)) {
88: $viewOptions[$option] = $this->{$option};
89: }
90: }
91:
92: $deprecatedOptions = [
93: 'layout' => 'setLayout',
94: 'view' => 'setTemplate',
95: 'theme' => 'setTheme',
96: 'autoLayout' => 'enableAutoLayout',
97: 'viewPath' => 'setTemplatePath',
98: 'layoutPath' => 'setLayoutPath',
99: ];
100: foreach ($deprecatedOptions as $old => $new) {
101: if (property_exists($this, $old)) {
102: $builder->{$new}($this->{$old});
103: $message = sprintf(
104: 'Property $%s is deprecated. Use $this->viewBuilder()->%s() instead in beforeRender().',
105: $old,
106: $new
107: );
108: deprecationWarning($message);
109: }
110: }
111:
112: foreach (['name', 'helpers', 'plugin'] as $prop) {
113: if (isset($this->{$prop})) {
114: $method = 'set' . ucfirst($prop);
115: $builder->{$method}($this->{$prop});
116: }
117: }
118: $builder->setOptions($viewOptions);
119:
120: return $builder->build(
121: $this->viewVars,
122: isset($this->request) ? $this->request : null,
123: isset($this->response) ? $this->response : null,
124: $this instanceof EventDispatcherInterface ? $this->getEventManager() : null
125: );
126: }
127:
128: /**
129: * Saves a variable or an associative array of variables for use inside a template.
130: *
131: * @param string|array $name A string or an array of data.
132: * @param mixed $value Value in case $name is a string (which then works as the key).
133: * Unused if $name is an associative array, otherwise serves as the values to $name's keys.
134: * @return $this
135: */
136: public function set($name, $value = null)
137: {
138: if (is_array($name)) {
139: if (is_array($value)) {
140: $data = array_combine($name, $value);
141: } else {
142: $data = $name;
143: }
144: } else {
145: $data = [$name => $value];
146: }
147: $this->viewVars = $data + $this->viewVars;
148:
149: return $this;
150: }
151:
152: /**
153: * Get/Set valid view options in the object's _validViewOptions property. The property is
154: * created as an empty array if it is not set. If called without any parameters it will
155: * return the current list of valid view options. See `createView()`.
156: *
157: * @param string|array|null $options string or array of string to be appended to _validViewOptions.
158: * @param bool $merge Whether to merge with or override existing valid View options.
159: * Defaults to `true`.
160: * @return array The updated view options as an array.
161: * @deprecated 3.7.0 Use ViewBuilder::setOptions() or any one of it's setter methods instead.
162: */
163: public function viewOptions($options = null, $merge = true)
164: {
165: deprecationWarning(
166: 'ViewVarsTrait::viewOptions() is deprecated, used ViewBuilder::setOptions() instead.'
167: );
168:
169: if (!isset($this->_validViewOptions)) {
170: $this->_validViewOptions = [];
171: }
172:
173: if ($options === null) {
174: return $this->_validViewOptions;
175: }
176:
177: if (!$merge) {
178: return $this->_validViewOptions = (array)$options;
179: }
180:
181: return $this->_validViewOptions = array_merge($this->_validViewOptions, (array)$options);
182: }
183: }
184: