|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Neimheadh\SonataAnnotationBundle\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Neimheadh\SonataAnnotationBundle\DependencyInjection\Compiler\NamespaceLoaderTrait; |
|
|
|
|
|
|
9
|
|
|
use Symfony\Component\Config\FileLocator; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* SonataAnnotationBundle extension. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Marko Kunic <[email protected]> |
|
19
|
|
|
* @author Mathieu Wambre <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class SonataAnnotationExtension extends Extension |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
public const PARAM_ENTITY_NAMESPACE = 'sonata_annotation.config.entity.namespace'; |
|
25
|
|
|
|
|
26
|
|
|
public const PARAM_MENU_USE_NAMESPACE = 'sonata_annotation.config.namespace_as_group'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
* @throws Exception |
|
31
|
|
|
*/ |
|
32
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
33
|
|
|
{ |
|
34
|
|
|
$configuration = new Configuration(); |
|
35
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
36
|
|
|
|
|
37
|
|
|
$this->loadEntityConfiguration( |
|
38
|
|
|
$config['entity'], |
|
39
|
|
|
$container, |
|
40
|
|
|
$config['directory'] ?? null, |
|
41
|
|
|
); |
|
42
|
|
|
$this->loadMenuConfiguration($config['menu'], $container); |
|
43
|
|
|
$this->loadReaderServices($container); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the namespace of the given directory. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $directory The directory. |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|null |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getDirectoryNamespace(string $directory): ?string |
|
54
|
|
|
{ |
|
55
|
|
|
$files = Finder::create() |
|
56
|
|
|
->in($directory) |
|
57
|
|
|
->files() |
|
58
|
|
|
->name('*.php'); |
|
59
|
|
|
$pattern = '/^namespace\s+(.+);/'; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($files as $file) { |
|
62
|
|
|
$namespaceLine = current( |
|
63
|
|
|
preg_grep($pattern, file($file->getPathname())) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if ($namespaceLine) { |
|
67
|
|
|
preg_match($pattern, $namespaceLine, $matches); |
|
68
|
|
|
$namespace = $matches[1]; |
|
69
|
|
|
$subdir = substr($file->getPath(), strlen($directory)); |
|
70
|
|
|
$subNs = str_replace('/', '\\\\', $subdir); |
|
71
|
|
|
return preg_replace("/\\$subNs$/", '', $namespace); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Load bundle entity configuration. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $config Entity configuration. |
|
82
|
|
|
* @param ContainerBuilder $container Container builder. |
|
83
|
|
|
* @param string|null $directory Deprecated directory configuration. |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
private function loadEntityConfiguration( |
|
88
|
|
|
array $config, |
|
89
|
|
|
ContainerBuilder $container, |
|
90
|
|
|
?string $directory |
|
91
|
|
|
): void { |
|
92
|
|
|
if ($directory !== null) { |
|
93
|
|
|
$ns = $this->getDirectoryNamespace($directory); |
|
94
|
|
|
$container->setParameter( |
|
95
|
|
|
self::PARAM_ENTITY_NAMESPACE, |
|
96
|
|
|
$ns ? [$ns] : [] |
|
97
|
|
|
); |
|
98
|
|
|
} else { |
|
99
|
|
|
$container->setParameter( |
|
100
|
|
|
self::PARAM_ENTITY_NAMESPACE, |
|
101
|
|
|
array_map( |
|
102
|
|
|
fn(string $ns) => str_ends_with($ns, '\\') |
|
103
|
|
|
? $ns |
|
104
|
|
|
: sprintf('%s\\', $ns), |
|
105
|
|
|
$config['namespace'] |
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Load bundle menu configuration. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $config Menu configuration. |
|
115
|
|
|
* @param ContainerBuilder $container Container builder. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
private function loadMenuConfiguration( |
|
120
|
|
|
array $config, |
|
121
|
|
|
ContainerBuilder $container |
|
122
|
|
|
): void { |
|
123
|
|
|
$container->setParameter( |
|
124
|
|
|
self::PARAM_MENU_USE_NAMESPACE, |
|
125
|
|
|
$config['namespace_as_group'] |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Load annotation readers. |
|
131
|
|
|
* |
|
132
|
|
|
* @param ContainerBuilder $container Container builder. |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
* @throws Exception |
|
136
|
|
|
*/ |
|
137
|
|
|
private function loadReaderServices(ContainerBuilder $container): void |
|
138
|
|
|
{ |
|
139
|
|
|
$loader = new XmlFileLoader( |
|
140
|
|
|
$container, |
|
141
|
|
|
new FileLocator( |
|
142
|
|
|
__DIR__ . '/../Resources/config' |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
$loader->load('reader.xml'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths