1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\UI; |
19
|
|
|
|
20
|
|
|
use Biurad\UI\Exceptions\RenderException; |
21
|
|
|
use Biurad\UI\Interfaces\LoaderInterface; |
22
|
|
|
use Biurad\UI\Interfaces\RenderInterface; |
23
|
|
|
use Biurad\UI\Interfaces\StorageInterface; |
24
|
|
|
use Biurad\UI\Interfaces\TemplateInterface; |
25
|
|
|
|
26
|
|
|
final class Template implements TemplateInterface |
27
|
|
|
{ |
28
|
|
|
/** @var LoaderInterface */ |
29
|
|
|
private $loader; |
30
|
|
|
|
31
|
|
|
/** @var RenderInterface[] */ |
32
|
|
|
private $renders; |
33
|
|
|
|
34
|
|
|
/** @var array<string,mixed> */ |
35
|
|
|
private $globals = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param StorageInterface $storage |
39
|
|
|
* @param null|Profile $profile |
40
|
|
|
* @param RenderInterface[] $renders An array of RenderInterface instances to add |
41
|
|
|
*/ |
42
|
|
|
public function __construct(StorageInterface $storage, ?Profile $profile = null, array $renders = []) |
43
|
|
|
{ |
44
|
|
|
$this->loader = new Loader($storage, $profile); |
45
|
|
|
|
46
|
|
|
foreach ($renders as $render) { |
47
|
|
|
$this->addRender($render); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function addGlobal(string $name, $value): void |
55
|
|
|
{ |
56
|
|
|
$this->globals[$name] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function addNamespace(string $namespace, $hints): void |
63
|
|
|
{ |
64
|
|
|
$this->loader->addNamespace($namespace, $hints); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function addRender(RenderInterface $render): void |
71
|
|
|
{ |
72
|
|
|
$this->renders[] = $render->withLoader($this->loader); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get all associated view engines. |
77
|
|
|
* |
78
|
|
|
* @return RenderInterface[] |
79
|
|
|
*/ |
80
|
|
|
public function getRenders(): array |
81
|
|
|
{ |
82
|
|
|
return $this->renders; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function render(string $template, array $parameters = []): string |
89
|
|
|
{ |
90
|
|
|
$this->addGlobal('template', $this); |
91
|
|
|
|
92
|
|
|
foreach ($this->renders as $engine) { |
93
|
|
|
if ($engine->getLoader()->exists($template)) { |
94
|
|
|
return $engine->render($template, \array_replace($parameters, $this->globals)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw new RenderException( |
99
|
|
|
\sprintf('No render engine is able to work with the template "%s".', $template) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Find the template file that exist, then render it contents. |
105
|
|
|
* |
106
|
|
|
* @param string $templates |
107
|
|
|
* @param array<string,mixed> $parameters |
108
|
|
|
* |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
|
|
public function renderTemplates(array $templates, array $parameters): ?string |
112
|
|
|
{ |
113
|
|
|
$this->addGlobal('template', $this); |
114
|
|
|
|
115
|
|
|
foreach ($this->renders as $engine) { |
116
|
|
|
foreach ($templates as $template) { |
117
|
|
|
if ($engine->getLoader()->exists($template)) { |
118
|
|
|
return $engine->render($template, \array_replace($parameters, $this->globals)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|