1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Charcoal\View; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
// From 'charcoal-view' |
11
|
|
|
use Charcoal\View\EngineInterface; |
12
|
|
|
use Charcoal\View\ViewInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base abstract class for _View_ interfaces, implements `ViewInterface`. |
16
|
|
|
* |
17
|
|
|
* Also implements the `ConfigurableInterface` |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractView implements ViewInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var EngineInterface $engine |
24
|
|
|
*/ |
25
|
|
|
private $engine; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Build the object with an array of dependencies. |
29
|
|
|
* |
30
|
|
|
* @param array $data View class dependencies. |
31
|
|
|
* @throws InvalidArgumentException If required parameters are missing. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $data) |
34
|
|
|
{ |
35
|
|
|
$this->setEngine($data['engine']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Load a template (from identifier). |
40
|
|
|
* |
41
|
|
|
* @param string $templateIdent The template identifier to load.. |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function loadTemplate(string $templateIdent): string |
45
|
|
|
{ |
46
|
|
|
if (!$templateIdent) { |
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
return $this->engine()->loadTemplate($templateIdent); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Load a template (from identifier) and render it. |
54
|
|
|
* |
55
|
|
|
* @param string $templateIdent The template identifier, to load and render. |
56
|
|
|
* @param mixed $context The view controller (rendering context). |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function render(string $templateIdent, $context = null): string |
60
|
|
|
{ |
61
|
|
|
return $this->engine()->render($templateIdent, $context); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Render a template (from string). |
66
|
|
|
* |
67
|
|
|
* @param string $templateString The full template string to render. |
68
|
|
|
* @param mixed $context The view controller (rendering context). |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function renderTemplate(string $templateString, $context = null): string |
72
|
|
|
{ |
73
|
|
|
return $this->engine()->render($templateString, $context); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $varName The name of the variable to set this template unto. |
78
|
|
|
* @param string|null $templateIdent The "dynamic template" to set. null to clear. |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function setDynamicTemplate(string $varName, ?string $templateIdent): void |
82
|
|
|
{ |
83
|
|
|
$this->engine()->setDynamicTemplate($varName, $templateIdent); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the view's rendering engine instance. |
88
|
|
|
* |
89
|
|
|
* @return EngineInterface |
90
|
|
|
*/ |
91
|
|
|
protected function engine(): EngineInterface |
92
|
|
|
{ |
93
|
|
|
return $this->engine; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the engine (`EngineInterface`) dependency. |
98
|
|
|
* |
99
|
|
|
* @param EngineInterface $engine The rendering engine. |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
private function setEngine(EngineInterface $engine): void |
103
|
|
|
{ |
104
|
|
|
$this->engine = $engine; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|