|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlexMasterov\EquipTwig\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use AlexMasterov\EquipTwig\Exception\ExtensionException; |
|
6
|
|
|
use Auryn\Injector; |
|
7
|
|
|
use Equip\Configuration\ConfigurationInterface; |
|
8
|
|
|
use Equip\Structure\Set; |
|
9
|
|
|
use Twig_Environment; |
|
10
|
|
|
use Twig_ExtensionInterface; |
|
11
|
|
|
use Twig_Extension_Debug; |
|
12
|
|
|
|
|
13
|
|
|
class TwigExtensionSet extends Set implements ConfigurationInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param Injector $injector |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public function apply(Injector $injector) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$injector->prepare(Twig_Environment::class, [$this, 'prepareExtension']); |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Twig_Environment $environment |
|
25
|
|
|
* @param Injector $injector |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function prepareExtension(Twig_Environment $environment, Injector $injector) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$extensions = $this->toArray(); |
|
32
|
|
|
|
|
33
|
1 |
|
if ($environment->isDebug()) { |
|
34
|
1 |
|
$extensions[] = Twig_Extension_Debug::class; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
foreach ($extensions as $extension) { |
|
38
|
1 |
|
if (!is_object($extension)) { |
|
39
|
1 |
|
$extension = $injector->make($extension); |
|
40
|
|
|
} |
|
41
|
1 |
|
$environment->addExtension($extension); |
|
42
|
|
|
} |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
* |
|
48
|
|
|
* @throws ExtensionException |
|
49
|
|
|
* If $classes does not implement the correct interface. |
|
50
|
|
|
*/ |
|
51
|
4 |
|
protected function assertValid(array $classes) |
|
52
|
|
|
{ |
|
53
|
4 |
|
parent::assertValid($classes); |
|
54
|
|
|
|
|
55
|
4 |
|
foreach ($classes as $extension) { |
|
56
|
2 |
|
if (!is_subclass_of($extension, Twig_ExtensionInterface::class)) { |
|
57
|
2 |
|
throw ExtensionException::invalidClass($extension); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
3 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|