|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\DependencyInjection\CompilerPass; |
|
15
|
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Compiler pass for automatic registration of generators. |
|
22
|
|
|
* |
|
23
|
|
|
* This compiler pass finds all services tagged with 'cecil.generator' |
|
24
|
|
|
* and registers them with the GeneratorManager service. |
|
25
|
|
|
* It supports priority-based ordering of generators. |
|
26
|
|
|
*/ |
|
27
|
|
|
class GeneratorPass implements CompilerPassInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function process(ContainerBuilder $container): void |
|
33
|
|
|
{ |
|
34
|
|
|
// Si le GeneratorManager n'existe pas, on ne fait rien |
|
35
|
|
|
if (!$container->has('Cecil\Generator\GeneratorManager')) { |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$definition = $container->findDefinition('Cecil\Generator\GeneratorManager'); |
|
40
|
|
|
$taggedServices = $container->findTaggedServiceIds('cecil.generator'); |
|
41
|
|
|
|
|
42
|
|
|
// Tri des générateurs par priorité |
|
43
|
|
|
$generators = []; |
|
44
|
|
|
foreach ($taggedServices as $id => $tags) { |
|
45
|
|
|
foreach ($tags as $attributes) { |
|
46
|
|
|
$priority = $attributes['priority'] ?? 0; |
|
47
|
|
|
$generators[$priority][] = $id; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Tri par priorité décroissante |
|
52
|
|
|
krsort($generators); |
|
53
|
|
|
|
|
54
|
|
|
// Enregistrement des générateurs dans le manager |
|
55
|
|
|
foreach ($generators as $priority => $ids) { |
|
56
|
|
|
foreach ($ids as $id) { |
|
57
|
|
|
$definition->addMethodCall('addGenerator', [ |
|
58
|
|
|
new Reference($id), |
|
59
|
|
|
$priority |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|