-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractView.hh
164 lines (136 loc) · 3.26 KB
/
AbstractView.hh
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\View;
use Titon\Cache\Storage;
use Titon\Event\EmitsEvents;
use Titon\Event\Listener;
use Titon\Event\Subject;
use Titon\Utility\Inflect;
use Titon\Utility\Registry;
use Titon\View\Exception\MissingHelperException;
use Titon\View\Helper;
/**
* Defines shared functionality for view managers.
*
* @package Titon\View\View
*/
abstract class AbstractView implements View, Subject {
use EmitsEvents;
/**
* Variable data for templates.
*
* @var \Titon\View\DataMap
*/
protected DataMap $data = Map {};
/**
* List of helpers.
*
* @var \Titon\View\HelperMap
*/
protected HelperMap $helpers = Map {};
/**
* Template locator instance.
*
* @var \Titon\View\Locator
*/
protected Locator $locator;
/**
* Storage engine.
*
* @var \Titon\Cache\Storage
*/
protected ?Storage $storage;
/**
* Set the template locator through the constructor.
*
* @param \Titon\View\Locator $locator
*/
public function __construct(Locator $locator) {
$this->locator = $locator;
}
/**
* {@inheritdoc}
*/
public function getHelper(string $key): Helper {
if ($this->helpers->contains($key)) {
return $this->helpers[$key];
}
throw new MissingHelperException(sprintf('Helper %s does not exist', $key));
}
/**
* {@inheritdoc}
*/
public function getHelpers(): HelperMap {
return $this->helpers;
}
/**
* {@inheritdoc}
*/
public function getLocator(): Locator {
return $this->locator;
}
/**
* Return the storage engine.
*
* @return \Titon\Cache\Storage
*/
public function getStorage(): ?Storage {
return $this->storage;
}
/**
* {@inheritdoc}
*/
public function getVariable(string $key): mixed {
return $this->getVariables()->get($key);
}
/**
* {@inheritdoc}
*/
public function getVariables(): DataMap {
return $this->data;
}
/**
* {@inheritdoc}
*/
public function setHelper(string $key, Helper $helper): this {
$helper->setView($this);
$this->helpers[$key] = $helper;
if ($helper instanceof Listener) {
$this->on($helper);
}
$this->setVariable($key, $helper);
// Store so helpers can use helpers
Registry::set($helper);
return $this;
}
/**
* Set the storage engine to cache views.
*
* @param \Titon\Cache\Storage $storage
* @return $this
*/
public function setStorage(Storage $storage): this {
$this->storage = $storage;
return $this;
}
/**
* {@inheritdoc}
*/
public function setVariable(string $key, mixed $value): this {
$this->data[Inflect::variable($key)] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function setVariables(DataMap $data): this {
foreach ($data as $key => $value) {
$this->setVariable($key, $value);
}
return $this;
}
}