|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stitcher\Renderer; |
|
4
|
|
|
|
|
5
|
|
|
use Stitcher\DynamicFactory; |
|
6
|
|
|
|
|
7
|
|
|
class RendererFactory extends DynamicFactory |
|
8
|
|
|
{ |
|
9
|
|
|
protected $templateDirectory; |
|
10
|
|
|
protected $rendererConfiguration; |
|
11
|
|
|
protected $extensions = []; |
|
12
|
|
|
|
|
13
|
1 |
|
public function __construct( |
|
14
|
|
|
string $templateDirectory, |
|
15
|
|
|
?string $rendererConfiguration = 'twig' |
|
16
|
|
|
) { |
|
17
|
1 |
|
$this->templateDirectory = $templateDirectory; |
|
18
|
1 |
|
$this->rendererConfiguration = $rendererConfiguration; |
|
19
|
|
|
|
|
20
|
1 |
|
$this->setTwigRule(); |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
public static function make( |
|
24
|
|
|
string $templateDirectory, |
|
25
|
|
|
?string $renderer = 'twig' |
|
26
|
|
|
): RendererFactory { |
|
27
|
1 |
|
return new self($templateDirectory, $renderer); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function addExtension(Extension $extension): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->extensions[$extension->name()] = $extension; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function create(): ?Renderer |
|
36
|
|
|
{ |
|
37
|
1 |
|
foreach ($this->getRules() as $rule) { |
|
38
|
1 |
|
$templateRenderer = $rule($this->rendererConfiguration); |
|
39
|
|
|
|
|
40
|
1 |
|
if ($templateRenderer) { |
|
41
|
1 |
|
$this->loadExtensions($templateRenderer); |
|
42
|
|
|
|
|
43
|
1 |
|
return $templateRenderer; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
protected function loadExtensions(Renderer $renderer): void |
|
51
|
|
|
{ |
|
52
|
1 |
|
foreach ($this->extensions as $extension) { |
|
53
|
|
|
$renderer->customExtension($extension); |
|
54
|
|
|
} |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function setTwigRule(): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->setRule(TwigRenderer::class, function ($value) { |
|
60
|
1 |
|
if ($value === 'twig') { |
|
61
|
1 |
|
return TwigRenderer::make($this->templateDirectory); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return null; |
|
65
|
1 |
|
}); |
|
66
|
1 |
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|