|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\DependencyInjection\CompilerPass\GeneratorPass; |
|
17
|
|
|
use Cecil\DependencyInjection\CompilerPass\StepPass; |
|
18
|
|
|
use Cecil\DependencyInjection\CompilerPass\TwigExtensionPass; |
|
19
|
|
|
use Symfony\Component\Config\FileLocator; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Container Builder for Cecil. |
|
25
|
|
|
* |
|
26
|
|
|
* This class is responsible for building and configuring the dependency injection container. |
|
27
|
|
|
* It loads service definitions from YAML files and registers compiler passes for automatic |
|
28
|
|
|
* service discovery and configuration. |
|
29
|
|
|
*/ |
|
30
|
|
|
class ContainerBuilder |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Builds and configures the dependency injection container. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array<string, mixed> $parameters Additional parameters to set in the container |
|
36
|
|
|
* @return SymfonyContainerBuilder The configured container |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function build(array $parameters = []): SymfonyContainerBuilder |
|
39
|
|
|
{ |
|
40
|
|
|
$container = new SymfonyContainerBuilder(); |
|
41
|
|
|
|
|
42
|
|
|
// Paramètres par défaut |
|
43
|
|
|
$defaultParams = [ |
|
44
|
|
|
'cecil.verbosity' => 0, |
|
45
|
|
|
'cecil.debug' => false, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
// Merge et définition des paramètres |
|
49
|
|
|
foreach (array_merge($defaultParams, $parameters) as $key => $value) { |
|
50
|
|
|
$container->setParameter($key, $value); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Déterminer le chemin de configuration |
|
54
|
|
|
$configPath = self::getConfigPath(); |
|
55
|
|
|
|
|
56
|
|
|
// Chargement des services depuis le fichier YAML |
|
57
|
|
|
$loader = new YamlFileLoader( |
|
58
|
|
|
$container, |
|
59
|
|
|
new FileLocator($configPath) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$loader->load('services.yaml'); |
|
64
|
|
|
} catch (\Exception $e) { |
|
65
|
|
|
throw new \RuntimeException( |
|
66
|
|
|
sprintf('Unable to load service configuration: %s', $e->getMessage()), |
|
67
|
|
|
0, |
|
68
|
|
|
$e |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Enregistrement des compiler passes |
|
73
|
|
|
$container->addCompilerPass(new GeneratorPass()); |
|
74
|
|
|
$container->addCompilerPass(new StepPass()); |
|
75
|
|
|
$container->addCompilerPass(new TwigExtensionPass()); |
|
76
|
|
|
|
|
77
|
|
|
// Compilation du container |
|
78
|
|
|
$container->compile(); |
|
79
|
|
|
|
|
80
|
|
|
return $container; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Determines the configuration path based on the execution context. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string The path to the configuration directory |
|
87
|
|
|
*/ |
|
88
|
|
|
private static function getConfigPath(): string |
|
89
|
|
|
{ |
|
90
|
|
|
// Si exécuté depuis un PHAR |
|
91
|
|
|
if (defined('PHAR_DIR')) { |
|
92
|
|
|
return PHAR_DIR . '/config'; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Sinon, chemin relatif depuis le répertoire src |
|
96
|
|
|
return __DIR__ . '/../../config'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Creates a cached container for production use. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $cacheDir Directory where the cached container should be stored |
|
103
|
|
|
* @param array<string, mixed> $parameters Additional parameters |
|
104
|
|
|
* @return SymfonyContainerBuilder The container |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function buildCached(string $cacheDir, array $parameters = []): SymfonyContainerBuilder |
|
107
|
|
|
{ |
|
108
|
|
|
$cacheFile = $cacheDir . '/container.php'; |
|
109
|
|
|
|
|
110
|
|
|
// Si le cache existe et est valide, on l'utilise |
|
111
|
|
|
if (file_exists($cacheFile) && !($parameters['cecil.debug'] ?? false)) { |
|
112
|
|
|
require_once $cacheFile; |
|
113
|
|
|
$containerClass = '\\Cecil\\DependencyInjection\\Cached\\CachedContainer'; |
|
114
|
|
|
if (class_exists($containerClass)) { |
|
115
|
|
|
return new $containerClass(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Sinon, on construit et on met en cache |
|
120
|
|
|
$container = self::build($parameters); |
|
121
|
|
|
|
|
122
|
|
|
// Sauvegarde du container compilé |
|
123
|
|
|
if (!is_dir($cacheDir)) { |
|
124
|
|
|
mkdir($cacheDir, 0777, true); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Cette fonctionnalité nécessiterait l'ajout de symfony/config pour le dumping |
|
128
|
|
|
// Pour l'instant, on retourne simplement le container |
|
129
|
|
|
return $container; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|