1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\PhpTemplatesBundle\DependencyInjection\Compiler; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
11
|
|
|
|
12
|
|
|
final class AddPathsCompilerPass implements CompilerPassInterface |
13
|
|
|
{ |
14
|
|
|
public function process(ContainerBuilder $container): void |
15
|
|
|
{ |
16
|
|
|
if (!$container->has('setono_php_templates.engine.default')) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$engine = $container->getDefinition('setono_php_templates.engine.default'); |
21
|
|
|
|
22
|
|
|
if ($container->hasParameter('kernel.bundles')) { |
23
|
|
|
self::addBundlePaths($container->getParameter('kernel.bundles'), $engine); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($container->hasParameter('kernel.project_dir')) { |
27
|
|
|
self::addProjectPath($container->getParameter('kernel.project_dir'), $engine); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private static function addBundlePaths(array $bundles, Definition $engine): void |
32
|
|
|
{ |
33
|
|
|
foreach ($bundles as $bundleName => $bundleClass) { |
34
|
|
|
$bundle = new $bundleClass(); |
35
|
|
|
if (!$bundle instanceof BundleInterface) { |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$phpTemplatesPath = $bundle->getPath() . '/Resources/views/php'; |
40
|
|
|
|
41
|
|
|
if (!is_dir($phpTemplatesPath) || !is_readable($phpTemplatesPath)) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$engine->addMethodCall('addPath', [$phpTemplatesPath]); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private static function addProjectPath(string $projectDir, Definition $engine): void |
50
|
|
|
{ |
51
|
|
|
$phpTemplatesPath = $projectDir . '/Resources/views/php'; |
52
|
|
|
|
53
|
|
|
if (!is_dir($phpTemplatesPath) || !is_readable($phpTemplatesPath)) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$engine->addMethodCall('addPath', [$phpTemplatesPath]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|