-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
DelegatingEngine.php
97 lines (83 loc) · 2.65 KB
/
DelegatingEngine.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Templating;
trigger_deprecation('symfony/templating', '6.4', '"%s" is deprecated since version 6.4 and will be removed in 7.0. Use Twig instead.', DelegatingEngine::class);
/**
* DelegatingEngine selects an engine for a given template.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since Symfony 6.4, use Twig instead
*/
class DelegatingEngine implements EngineInterface, StreamingEngineInterface
{
/**
* @var EngineInterface[]
*/
protected $engines = [];
/**
* @param EngineInterface[] $engines An array of EngineInterface instances to add
*/
public function __construct(array $engines = [])
{
foreach ($engines as $engine) {
$this->addEngine($engine);
}
}
public function render(string|TemplateReferenceInterface $name, array $parameters = []): string
{
return $this->getEngine($name)->render($name, $parameters);
}
/**
* @return void
*/
public function stream(string|TemplateReferenceInterface $name, array $parameters = [])
{
$engine = $this->getEngine($name);
if (!$engine instanceof StreamingEngineInterface) {
throw new \LogicException(sprintf('Template "%s" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface.', $name));
}
$engine->stream($name, $parameters);
}
public function exists(string|TemplateReferenceInterface $name): bool
{
return $this->getEngine($name)->exists($name);
}
/**
* @return void
*/
public function addEngine(EngineInterface $engine)
{
$this->engines[] = $engine;
}
public function supports(string|TemplateReferenceInterface $name): bool
{
try {
$this->getEngine($name);
} catch (\RuntimeException) {
return false;
}
return true;
}
/**
* Get an engine able to render the given template.
*
* @throws \RuntimeException if no engine able to work with the template is found
*/
public function getEngine(string|TemplateReferenceInterface $name): EngineInterface
{
foreach ($this->engines as $engine) {
if ($engine->supports($name)) {
return $engine;
}
}
throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".', $name));
}
}