Debian Codegraph @

library/Toplevelview/Model/View.php

raw
1 <?php
2
3 namespace Icinga\Module\Toplevelview\Model;
4
5 use Icinga\Application\Benchmark;
6 use Icinga\Exception\InvalidPropertyException;
7 use Icinga\Exception\NotImplementedError;
8 use Icinga\Exception\ProgrammingError;
9 use Icinga\Module\Toplevelview\Tree\TLVTree;
10
11 /**
12 * View represents a single Top Level View.
13 * It contains the actual Tree and metadata.
14 */
15 class View
16 {
17 const FORMAT_YAML = 'yml';
18
19 /**
20 * Format of this View (e.g. 'yml')
21 * @var string
22 */
23 protected $format;
24 /**
25 * Name if this view (also used for the filename)
26 * @var string
27 */
28 protected $name;
29 /**
30 * Contains the parsed YAML (yaml_parse)
31 * @var string
32 */
33 protected $raw;
34 /**
35 * The TLVTree object for this View
36 * @var TLVTree
37 */
38 protected $tree;
39 /**
40 * Has this View been loaded
41 * @var bool
42 */
43 public $hasBeenLoaded = false;
44 /**
45 * Has this View been loaded from a session
46 * @var bool
47 */
48 public $hasBeenLoadedFromSession = false;
49 /**
50 * Content of the configuration file
51 * @var string
52 */
53 protected $text;
54 /**
55 * SHA1 checksum of the text
56 * @var string
57 */
58 protected $textChecksum;
59
60 public function __construct(string $name, string $format)
61 {
62 $this->name = $name;
63 $this->format = $format;
64 }
65
66 public function __clone()
67 {
68 $this->name = null;
69 $this->raw = null;
70 $this->tree = null;
71
72 $this->hasBeenLoaded = false;
73 $this->hasBeenLoadedFromSession = false;
74 }
75
76 /**
77 * getTree loads the Tree for this configuration
78 *
79 * @return TLVTree
80 */
81 public function getTree(): TLVTree
82 {
83 if ($this->tree === null) {
84 $this->ensureParsed();
85 $this->tree = $tree = TLVTree::fromArray($this->raw);
86 $tree->setViewName($this->getName());
87 $tree->setViewChecksum($this->getTextChecksum());
88 }
89 return $this->tree;
90 }
91
92 /**
93 * ensureParsed parses the Views YAML text.
94 *
95 * @throws InvalidPropertyException if the YAML config cannot be parsed
96 * @throws NotImplementedError if the format is unknown
97 */
98 protected function ensureParsed()
99 {
100 if ($this->raw === null) {
101 Benchmark::measure('Begin parsing YAML document');
102
103 $text = $this->getText();
104 if ($text === null) {
105 // new View
106 $this->raw = array();
107 } elseif ($this->format == self::FORMAT_YAML) {
108 // TODO: use stdClass instead of Array?
109 $this->raw = yaml_parse($text);
110 if (! is_array($this->raw)) {
111 throw new InvalidPropertyException('Could not parse YAML config!');
112 }
113 } else {
114 throw new NotImplementedError("Unknown format '%s'", $this->format);
115 }
116
117 Benchmark::measure('Finished parsing YAML document');
118 }
119 }
120
121 /**
122 * getMeta returns a value from the View's metadata.
123 * Metadata are root elemts in the YAML file that are not 'children'
124 * @throws ProgrammingError if you try to edit children here
125 */
126 public function getMeta($key)
127 {
128 $this->ensureParsed();
129 if ($key !== 'children' && array_key_exists($key, $this->raw)) {
130 return $this->raw[$key];
131 } else {
132 return null;
133 }
134 }
135
136 /**
137 * setMeta sets a given key's value
138 *
139 * @throws ProgrammingError if you try to edit children here
140 */
141 public function setMeta($key, $value)
142 {
143 if ($key === 'children') {
144 throw new ProgrammingError('You can not edit children here!');
145 }
146 $this->raw[$key] = $value;
147 return $this;
148 }
149
150 /**
151 * getMetaData returns all YAML root elements that are not 'childen',
152 * thus the View's metadata.
153 */
154 public function getMetaData()
155 {
156 $this->ensureParsed();
157 $data = array();
158 foreach ($this->raw as $key => $value) {
159 if ($key !== 'children') {
160 $data[$key] = $value;
161 }
162 }
163 return $data;
164 }
165
166 /**
167 * @return bool
168 */
169 public function hasBeenLoadedFromSession()
170 {
171 return $this->hasBeenLoadedFromSession;
172 }
173
174 /**
175 * @return bool
176 */
177 public function hasBeenLoaded()
178 {
179 return $this->hasBeenLoaded;
180 }
181
182 /**
183 * getText returns the Views text, which contains the full YAML data
184 * @return string
185 */
186 public function getText(): ?string
187 {
188 return $this->text;
189 }
190
191 /**
192 * getTextChecksum returns the textChecksum of this View
193 * @return string
194 */
195 public function getTextChecksum(): string
196 {
197 if ($this->textChecksum === null) {
198 $this->textChecksum = sha1($this->text);
199 }
200 return $this->textChecksum;
201 }
202
203 /**
204 * getFormat returns the View's format
205 */
206 public function getFormat(): string
207 {
208 return $this->format;
209 }
210
211 /**
212 * setText sets the text (YAML) for this View.
213 * Hint: This will reset textChecksum, raw, and tree
214 * @param $text
215 * @return $this
216 */
217 public function setText($text)
218 {
219 $this->text = $text;
220 $this->textChecksum = null;
221 $this->raw = null;
222 $this->tree = null;
223 return $this;
224 }
225
226 /**
227 * validateName validates the name of the view.
228 * This can be used to ensure the YAML files have proper/expected names
229 * @return bool
230 */
231 public function validateName(): bool
232 {
233 if (empty($this->name)) {
234 return false;
235 }
236 if (preg_match('/[!@#\$%^&*\/\\\()]/', $this->name)) {
237 return false;
238 }
239 return true;
240 }
241
242 /**
243 * getName returns the name of this View
244 * @return ?string
245 */
246 public function getName(): ?string
247 {
248 return $this->name;
249 }
250
251 /**
252 * setName sets the name for this View
253 * @param string $name
254 * @return $this
255 */
256 public function setName($name)
257 {
258 $this->name = $name;
259 return $this;
260 }
261 }