|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cypress\PatchManager\Bundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
|
|
11
|
|
|
class PatchManagerExtension extends Extension |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Loads a specific configuration. |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $configs An array of configuration values |
|
17
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \InvalidArgumentException When provided tag is not defined in this extension |
|
20
|
|
|
* |
|
21
|
|
|
* @api |
|
22
|
|
|
*/ |
|
23
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
24
|
|
|
{ |
|
25
|
|
|
$configuration = new Configuration(); |
|
26
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
27
|
|
|
|
|
28
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
29
|
|
|
$loaderHandlers = new Loader\XmlFileLoader( |
|
30
|
|
|
$container, |
|
31
|
|
|
new FileLocator(__DIR__.'/../Resources/config/handlers') |
|
32
|
|
|
); |
|
33
|
|
|
$loader->load('services.xml'); |
|
34
|
|
|
$this->handleGenericConfig($config, $container, $loaderHandlers); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param array $config |
|
39
|
|
|
* @param ContainerBuilder $container |
|
40
|
|
|
* @param Loader\XmlFileLoader $loaderHandlers |
|
41
|
|
|
*/ |
|
42
|
|
|
private function handleGenericConfig( |
|
43
|
|
|
array $config, |
|
44
|
|
|
ContainerBuilder $container, |
|
45
|
|
|
Loader\XmlFileLoader $loaderHandlers |
|
46
|
|
|
): void { |
|
47
|
|
|
if ($config['dispatch_events']) { |
|
48
|
|
|
$patchManagerDefinition = $container->getDefinition('patch_manager'); |
|
49
|
|
|
$patchManagerDefinition->addMethodCall( |
|
50
|
|
|
'setEventDispatcherInterface', |
|
51
|
|
|
[new Reference('event_dispatcher')] |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
if (!is_null($config['alias'])) { |
|
55
|
|
|
$container->setAlias($config['alias'], 'patch_manager'); |
|
56
|
|
|
} |
|
57
|
|
|
if (array_key_exists('state_machine', $config['handlers'])) { |
|
58
|
|
|
$this->handleStateMachine($loaderHandlers, $container); |
|
59
|
|
|
} |
|
60
|
|
|
$container->setParameter('patch_manager.strict_mode', $config['strict_mode']); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Loader\XmlFileLoader $loaderHandlers |
|
65
|
|
|
* @param ContainerBuilder $container |
|
66
|
|
|
*/ |
|
67
|
|
|
private function handleStateMachine(Loader\XmlFileLoader $loaderHandlers, ContainerBuilder $container): void |
|
68
|
|
|
{ |
|
69
|
|
|
if (!interface_exists('Finite\Factory\FactoryInterface')) { |
|
70
|
|
|
$msg = 'If you want to use the patch manager with "op": "sm" you should install '; |
|
71
|
|
|
$msg .= 'the finite library. See https://github.com/yohang/Finite'; |
|
72
|
|
|
|
|
73
|
|
|
throw new \RuntimeException($msg); |
|
74
|
|
|
} |
|
75
|
|
|
$loaderHandlers->load('state_machine.xml'); |
|
76
|
|
|
$smHandlerDefinition = $container->getDefinition('patch_manager.handler.state_machine'); |
|
77
|
|
|
$smHandlerDefinition->addTag('patch_manager.handler'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|