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