|
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(); |
|
|
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: