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 Dflydev\ApacheMimeTypes\ArrayRepository; |
11
|
|
|
use Nette\DI\Compiler; |
12
|
|
|
use Nette\DI\CompilerExtension; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Symplify\PHP7_Sculpin\Core\Converter\ConverterInterface; |
16
|
|
|
use Symplify\PHP7_Sculpin\Core\Converter\ConverterManager; |
17
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderInterface; |
18
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderManager; |
19
|
|
|
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterInterface; |
20
|
|
|
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager; |
21
|
|
|
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorInterface; |
22
|
|
|
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorManager; |
23
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\CompositeDataSource; |
24
|
|
|
use Symplify\PHP7_Sculpin\Core\Source\DataSourceInterface; |
25
|
|
|
use Symplify\PHP7_Sculpin\DI\Helper\CollectByTypeTrait; |
26
|
|
|
use Twig_ExtensionInterface; |
27
|
|
|
|
28
|
|
|
final class SculpinCompilerExtension extends CompilerExtension |
29
|
|
|
{ |
30
|
|
|
use CollectByTypeTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
private $serviceConfigFileNames = [ |
36
|
|
|
'console.neon', |
37
|
|
|
'eventDispatcher.neon', |
38
|
|
|
'filesystem.neon', |
39
|
|
|
'markdown.neon', |
40
|
|
|
'markdown-twig-compatibility.neon', |
41
|
|
|
'pagination.neon', |
42
|
|
|
'posts.neon', |
43
|
|
|
'sculpin.neon', |
44
|
|
|
'twig.neon', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
public function loadConfiguration() |
48
|
|
|
{ |
49
|
|
|
foreach ($this->serviceConfigFileNames as $serviceConfigFileName) { |
50
|
|
|
$this->loadDefinitionsFromFile(__DIR__ . '/../config/services/' . $serviceConfigFileName); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function beforeCompile() |
55
|
|
|
{ |
56
|
|
|
$containerBuilder = $this->getContainerBuilder(); |
57
|
|
|
|
58
|
|
|
$this->collectServicesByType(EventDispatcherInterface::class, EventSubscriberInterface::class, 'addSubscriber'); |
59
|
|
|
|
60
|
|
|
$this->collectServicesByType(ConverterManager::class, ConverterInterface::class, 'registerConverter'); |
61
|
|
|
|
62
|
|
|
// |
63
|
|
|
$definition = $containerBuilder->getDefinition('sculpin_posts.posts_map'); |
64
|
|
|
foreach ($containerBuilder->findByTag('sculpin_posts.posts_map') as $id => $tagAttributes) { |
65
|
|
|
foreach ($tagAttributes as $attributes) { |
66
|
|
|
$definition->addSetup('addMap', ['@' . $id]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
$definition = $this->findOneByType(ArrayRepository::class); |
72
|
|
|
$data = []; |
73
|
|
|
|
74
|
|
|
foreach ($containerBuilder->findByTag('sculpin.custom_mime_extensions') as $attributes) { |
75
|
|
|
$type = $attributes['type']; |
76
|
|
|
$parameter = $attributes['parameter']; |
77
|
|
|
|
78
|
|
|
$parameters = $containerBuilder->parameters; |
79
|
|
|
if (isset($parameters[$parameter])) { |
80
|
|
|
if (isset($data[$type])) { |
81
|
|
|
$data[$type] = array_unique(array_merge( |
82
|
|
|
$parameters[$type], |
83
|
|
|
$parameters[$parameter] |
84
|
|
|
)); |
85
|
|
|
} else { |
86
|
|
|
$data[$type] = array_unique($parameters[$parameter]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($data as $type => $extensions) { |
92
|
|
|
$data[$type] = array_filter($extensions, function ($var) { |
93
|
|
|
return strlen($var) > 0; |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
$definition->setArguments([1 => $data]); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
$this->collectServicesByType(FormatterManager::class, FormatterInterface::class, 'registerFormatter'); |
100
|
|
|
|
101
|
|
|
$this->collectServicesByType(GeneratorManager::class, GeneratorInterface::class, 'registerGenerator'); |
102
|
|
|
|
103
|
|
|
$this->collectServicesByType(CompositeDataSource::class, DataSourceInterface::class, 'addDataSource'); |
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 ($this->getContainerBuilder()->findByType(Twig_ExtensionInterface::class) as $id => $attributes) { |
121
|
|
|
$definition->addSetup('addExtension', ['@' . $id]); |
122
|
|
|
} |
123
|
|
|
// $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls)); |
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
// twig loaders |
127
|
|
|
// $parameters = $containerBuilder->parameters; |
128
|
|
|
|
129
|
|
|
// $sourceViewPaths = $parameters['sculpin_twig.source_view_paths']; |
|
|
|
|
130
|
|
|
|
131
|
|
|
// foreach ($parameters['kernel.bundles'] as $class) { |
|
|
|
|
132
|
|
|
// $reflection = new \ReflectionClass($class); |
133
|
|
|
// foreach ($sourceViewPaths as $sourceViewPath) { |
134
|
|
|
// if (is_dir($dir = dirname($reflection->getFilename()) . '/Resources/' . $sourceViewPath)) { |
135
|
|
|
// $appendedLoaders[] = $dir; |
136
|
|
|
// } |
137
|
|
|
// } |
138
|
|
|
// } |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
$this->collectServicesByType(DataProviderManager::class, DataProviderInterface::class, 'registerDataProvider'); |
143
|
|
|
|
144
|
|
|
///** |
|
|
|
|
145
|
|
|
// * {@inheritdoc} |
146
|
|
|
// */ |
147
|
|
|
//public function process(ContainerBuilder $container) |
148
|
|
|
//{ |
149
|
|
|
// foreach ($container->findByTag('sculpin.path_configurator') as $tagAttributes) { |
150
|
|
|
// foreach ($tagAttributes as $attributes) { |
151
|
|
|
// $typeParameter = 'sculpin.'.$attributes['type']; |
152
|
|
|
// $parameter = $attributes['parameter']; |
153
|
|
|
// |
154
|
|
|
// if ($container->hasParameter($parameter)) { |
155
|
|
|
// $value = $container->getParameter($parameter); |
156
|
|
|
// if (!is_array($value)) { |
157
|
|
|
// $value = [$value]; |
158
|
|
|
// } |
159
|
|
|
// |
160
|
|
|
// if ($container->hasParameter($typeParameter)) { |
161
|
|
|
// $container->setParameter($typeParameter, array_unique(array_merge($container->getParameter($typeParameter), $this->antify($value)))); |
162
|
|
|
// } else { |
163
|
|
|
// $container->setParameter($typeParameter, array_unique($this->antify($value))); |
164
|
|
|
// } |
165
|
|
|
// } |
166
|
|
|
// } |
167
|
|
|
// } |
168
|
|
|
//} |
169
|
|
|
//} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function loadDefinitionsFromFile(string $serviceConfigFile) |
173
|
|
|
{ |
174
|
|
|
Compiler::loadDefinitions( |
175
|
|
|
$this->getContainerBuilder(), |
176
|
|
|
$this->loadFromFile($serviceConfigFile)['services'] |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// private function antify(array $paths) : array |
|
|
|
|
181
|
|
|
// { |
182
|
|
|
// $matcher = new AntPathMatcher(); |
183
|
|
|
// |
184
|
|
|
// return array_map( |
185
|
|
|
// function ($path) use ($matcher) { |
186
|
|
|
// if ($matcher->isPattern($path)) { |
187
|
|
|
// return $path; |
188
|
|
|
// } |
189
|
|
|
// |
190
|
|
|
// return $path.'/**'; |
191
|
|
|
// }, |
192
|
|
|
// $paths |
193
|
|
|
// ); |
194
|
|
|
// } |
195
|
|
|
} |
196
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.