1 | <?php |
||
34 | class TwigLoaderPass implements CompilerPassInterface |
||
35 | 2 | { |
|
36 | 3 | public function process(ContainerBuilder $container) |
|
37 | { |
||
38 | 3 | if (false === $container->hasDefinition('twig')) { |
|
39 | 3 | return; |
|
40 | } |
||
41 | |||
42 | 4 | $chainLoader = $container->getDefinition('twig.loader.chain'); |
|
43 | |||
44 | 4 | $methodCalls = $chainLoader->getMethodCalls(); |
|
45 | 2 | $symfonyKey1 = null; |
|
46 | 4 | $symfonyKey2 = null; |
|
47 | 4 | $puliKey = null; |
|
48 | 2 | ||
49 | 2 | // By default, the filesystem loader is added before the Puli loader |
|
50 | // However, that prevents the Puli loader from being used at all, so |
||
51 | // we need to insert the Puli loader right before the filesystem |
||
52 | // loader |
||
53 | 4 | foreach ($methodCalls as $key => $methodCall) { |
|
54 | if ($this->isAddLoaderCall($methodCall, 'twig.loader.filesystem')) { |
||
55 | 2 | $symfonyKey1 = $key; |
|
56 | 2 | continue; |
|
57 | 2 | } |
|
58 | |||
59 | if ($this->isAddLoaderCall($methodCall, 'twig.loader.native_filesystem')) { |
||
60 | $symfonyKey2 = $key; |
||
61 | 2 | continue; |
|
62 | } |
||
63 | 2 | ||
64 | if ($this->isAddLoaderCall($methodCall, 'puli.twig.template_loader')) { |
||
65 | $puliKey = $key; |
||
66 | continue; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | // Move the Puli loader before the filesystem loaders if necessary |
||
71 | 2 | if ((null === $symfonyKey1 && null === $symfonyKey2) || null === $puliKey) { |
|
72 | 2 | return; |
|
73 | } |
||
74 | |||
75 | $symfonyKey = min($symfonyKey1, $symfonyKey2); |
||
76 | |||
77 | if ($puliKey < $symfonyKey) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | $puliLoaderCall = $methodCalls[$puliKey]; |
||
82 | unset($methodCalls[$puliKey]); |
||
83 | |||
84 | array_splice($methodCalls, $symfonyKey, 0, array($puliLoaderCall)); |
||
85 | |||
86 | $chainLoader->setMethodCalls($methodCalls); |
||
87 | } |
||
88 | |||
89 | private function isAddLoaderCall(array $methodCall, $serviceId) |
||
95 | } |
||
96 |