RegisterTwigExtensionsPass::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 16
nop 5
1
<?php
2
3
namespace Knp\RadBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Container;
8
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
9
use Knp\RadBundle\Finder\ClassFinder;
10
use Knp\RadBundle\DependencyInjection\Definition\TwigExtensionFactory;
11
use Knp\RadBundle\DependencyInjection\ReferenceFactory;
12
use Knp\RadBundle\DependencyInjection\ServiceIdGenerator;
13
14
class RegisterTwigExtensionsPass implements CompilerPassInterface
15
{
16
    private $bundle;
17
    private $classFinder;
18
    private $definitionFactory;
19
    private $serviceIdGenerator;
20
21
    public function __construct(BundleInterface $bundle, ClassFinder $classFinder = null, TwigExtensionFactory $definitionFactory = null, ReferenceFactory $referenceFactory = null, ServiceIdGenerator $serviceIdGenerator = null)
22
    {
23
        $this->bundle = $bundle;
24
        $this->classFinder = $classFinder ?: new ClassFinder();
25
        $this->definitionFactory = $definitionFactory ?: new TwigExtensionFactory();
26
        $this->referenceFactory = $referenceFactory ?: new ReferenceFactory();
0 ignored issues
show
Bug introduced by
The property referenceFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        $this->serviceIdGenerator = $serviceIdGenerator ?: new ServiceIdGenerator();
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function process(ContainerBuilder $container)
34
    {
35
        if (!$container->getparameter('knp_rad.detect.twig')) {
36
            return;
37
        }
38
39
        if (false === $container->hasDefinition('twig')) {
40
            return;
41
        }
42
43
        $twigDef = $container->getDefinition('twig');
44
45
        $directory = $this->bundle->getPath().'/Twig';
46
        $namespace = $this->bundle->getNamespace().'\Twig';
47
48
        $potentialClasses = $this->classFinder->findClassesMatching($directory, $namespace, 'Extension$');
49
        $classes = $this->classFinder->filterClassesImplementing($potentialClasses, 'Twig_ExtensionInterface');
50
51
        foreach ($classes as $class) {
52
            $id = $this->serviceIdGenerator->generateForBundleClass($this->bundle, $class);
53
54
            if ($container->hasDefinition($id)) {
55
                continue;
56
            }
57
58
            $def = $this->definitionFactory->createDefinition($class);
59
            $ref = $this->referenceFactory->createReference($id);
60
61
            $container->setDefinition($id, $def);
62
63
            $twigDef->addMethodCall('addExtension', array($ref));
64
        }
65
    }
66
}
67