-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathCmsCompoundObject.php
493 lines (420 loc) · 13.9 KB
/
CmsCompoundObject.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php namespace Cms\Classes;
use Ini;
use Lang;
use Cache;
use Config;
use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Components\ViewBag;
use System\Twig\Extension as SystemTwigExtension;
use October\Rain\Halcyon\Processors\SectionParser;
use Twig_Source;
use Twig_Environment;
use ApplicationException;
/**
* This is a base class for CMS objects that have multiple sections - pages, partials and layouts.
* The class implements functionality for the compound object file parsing. It also provides a way
* to access parameters defined in the INI settings section as the object properties.
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class CmsCompoundObject extends CmsObject
{
/**
* @var array Initialized components defined in the template file.
*/
public $components = [];
/**
* @var array INI settings defined in the template file. Not to be confused
* with the attribute called settings. In this array, components are bumped
* to their own array inside the 'components' key.
*/
public $settings = [
'components' => []
];
/**
* @var array Contains the view bag properties.
* This property is used by the page editor internally.
*/
public $viewBag = [];
/**
* @var array The attributes that are mass assignable.
*/
protected $fillable = [
'markup',
'settings',
'code'
];
/**
* The methods that should be returned from the collection of all objects.
*
* @var array
*/
protected $passthru = [
'lists',
'where',
'sortBy',
'whereComponent',
'withComponent'
];
/**
* @var bool Model supports code and settings sections.
*/
protected $isCompoundObject = true;
/**
* @var array|null Cache for component properties.
*/
protected static $objectComponentPropertyMap = null;
/**
* @var mixed Cache store for the getViewBag method.
*/
protected $viewBagCache = false;
/**
* Triggered after the object is loaded.
* @return void
*/
public function afterFetch()
{
$this->parseComponentSettings();
$this->validateSettings();
$this->parseSettings();
}
/**
* Triggered when the model is saved.
* @return void
*/
public function beforeSave()
{
$this->checkSafeMode();
}
/**
* Create a new Collection instance.
*
* @param array $models
* @return \October\Rain\Halcyon\Collection
*/
public function newCollection(array $models = [])
{
return new CmsObjectCollection($models);
}
/**
* If the model is loaded with an invalid INI section, the invalid content will be
* passed as a special attribute. Look for it, then locate the failure reason.
* @return void
*/
protected function validateSettings()
{
if (isset($this->attributes[SectionParser::ERROR_INI])) {
CmsException::mask($this, 200);
Ini::parse($this->attributes[SectionParser::ERROR_INI]);
CmsException::unmask();
}
}
/**
* Parses the settings array.
* Child classes can override this method in order to update the content
* of the $settings property after the object is loaded from a file.
* @return void
*/
protected function parseSettings()
{
$this->fillViewBagArray();
}
/**
* This method checks if safe mode is enabled by config, and the code
* attribute is modified and populated. If so an exception is thrown.
* @return void
*/
protected function checkSafeMode()
{
$safeMode = Config::get('cms.enableSafeMode', null);
if ($safeMode === null) {
$safeMode = !Config::get('app.debug', false);
}
if ($safeMode && $this->isDirty('code') && strlen(trim($this->code))) {
throw new ApplicationException(Lang::get('cms::lang.cms_object.safe_mode_enabled'));
}
}
//
// Components
//
/**
* Runs components defined in the settings
* Process halts if a component returns a value
* @return void
*/
public function runComponents()
{
foreach ($this->components as $component) {
if ($event = $component->fireEvent('component.beforeRun', [], true)) {
return $event;
}
if ($result = $component->onRun()) {
return $result;
}
if ($event = $component->fireEvent('component.run', [], true)) {
return $event;
}
}
}
/**
* Parse component sections.
* Replace the multiple component sections with a single "components"
* element in the $settings property.
* @return void
*/
protected function parseComponentSettings()
{
$this->settings = $this->getSettingsAttribute();
$manager = ComponentManager::instance();
$components = [];
foreach ($this->settings as $setting => $value) {
if (!is_array($value)) {
continue;
}
$settingParts = explode(' ', $setting);
$settingName = $settingParts[0];
$components[$setting] = $value;
unset($this->settings[$setting]);
}
$this->settings['components'] = $components;
}
/**
* Returns a component by its name.
* This method is used only in the back-end and for internal system needs when
* the standard way to access components is not an option.
* @param string $componentName Specifies the component name.
* @return \Cms\Classes\ComponentBase Returns the component instance or null.
*/
public function getComponent($componentName)
{
if (!($componentSection = $this->hasComponent($componentName))) {
return null;
}
return ComponentManager::instance()->makeComponent(
$componentName,
null,
$this->settings['components'][$componentSection]
);
}
/**
* Checks if the object has a component with the specified name.
* @param string $componentName Specifies the component name.
* @return mixed Return false or the full component name used on the page (it could include the alias).
*/
public function hasComponent($componentName)
{
$componentManager = ComponentManager::instance();
$componentName = $componentManager->resolve($componentName);
foreach ($this->settings['components'] as $sectionName => $values) {
$result = $sectionName;
if ($sectionName == $componentName) {
return $result;
}
$parts = explode(' ', $sectionName);
if (count($parts) > 1) {
$sectionName = trim($parts[0]);
if ($sectionName == $componentName) {
return $result;
}
}
$sectionName = $componentManager->resolve($sectionName);
if ($sectionName == $componentName) {
return $result;
}
}
return false;
}
/**
* Returns component property names and values.
* This method implements caching and can be used in the run-time on the front-end.
* @param string $componentName Specifies the component name.
* @return array Returns an associative array with property names in the keys and property values in the values.
*/
public function getComponentProperties($componentName)
{
$key = md5($this->theme->getPath()).'component-properties';
if (self::$objectComponentPropertyMap !== null) {
$objectComponentMap = self::$objectComponentPropertyMap;
}
else {
$cached = Cache::get($key, false);
$unserialized = $cached ? @unserialize(@base64_decode($cached)) : false;
$objectComponentMap = $unserialized ? $unserialized : [];
if ($objectComponentMap) {
self::$objectComponentPropertyMap = $objectComponentMap;
}
}
$objectCode = $this->getBaseFileName();
if (array_key_exists($objectCode, $objectComponentMap)) {
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName];
}
return [];
}
if (!isset($this->settings['components'])) {
$objectComponentMap[$objectCode] = [];
}
else {
foreach ($this->settings['components'] as $name => $settings) {
$nameParts = explode(' ', $name);
if (count($nameParts) > 1) {
$name = trim($nameParts[0]);
}
$component = $this->getComponent($name);
if (!$component) {
continue;
}
$componentProperties = [];
$propertyDefinitions = $component->defineProperties();
foreach ($propertyDefinitions as $propertyName => $propertyInfo) {
$componentProperties[$propertyName] = $component->property($propertyName);
}
$objectComponentMap[$objectCode][$name] = $componentProperties;
}
}
self::$objectComponentPropertyMap = $objectComponentMap;
Cache::put($key, base64_encode(serialize($objectComponentMap)), Config::get('cms.parsedPageCacheTTL', 10));
if (array_key_exists($componentName, $objectComponentMap[$objectCode])) {
return $objectComponentMap[$objectCode][$componentName];
}
return [];
}
/**
* Clears the object cache.
* @param \Cms\Classes\Theme $theme Specifies a parent theme.
* @return void
*/
public static function clearCache($theme)
{
$key = md5($theme->getPath()).'component-properties';
Cache::forget($key);
}
//
// View Bag
//
/**
* Returns the configured view bag component.
* This method is used only in the back-end and for internal system needs when
* the standard way to access components is not an option.
* @return \Cms\Components\ViewBag Returns the view bag component instance.
*/
public function getViewBag()
{
if ($this->viewBagCache !== false) {
return $this->viewBagCache;
}
$componentName = 'viewBag';
if (!isset($this->settings['components'][$componentName])) {
$viewBag = new ViewBag(null, []);
$viewBag->name = $componentName;
return $this->viewBagCache = $viewBag;
}
return $this->viewBagCache = $this->getComponent($componentName);
}
/**
* Copies view bag properties to the view bag array.
* This is required for the back-end editors.
* @return void
*/
protected function fillViewBagArray()
{
$viewBag = $this->getViewBag();
foreach ($viewBag->getProperties() as $name => $value) {
$this->viewBag[$name] = $value;
}
$this->fireEvent('cmsObject.fillViewBagArray');
}
//
// Twig
//
/**
* Returns the Twig content string
* @return string
*/
public function getTwigContent()
{
return $this->markup;
}
/**
* Returns Twig node tree generated from the object's markup.
* This method is used by the system internally and shouldn't
* participate in the front-end request processing.
* @link http://twig.sensiolabs.org/doc/internals.html Twig internals
* @param mixed $markup Specifies the markup content.
* Use FALSE to load the content from the markup section.
* @return Twig_Node_Module A node tree
*/
public function getTwigNodeTree($markup = false)
{
$loader = new TwigLoader();
$twig = new Twig_Environment($loader, []);
$twig->addExtension(new CmsTwigExtension());
$twig->addExtension(new SystemTwigExtension);
$stream = $twig->tokenize(new Twig_Source($markup === false ? $this->markup : $markup, 'getTwigNodeTree'));
return $twig->parse($stream);
}
//
// Magic
//
/**
* Implements getter functionality for visible properties defined in
* the settings section or view bag array.
*/
public function __get($name)
{
if (is_array($this->settings) && array_key_exists($name, $this->settings)) {
return $this->settings[$name];
}
if (is_array($this->viewBag) && array_key_exists($name, $this->viewBag)) {
return $this->viewBag[$name];
}
return parent::__get($name);
}
/**
* Dynamically set attributes on the model.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function __set($key, $value)
{
parent::__set($key, $value);
if (array_key_exists($key, $this->settings)) {
$this->settings[$key] = $this->attributes[$key];
}
}
/**
* Determine if an attribute exists on the object.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
if (parent::__isset($key) === true) {
return true;
}
if (isset($this->viewBag[$key]) === true) {
return true;
}
return isset($this->settings[$key]);
}
/**
* Dynamically handle calls into the query instance.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (in_array($method, $this->passthru)) {
$collection = $this->get();
return call_user_func_array([$collection, $method], $parameters);
}
return parent::__call($method, $parameters);
}
}