1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Symplify |
5
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Symplify\PHP7_Sculpin\DI; |
9
|
|
|
|
10
|
|
|
use Nette\DI\Compiler; |
11
|
|
|
use Nette\DI\CompilerExtension; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
14
|
|
|
use Symplify\PHP7_Sculpin\DI\Helper\CollectByTypeTrait; |
15
|
|
|
|
16
|
|
|
final class SculpinCompilerExtension extends CompilerExtension |
17
|
|
|
{ |
18
|
|
|
use CollectByTypeTrait; |
19
|
|
|
|
20
|
|
|
public function loadConfiguration() |
21
|
|
|
{ |
22
|
|
|
Compiler::loadDefinitions( |
23
|
|
|
$this->getContainerBuilder(), |
24
|
|
|
$this->loadFromFile(__DIR__.'/../config/services.neon') |
|
|
|
|
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function beforeCompile() |
29
|
|
|
{ |
30
|
|
|
$containerBuilder = $this->getContainerBuilder(); |
31
|
|
|
|
32
|
|
|
$this->collectServicesByType( |
33
|
|
|
EventDispatcherInterface::class, |
34
|
|
|
EventSubscriberInterface::class, |
35
|
|
|
'addSubscriber' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
// |
39
|
|
|
$definition = $containerBuilder->getDefinition('sculpin_posts.posts_map'); |
40
|
|
|
foreach ($containerBuilder->findByTag('sculpin_posts.posts_map') as $id => $tagAttributes) { |
41
|
|
|
foreach ($tagAttributes as $attributes) { |
42
|
|
|
$definition->addSetup('addMap', ['@' . $id]); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.converter_manager'); |
47
|
|
View Code Duplication |
foreach ($containerBuilder->findByTag('sculpin.converter') as $id => $tagAttributes) { |
|
|
|
|
48
|
|
|
foreach ($tagAttributes as $attributes) { |
49
|
|
|
$definition->addSetup('registerConverter', [$attributes['alias'], '@' . $id] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.custom_mime_types_repository'); |
55
|
|
|
$data = []; |
56
|
|
|
foreach ($containerBuilder->findByTag('sculpin.custom_mime_extensions') as $tagAttributes) { |
57
|
|
|
foreach ($tagAttributes as $attributes) { |
58
|
|
|
$type = $attributes['type']; |
|
|
|
|
59
|
|
|
$parameter = $attributes['parameter']; |
60
|
|
|
|
61
|
|
|
dump($parameter); |
62
|
|
|
die; |
|
|
|
|
63
|
|
|
|
64
|
|
|
if ($containerBuilder->hasParameter($parameter)) { |
|
|
|
|
65
|
|
|
if (isset($data[$type])) { |
66
|
|
|
$data[$type] = array_unique(array_merge( |
67
|
|
|
$containerBuilder->getParameter($type), |
68
|
|
|
$containerBuilder->getParameter($parameter) |
69
|
|
|
)); |
70
|
|
|
} else { |
71
|
|
|
$data[$type] = array_unique($containerBuilder->getParameter($parameter)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($data as $type => $extensions) { |
78
|
|
|
$data[$type] = array_filter($extensions, function ($var) { |
79
|
|
|
return strlen($var) > 0; |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$definition->addArgument($data); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.formatter_manager'); |
87
|
|
View Code Duplication |
foreach ($containerBuilder->findByTag('sculpin.formatter') as $id => $tagAttributes) { |
|
|
|
|
88
|
|
|
foreach ($tagAttributes as $attributes) { |
89
|
|
|
$definition->addSetup('registerFormatter', [$attributes['alias'], '@' . $id]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.generator_manager'); |
94
|
|
View Code Duplication |
foreach ($containerBuilder->findByTag('sculpin.generator') as $id => $tagAttributes) { |
|
|
|
|
95
|
|
|
foreach ($tagAttributes as $attributes) { |
96
|
|
|
$definition->addSetup('registerGenerator', [$attributes['alias'], '@' . $id]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.data_source'); |
101
|
|
|
foreach ($containerBuilder->findByTag('sculpin.data_source') as $id => $tagAttributes) { |
102
|
|
|
$definition->addSetup('addDataSource', ['@' . $id]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$definition = $containerBuilder->getDefinition('event_dispatcher'); |
106
|
|
|
foreach ($containerBuilder->findByTag('kernel.event_subscriber') as $id => $attributes) { |
107
|
|
|
$class = $containerBuilder->getDefinition($id)->getClass(); |
108
|
|
|
$definition->addSetup('addSubscriberService', [$id, $class]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// twig |
112
|
|
|
$definition = $containerBuilder->getDefinition('sculpin_twig.twig'); |
113
|
|
|
|
114
|
|
|
// Extensions must always be registered before everything else. |
115
|
|
|
// For instance, global variable definitions must be registered |
116
|
|
|
// afterward. If not, the globals from the extensions will never |
117
|
|
|
// be registered. |
118
|
|
|
$calls = $definition->getMethodCalls(); |
119
|
|
|
$definition->setMethodCalls([]); |
120
|
|
|
foreach ($containerBuilder->findByTag('twig.extension') as $id => $attributes) { |
121
|
|
|
$definition->addSetup('addExtension', ['@' . $id]); |
122
|
|
|
} |
123
|
|
|
$definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls)); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// twig loaders |
127
|
|
|
$definition = $containerBuilder->getDefinition('sculpin_twig.loader'); |
128
|
|
|
$arguments = $definition->getParameters(); |
129
|
|
|
$loaders = $arguments[0]; |
130
|
|
|
|
131
|
|
|
$prependedLoaders = []; |
132
|
|
|
$appendedLoaders = []; |
133
|
|
|
foreach ($containerBuilder->findByTag('twig.loaders.prepend') as $id => $attributes) { |
134
|
|
|
$prependedLoaders[] = '@' . $id; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($containerBuilder->findByTag('twig.loaders.append') as $id => $attributes) { |
138
|
|
|
$appendedLoaders[] = '@' . $id; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$parameters = $containerBuilder->parameters; |
142
|
|
|
$sourceViewPaths = $parameters['sculpin_twig.source_view_paths']; |
143
|
|
|
foreach ($parameters['kernel.bundles'] as $class) { |
144
|
|
|
$reflection = new \ReflectionClass($class); |
145
|
|
|
foreach ($sourceViewPaths as $sourceViewPath) { |
146
|
|
|
if (is_dir($dir = dirname($reflection->getFilename()) . '/Resources/' . $sourceViewPath)) { |
147
|
|
|
$appendedLoaders[] = $dir; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$arguments[0] = array_merge($prependedLoaders, $loaders, $appendedLoaders); |
153
|
|
|
$definition->setArguments($arguments); |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
$definition = $containerBuilder->getDefinition('sculpin.data_provider_manager'); |
157
|
|
|
|
158
|
|
View Code Duplication |
foreach ($containerBuilder->findByTag('sculpin.data_provider') as $id => $tagAttributes) { |
|
|
|
|
159
|
|
|
foreach ($tagAttributes as $attributes) { |
160
|
|
|
$definition->addSetup('registerDataProvider', [$attributes['alias'], '@' . $id]); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
///** |
|
|
|
|
165
|
|
|
// * {@inheritdoc} |
166
|
|
|
// */ |
167
|
|
|
//public function process(ContainerBuilder $container) |
168
|
|
|
//{ |
169
|
|
|
// foreach ($container->findByTag('sculpin.path_configurator') as $tagAttributes) { |
170
|
|
|
// foreach ($tagAttributes as $attributes) { |
171
|
|
|
// $typeParameter = 'sculpin.'.$attributes['type']; |
172
|
|
|
// $parameter = $attributes['parameter']; |
173
|
|
|
// |
174
|
|
|
// if ($container->hasParameter($parameter)) { |
175
|
|
|
// $value = $container->getParameter($parameter); |
176
|
|
|
// if (!is_array($value)) { |
177
|
|
|
// $value = [$value]; |
178
|
|
|
// } |
179
|
|
|
// |
180
|
|
|
// if ($container->hasParameter($typeParameter)) { |
181
|
|
|
// $container->setParameter($typeParameter, array_unique(array_merge($container->getParameter($typeParameter), $this->antify($value)))); |
182
|
|
|
// } else { |
183
|
|
|
// $container->setParameter($typeParameter, array_unique($this->antify($value))); |
184
|
|
|
// } |
185
|
|
|
// } |
186
|
|
|
// } |
187
|
|
|
// } |
188
|
|
|
//} |
189
|
|
|
//} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// private function antify(array $paths) : array |
|
|
|
|
193
|
|
|
// { |
194
|
|
|
// $matcher = new AntPathMatcher(); |
195
|
|
|
// |
196
|
|
|
// return array_map( |
197
|
|
|
// function ($path) use ($matcher) { |
198
|
|
|
// if ($matcher->isPattern($path)) { |
199
|
|
|
// return $path; |
200
|
|
|
// } |
201
|
|
|
// |
202
|
|
|
// return $path.'/**'; |
203
|
|
|
// }, |
204
|
|
|
// $paths |
205
|
|
|
// ); |
206
|
|
|
// } |
207
|
|
|
} |
208
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.