-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ViewVarsTrait.php
96 lines (87 loc) · 2.87 KB
/
ViewVarsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c), Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View;
use Cake\Event\EventDispatcherInterface;
/**
* Provides the set() method for collecting template context.
*
* Once collected context data can be passed to another object.
* This is done in Controller, TemplateTask and View for example.
*/
trait ViewVarsTrait
{
/**
* The view builder instance being used.
*
* @var \Cake\View\ViewBuilder|null
*/
protected ?ViewBuilder $_viewBuilder = null;
/**
* Get the view builder being used.
*
* @return \Cake\View\ViewBuilder
*/
public function viewBuilder(): ViewBuilder
{
return $this->_viewBuilder ??= new ViewBuilder();
}
/**
* Constructs the view class instance based on the current configuration.
*
* @param string|null $viewClass Optional namespaced class name of the View class to instantiate.
* @return \Cake\View\View
* @throws \Cake\View\Exception\MissingViewException If view class was not found.
*/
public function createView(?string $viewClass = null): View
{
$builder = $this->viewBuilder();
if ($viewClass) {
$builder->setClassName($viewClass);
}
foreach (['name', 'plugin'] as $prop) {
if (isset($this->{$prop})) {
$method = 'set' . ucfirst($prop);
$builder->{$method}($this->{$prop});
}
}
return $builder->build(
$this->request ?? null,
$this->response ?? null,
$this instanceof EventDispatcherInterface ? $this->getEventManager() : null
);
}
/**
* Saves a variable or an associative array of variables for use inside a template.
*
* @param array|string $name A string or an array of data.
* @param mixed $value Value in case $name is a string (which then works as the key).
* Unused if $name is an associative array, otherwise serves as the values to $name's keys.
* @return $this
*/
public function set(array|string $name, mixed $value = null)
{
if (is_array($name)) {
if (is_array($value)) {
$data = array_combine($name, $value);
} else {
$data = $name;
}
} else {
$data = [$name => $value];
}
$this->viewBuilder()->setVars($data);
return $this;
}
}