Passed
Push — master ( d9d5dd...a7bec4 )
by Joachim
01:48
created

AddPathsCompilerPass::addBundlePaths()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 15
rs 9.6111
cc 5
nc 4
nop 2
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