1
|
|
|
<?php |
2
|
|
|
namespace Boekkooi\Bundle\TwigJackBundle\DependencyInjection; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
5
|
|
|
use Symfony\Component\Config\Definition\Processor; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Warnar Boekkooi <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class BoekkooiTwigJackExtension extends Extension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
15 |
|
public function load(array $config, ContainerBuilder $container) |
23
|
|
|
{ |
24
|
15 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
25
|
|
|
|
26
|
15 |
|
$processor = new Processor(); |
27
|
15 |
|
$config = $processor->processConfiguration(new Configuration(), $config); |
28
|
|
|
|
29
|
12 |
|
$this->loadDefer($container, $loader, $config); |
30
|
12 |
|
$this->loadConstraint($container, $loader, $config); |
31
|
12 |
|
$this->loadExclamation($container, $loader, $config); |
32
|
|
|
|
33
|
12 |
|
$this->loadLoaders($container, $loader, $config); |
34
|
11 |
|
} |
35
|
|
|
|
36
|
12 |
|
private function loadLoaders(ContainerBuilder $container, LoaderInterface $configLoader, array $config) |
37
|
|
|
{ |
38
|
12 |
|
if (empty($config['loaders'])) { |
39
|
6 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
$configLoader->load('loader.yml'); |
43
|
6 |
|
$useFactoryMethod = method_exists($container->getDefinition('boekkooi.twig_jack.loader.abstract'), 'setFactory'); |
44
|
|
|
|
45
|
6 |
|
foreach ($config['loaders'] as $name => $loader) { |
46
|
6 |
|
$this->setupLoader($container, $name, $loader, $useFactoryMethod); |
47
|
5 |
|
} |
48
|
5 |
|
} |
49
|
|
|
|
50
|
6 |
|
private function setupLoader(ContainerBuilder $container, $name, array $loaderConfig, $useFactoryMethod) |
51
|
|
|
{ |
52
|
6 |
|
$repositoryService = $this->createLoaderRepository($container, $name, $loaderConfig, $useFactoryMethod); |
53
|
|
|
|
54
|
|
|
// Create loader |
55
|
5 |
|
$loader = $container->setDefinition(sprintf('boekkooi.twig_jack.loaders.%s', $name), new DefinitionDecorator('boekkooi.twig_jack.loader.abstract')); |
56
|
5 |
|
$loader->setPublic(true); |
57
|
5 |
|
$loader->addTag('twig.loader'); |
58
|
|
|
|
59
|
|
|
$loader |
60
|
5 |
|
->replaceArgument(0, new Reference($repositoryService)) |
61
|
5 |
|
->replaceArgument(1, (string) $loaderConfig['prefix']) |
62
|
5 |
|
->replaceArgument(2, !empty($loaderConfig['locale_callable']) ? new Reference($loaderConfig['locale_callable']) : null); |
63
|
5 |
|
} |
64
|
|
|
|
65
|
6 |
|
private function createLoaderRepository(ContainerBuilder $container, $loaderName, array $loaderConfig, $useFactoryMethod) |
66
|
|
|
{ |
67
|
6 |
|
switch ($loaderConfig['type']) { |
68
|
6 |
|
case 'custom': |
69
|
2 |
|
if (empty($loaderConfig['repository'])) { |
70
|
1 |
|
throw new InvalidConfigurationException(sprintf('No repository option provided for %s', $loaderName)); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $loaderConfig['repository']; |
74
|
4 |
|
case 'orm': |
75
|
2 |
|
$managerService = 'doctrine'; |
76
|
2 |
|
break; |
77
|
2 |
|
case 'mongo': |
78
|
1 |
|
$managerService = 'doctrine_mongodb'; |
79
|
1 |
|
break; |
80
|
1 |
|
case 'couch': |
81
|
1 |
|
$managerService = 'doctrine_couchdb'; |
82
|
1 |
|
break; |
83
|
|
|
// @codeCoverageIgnoreStart |
84
|
|
|
default: |
85
|
|
|
throw new InvalidConfigurationException(sprintf('Unknown loader type provided for %s', $loaderName)); |
86
|
|
|
// @codeCoverageIgnoreEnd |
87
|
4 |
|
} |
88
|
|
|
|
89
|
4 |
|
$repositoryService = sprintf('boekkooi.twig_jack.loaders.%s.object_repository', $loaderName); |
90
|
4 |
|
$entityManagerService = sprintf('boekkooi.twig_jack.loaders.%s.object_manager', $loaderName); |
91
|
|
|
|
92
|
4 |
|
$modelClass = ltrim($loaderConfig['model_class'], '\\'); |
93
|
|
|
|
94
|
|
|
// Create factory to get the entity manager for the entity |
95
|
|
|
$container |
96
|
4 |
|
->setDefinition($entityManagerService, new DefinitionDecorator('boekkooi.twig_jack.doctrine.object_manager.abstract')) |
97
|
4 |
|
->setPublic(true) |
98
|
4 |
|
->setArguments(array($modelClass)); |
99
|
4 |
|
if ($useFactoryMethod) { |
100
|
|
|
$container->getDefinition($entityManagerService) |
101
|
|
|
->setFactory(array( |
102
|
|
|
new Reference($managerService), |
103
|
|
|
'getManagerForClass' |
104
|
|
|
)); |
105
|
|
|
} else { |
106
|
4 |
|
$container->getDefinition($entityManagerService) |
|
|
|
|
107
|
4 |
|
->setFactoryService($managerService) |
108
|
4 |
|
->setFactoryMethod('getManagerForClass'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Create factory to get the repository for the entity |
112
|
|
|
$container |
113
|
4 |
|
->setDefinition($repositoryService, new DefinitionDecorator('boekkooi.twig_jack.doctrine.object_repository.abstract')) |
114
|
4 |
|
->setArguments(array($modelClass)); |
115
|
4 |
|
if ($useFactoryMethod) { |
116
|
|
|
$container->getDefinition($repositoryService) |
117
|
|
|
->setFactory(array( |
118
|
|
|
new Reference($entityManagerService), |
119
|
|
|
'getRepository' |
120
|
|
|
)); |
121
|
|
|
} else { |
122
|
4 |
|
$container->getDefinition($repositoryService) |
|
|
|
|
123
|
4 |
|
->setFactoryService($entityManagerService) |
124
|
4 |
|
->setFactoryMethod('getRepository'); |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
return $repositoryService; |
128
|
|
|
} |
129
|
|
|
|
130
|
12 |
|
private function loadDefer(ContainerBuilder $container, LoaderInterface $loader, array $config) |
131
|
|
|
{ |
132
|
12 |
|
if (!$config['defer']['enabled']) { |
133
|
1 |
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
11 |
|
$container->setParameter('boekkooi.twig_jack.defer.prefix', $config['defer']['prefix']); |
137
|
11 |
|
$loader->load('defer.yml'); |
138
|
11 |
|
} |
139
|
|
|
|
140
|
12 |
|
private function loadConstraint(ContainerBuilder $container, LoaderInterface $loader, array $config) |
141
|
|
|
{ |
142
|
12 |
|
if (!$config['constraint']['enabled']) { |
143
|
10 |
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
$loader->load('constraint.yml'); |
147
|
2 |
|
$container->getDefinition('boekkooi.twig_jack.constraint_validator') |
148
|
2 |
|
->replaceArgument(0, new Reference($config['constraint']['environment'])); |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param ContainerBuilder $container |
153
|
|
|
* @param LoaderInterface $loader |
154
|
|
|
* @param array $config |
155
|
|
|
*/ |
156
|
12 |
|
public function loadExclamation(ContainerBuilder $container, LoaderInterface $loader, array $config) |
157
|
|
|
{ |
158
|
12 |
|
$container->setParameter('boekkooi.twig_jack.exclamation', $config['exclamation']); |
159
|
|
|
|
160
|
12 |
|
if ($config['exclamation']) { |
161
|
10 |
|
$loader->load('exclamation.yml'); |
162
|
10 |
|
} |
163
|
12 |
|
} |
164
|
|
|
} |
165
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.