-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ViewBuilder.php
694 lines (615 loc) · 16.6 KB
/
ViewBuilder.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
<?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
* For full copyright and license information, please see the LICENSE.txt
* 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.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View;
use Cake\Core\App;
use Cake\Event\EventManagerInterface;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\View\Exception\MissingViewException;
use Closure;
use Exception;
use InvalidArgumentException;
use JsonSerializable;
use PDO;
use function Cake\Core\pluginSplit;
/**
* Provides an API for iteratively building a view up.
*
* Once you have configured the view and established all the context
* you can create a view instance with `build()`.
*/
class ViewBuilder implements JsonSerializable
{
/**
* The subdirectory to the template.
*
* @var string|null
*/
protected ?string $_templatePath = null;
/**
* The template file to render.
*
* @var string|null
*/
protected ?string $_template = null;
/**
* The plugin name to use.
*
* @var string|null
*/
protected ?string $_plugin = null;
/**
* The theme name to use.
*
* @var string|null
*/
protected ?string $_theme = null;
/**
* The layout name to render.
*
* @var string|null
*/
protected ?string $_layout = null;
/**
* Whether autoLayout should be enabled.
*
* @var bool
*/
protected bool $_autoLayout = true;
/**
* The layout path to build the view with.
*
* @var string|null
*/
protected ?string $_layoutPath = null;
/**
* The view variables to use
*
* @var string|null
*/
protected ?string $_name = null;
/**
* The view class name to use.
* Can either use plugin notation, a short name
* or a fully namespaced classname.
*
* @var string|null
* @psalm-var class-string<\Cake\View\View>|string|null
*/
protected ?string $_className = null;
/**
* Additional options used when constructing the view.
*
* These options array lets you provide custom constructor
* arguments to application/plugin view classes.
*
* @var array<string, mixed>
*/
protected array $_options = [];
/**
* The helpers to use
*
* @var array
*/
protected array $_helpers = [];
/**
* View vars
*
* @var array<string, mixed>
*/
protected array $_vars = [];
/**
* Saves a variable for use inside a template.
*
* @param string $name A string or an array of data.
* @param mixed $value Value.
* @return $this
*/
public function setVar(string $name, mixed $value = null)
{
$this->_vars[$name] = $value;
return $this;
}
/**
* Saves view vars for use inside templates.
*
* @param array<string, mixed> $data Array of data.
* @param bool $merge Whether to merge with existing vars, default true.
* @return $this
*/
public function setVars(array $data, bool $merge = true)
{
if ($merge) {
$this->_vars = $data + $this->_vars;
} else {
$this->_vars = $data;
}
return $this;
}
/**
* Check if view var is set.
*
* @param string $name Var name
* @return bool
*/
public function hasVar(string $name): bool
{
return array_key_exists($name, $this->_vars);
}
/**
* Get view var
*
* @param string $name Var name
* @return mixed The var value or null if unset.
*/
public function getVar(string $name): mixed
{
return $this->_vars[$name] ?? null;
}
/**
* Get all view vars.
*
* @return array<string, mixed>
*/
public function getVars(): array
{
return $this->_vars;
}
/**
* Sets path for template files.
*
* @param string|null $path Path for view files.
* @return $this
*/
public function setTemplatePath(?string $path)
{
$this->_templatePath = $path;
return $this;
}
/**
* Gets path for template files.
*
* @return string|null
*/
public function getTemplatePath(): ?string
{
return $this->_templatePath;
}
/**
* Sets path for layout files.
*
* @param string|null $path Path for layout files.
* @return $this
*/
public function setLayoutPath(?string $path)
{
$this->_layoutPath = $path;
return $this;
}
/**
* Gets path for layout files.
*
* @return string|null
*/
public function getLayoutPath(): ?string
{
return $this->_layoutPath;
}
/**
* Turns on or off CakePHP's conventional mode of applying layout files.
* On by default. Setting to off means that layouts will not be
* automatically applied to rendered views.
*
* @param bool $enable Boolean to turn on/off.
* @return $this
*/
public function enableAutoLayout(bool $enable = true)
{
$this->_autoLayout = $enable;
return $this;
}
/**
* Turns off CakePHP's conventional mode of applying layout files.
*
* Setting to off means that layouts will not be automatically applied to
* rendered views.
*
* @return $this
*/
public function disableAutoLayout()
{
$this->_autoLayout = false;
return $this;
}
/**
* Returns if CakePHP's conventional mode of applying layout files is enabled.
* Disabled means that layouts will not be automatically applied to rendered views.
*
* @return bool
*/
public function isAutoLayoutEnabled(): bool
{
return $this->_autoLayout;
}
/**
* Sets the plugin name to use.
*
* @param string|null $name Plugin name.
* Use null to remove the current plugin name.
* @return $this
*/
public function setPlugin(?string $name)
{
$this->_plugin = $name;
return $this;
}
/**
* Gets the plugin name to use.
*
* @return string|null
*/
public function getPlugin(): ?string
{
return $this->_plugin;
}
/**
* Adds a helper to use, overwriting any existing one with that name.
*
* @param string $helper Helper to use.
* @param array<string, mixed> $options Options.
* @return $this
* @since 4.1.0
*/
public function addHelper(string $helper, array $options = [])
{
[$plugin, $name] = pluginSplit($helper);
if ($plugin) {
$options['className'] = $helper;
}
$this->_helpers[$name] = $options;
return $this;
}
/**
* Adds helpers to use, overwriting any existing one with that name.
*
* @param array $helpers Helpers to use.
* @return $this
* @since 4.3.0
*/
public function addHelpers(array $helpers)
{
foreach ($helpers as $helper => $config) {
if (is_int($helper)) {
$helper = $config;
$config = [];
}
$this->addHelper($helper, $config);
}
return $this;
}
/**
* Sets the helpers to use, resetting the helpers config.
*
* @param array $helpers Helpers to use.
* @return $this
*/
public function setHelpers(array $helpers)
{
$this->_helpers = [];
foreach ($helpers as $helper => $config) {
if (is_int($helper)) {
$helper = $config;
$config = [];
}
$this->addHelper($helper, $config);
}
return $this;
}
/**
* Gets the helpers to use.
*
* @return array
*/
public function getHelpers(): array
{
return $this->_helpers;
}
/**
* Sets the view theme to use.
*
* @param string|null $theme Theme name.
* Use null to remove the current theme.
* @return $this
*/
public function setTheme(?string $theme)
{
$this->_theme = $theme;
return $this;
}
/**
* Gets the view theme to use.
*
* @return string|null
*/
public function getTheme(): ?string
{
return $this->_theme;
}
/**
* Sets the name of the view file to render. The name specified is the
* filename in `templates/<SubFolder>/` without the .php extension.
*
* @param string|null $name View file name to set, or null to remove the template name.
* @return $this
*/
public function setTemplate(?string $name)
{
$this->_template = $name;
return $this;
}
/**
* Gets the name of the view file to render. The name specified is the
* filename in `templates/<SubFolder>/` without the .php extension.
*
* @return string|null
*/
public function getTemplate(): ?string
{
return $this->_template;
}
/**
* Sets the name of the layout file to render the view inside of.
* The name specified is the filename of the layout in `templates/layout/`
* without the .php extension.
*
* @param string|null $name Layout file name to set.
* @return $this
*/
public function setLayout(?string $name)
{
$this->_layout = $name;
return $this;
}
/**
* Gets the name of the layout file to render the view inside.
*
* @return string|null
*/
public function getLayout(): ?string
{
return $this->_layout;
}
/**
* Get view option.
*
* @param string $name The name of the option.
* @return mixed
*/
public function getOption(string $name): mixed
{
return $this->_options[$name] ?? null;
}
/**
* Set view option.
*
* @param string $name The name of the option.
* @param mixed $value Value to set.
* @return $this
*/
public function setOption(string $name, mixed $value)
{
$this->_options[$name] = $value;
return $this;
}
/**
* Sets additional options for the view.
*
* This lets you provide custom constructor arguments to application/plugin view classes.
*
* @param array<string, mixed> $options An array of options.
* @param bool $merge Whether to merge existing data with the new data.
* @return $this
*/
public function setOptions(array $options, bool $merge = true)
{
if ($merge) {
$options += $this->_options;
}
$this->_options = $options;
return $this;
}
/**
* Gets additional options for the view.
*
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->_options;
}
/**
* Sets the view name.
*
* @param string|null $name The name of the view, or null to remove the current name.
* @return $this
*/
public function setName(?string $name)
{
$this->_name = $name;
return $this;
}
/**
* Gets the view name.
*
* @return string|null
*/
public function getName(): ?string
{
return $this->_name;
}
/**
* Sets the view classname.
*
* Accepts either a short name (Ajax) a plugin name (MyPlugin.Ajax)
* or a fully namespaced name (App\View\AppView) or null to use the
* View class provided by CakePHP.
*
* @param string|null $name The class name for the view.
* @return $this
*/
public function setClassName(?string $name)
{
$this->_className = $name;
return $this;
}
/**
* Gets the view classname.
*
* @return string|null
*/
public function getClassName(): ?string
{
return $this->_className;
}
/**
* Using the data in the builder, create a view instance.
*
* If className() is null, App\View\AppView will be used.
* If that class does not exist, then {@link \Cake\View\View} will be used.
*
* @param \Cake\Http\ServerRequest|null $request The request to use.
* @param \Cake\Http\Response|null $response The response to use.
* @param \Cake\Event\EventManagerInterface|null $events The event manager to use.
* @return \Cake\View\View
* @throws \Cake\View\Exception\MissingViewException
*/
public function build(
?ServerRequest $request = null,
?Response $response = null,
?EventManagerInterface $events = null
): View {
$className = $this->_className ?? App::className('App', 'View', 'View') ?? View::class;
if ($className === 'View') {
$className = App::className($className, 'View');
} else {
$className = App::className($className, 'View', 'View');
}
if ($className === null) {
throw new MissingViewException(['class' => $this->_className]);
}
$data = [
'name' => $this->_name,
'templatePath' => $this->_templatePath,
'template' => $this->_template,
'plugin' => $this->_plugin,
'theme' => $this->_theme,
'layout' => $this->_layout,
'autoLayout' => $this->_autoLayout,
'layoutPath' => $this->_layoutPath,
'helpers' => $this->_helpers,
'viewVars' => $this->_vars,
];
$data += $this->_options;
/** @var \Cake\View\View */
return new $className($request, $response, $events, $data);
}
/**
* Serializes the view builder object to a value that can be natively
* serialized and re-used to clone this builder instance.
*
* There are limitations for viewVars that are good to know:
*
* - ORM\Query executed and stored as resultset
* - SimpleXMLElements stored as associative array
* - Exceptions stored as strings
* - Resources, \Closure and \PDO are not supported.
*
* @return array Serializable array of configuration properties.
*/
public function jsonSerialize(): array
{
$properties = [
'_templatePath', '_template', '_plugin', '_theme', '_layout', '_autoLayout',
'_layoutPath', '_name', '_className', '_options', '_helpers', '_vars',
];
$array = [];
foreach ($properties as $property) {
$array[$property] = $this->{$property};
}
array_walk_recursive($array['_vars'], $this->_checkViewVars(...));
return array_filter($array, function ($i) {
return !is_array($i) && strlen((string)$i) || !empty($i);
});
}
/**
* Iterates through hash to clean up and normalize.
*
* @param mixed $item Reference to the view var value.
* @param string $key View var key.
* @return void
* @throws \InvalidArgumentException
*/
protected function _checkViewVars(mixed &$item, string $key): void
{
if ($item instanceof Exception) {
$item = (string)$item;
}
if (
is_resource($item) ||
$item instanceof Closure ||
$item instanceof PDO
) {
throw new InvalidArgumentException(sprintf(
'Failed serializing the `%s` %s in the `%s` view var',
is_resource($item) ? get_resource_type($item) : $item::class,
is_resource($item) ? 'resource' : 'object',
$key
));
}
}
/**
* Configures a view builder instance from serialized config.
*
* @param array<string, mixed> $config View builder configuration array.
* @return $this
*/
public function createFromArray(array $config)
{
foreach ($config as $property => $value) {
$this->{$property} = $value;
}
return $this;
}
/**
* Magic method used for serializing the view builder object.
*
* @return array
*/
public function __serialize(): array
{
return $this->jsonSerialize();
}
/**
* Magic method used to rebuild the view builder object.
*
* @param array<string, mixed> $data Data array.
* @return void
*/
public function __unserialize(array $data): void
{
$this->createFromArray($data);
}
}