-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathIOConfigurationPass.php
More file actions
125 lines (108 loc) · 5.02 KB
/
Copy pathIOConfigurationPass.php
File metadata and controls
125 lines (108 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace Ibexa\Bundle\IO\DependencyInjection\Compiler;
use ArrayObject;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
/**
* This compiler pass will create the metadata and binarydata IO handlers depending on container configuration.
*
* @todo Refactor into two passes, since they're very very close.
*/
class IOConfigurationPass implements CompilerPassInterface
{
/** @var \Ibexa\Bundle\IO\DependencyInjection\ConfigurationFactory[]|\ArrayObject */
private $metadataHandlerFactories;
/** @var \Ibexa\Bundle\IO\DependencyInjection\ConfigurationFactory[]|\ArrayObject */
private $binarydataHandlerFactories;
public function __construct(
?ArrayObject $metadataHandlerFactories = null,
?ArrayObject $binarydataHandlerFactories = null
) {
$this->metadataHandlerFactories = $metadataHandlerFactories ?? new ArrayObject();
$this->binarydataHandlerFactories = $binarydataHandlerFactories ?? new ArrayObject();
}
/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*
* @throws \LogicException
*/
public function process(ContainerBuilder $container)
{
$ioMetadataHandlers = $container->hasParameter('ibexa.io.metadata_handlers') ?
$container->getParameter('ibexa.io.metadata_handlers') :
[];
$this->processHandlers(
$container,
$container->getDefinition('ibexa.core.io.metadata_handler.registry'),
$ioMetadataHandlers,
$this->metadataHandlerFactories,
'ibexa.core.io.metadata_handler.flysystem.default'
);
$ioBinarydataHandlers = $container->hasParameter('ibexa.io.binarydata_handlers') ?
$container->getParameter('ibexa.io.binarydata_handlers') :
[];
$this->processHandlers(
$container,
$container->getDefinition('ibexa.core.io.binarydata_handler.registry'),
$ioBinarydataHandlers,
$this->binarydataHandlerFactories,
'ibexa.core.io.binarydata_handler.flysystem.default'
);
// Unset parameters that are no longer required ?
}
/**
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param \Symfony\Component\DependencyInjection\Definition $factory The factory service that should receive the list of handlers
* @param array $configuredHandlers Handlers configuration declared via semantic config
* @param \Ibexa\Bundle\IO\DependencyInjection\ConfigurationFactory[]|\ArrayObject $factories Map of alias => handler service id
* @param string $defaultHandler default handler id
*/
protected function processHandlers(
ContainerBuilder $container,
Definition $factory,
array $configuredHandlers,
ArrayObject $factories,
$defaultHandler
) {
$handlers = ['default' => new Reference($defaultHandler)];
foreach ($configuredHandlers as $name => $config) {
$configurationFactory = $this->getFactory($factories, $config['type'], $container);
$parentHandlerId = $configurationFactory->getParentServiceId();
$handlerId = sprintf('%s.%s', $parentHandlerId, $name);
$handlerServiceDefinition = new ChildDefinition($parentHandlerId);
$definition = $container->setDefinition($handlerId, $handlerServiceDefinition);
$configurationFactory->configureHandler($definition, $config);
$handlers[$name] = new Reference($handlerId);
}
$factory->addMethodCall('setHandlersMap', [$handlers]);
}
/**
* Returns from $factories the factory for handler $type.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
* @param \Ibexa\Bundle\IO\DependencyInjection\ConfigurationFactory[]|\ArrayObject $factories
* @param string $type
*
* @return \Ibexa\Bundle\IO\DependencyInjection\ConfigurationFactory
*/
protected function getFactory(ArrayObject $factories, $type, ContainerBuilder $container)
{
if (!isset($factories[$type])) {
throw new InvalidConfigurationException("Unknown handler type $type");
}
if ($factories[$type] instanceof ContainerAwareInterface) {
$factories[$type]->setContainer($container);
}
return $factories[$type];
}
}
class_alias(IOConfigurationPass::class, 'eZ\Bundle\EzPublishIOBundle\DependencyInjection\Compiler\IOConfigurationPass');