-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLockBundle.php
More file actions
181 lines (162 loc) · 8.53 KB
/
Copy pathLockBundle.php
File metadata and controls
181 lines (162 loc) · 8.53 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Lock;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Kernel\AbstractBundle;
use Symfony\Component\DependencyInjection\Kernel\RequiredBundle;
use Symfony\Component\DependencyInjection\Kernel\ServicesBundle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Lock\Store\StoreFactory;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* Provides the lock services.
*/
#[RequiredBundle(ServicesBundle::class)]
class LockBundle extends AbstractBundle
{
public function getPath(): string
{
return $this->path ??= __DIR__;
}
public function configure(DefinitionConfigurator $definition): void
{
$definition->rootNode()
->acceptAndWrap(['string'], 'resources')
->canBeDisabled()
->beforeNormalization()
->ifArray()
->then(static function ($v) {
if ($v && !isset($v['resources']) && !isset($v['resource'])) {
$v = ['resources' => $v];
if (\array_key_exists('enabled', $v['resources'])) {
$v['enabled'] = $v['resources']['enabled'];
unset($v['resources']['enabled']);
}
}
return $v;
})
->end()
->children()
->arrayNode('resources', 'resource')
->normalizeKeys(false)
->useAttributeAsKey('name')
->info('Defaults to a "default" resource using the semaphore store when it is supported, the flock store otherwise.')
->acceptAndWrap(['string'], 'default')
->beforeNormalization()
->ifArray()
->then(static function ($v) {
if (!array_is_list($v)) {
return isset($v['service_id']) ? ['default' => $v] : $v;
}
$resources = [];
foreach ($v as $resource) {
[$name, $store] = \is_array($resource) && isset($resource['name'])
? [$resource['name'], $resource['value']]
: ['default', $resource]
;
$resources[] = [$name => \is_array($store) && !array_is_list($store) ? [$store] : $store];
}
return array_merge_recursive([], ...$resources);
})
->end()
->prototype('array')
->info('Each store is a DSN, a store keyword, the id of a service holding a connection, or an array with a "service_id" key and an "advisory" key.')
->performNoDeepMerging()
->acceptAndWrap(['string'])
// acceptAndWrap() doesn't list null as an accepted value on purpose,
// yet the XML loader can yield some and we should convert them to 'null'
->beforeNormalization()->ifNull()->then(static fn () => ['null'])->end()
->beforeNormalization()
->ifTrue(static fn ($v) => \is_array($v) && (isset($v['service_id']) || isset($v['advisory'])))
->then(static fn ($v) => [$v])
->end()
->variablePrototype()
->beforeNormalization()
->ifArray()
->then(static fn ($v) => ['service_id' => $v['service_id'] ?? null, 'advisory' => $v['advisory'] ?? false] + $v)
->end()
->validate()
->ifTrue(static fn ($v) => \is_array($v) && (2 !== \count($v) || !\is_string($v['service_id']) || !\is_bool($v['advisory'])))
->thenInvalid('A lock store must be a string or an array with a "service_id" string and an optional "advisory" boolean, got %s.')
->end()
->end()
->end()
->end()
->end()
;
}
public function loadExtension(array $config, ContainerConfigurator $configurator, ContainerBuilder $container): void
{
if (!$config['enabled']) {
return;
}
if (!$config['resources']) {
$config['resources'] = ['default' => [SemaphoreStore::isSupported() ? 'semaphore' : 'flock']];
}
$configurator->import('Resources/config/lock.php');
if (!interface_exists(DenormalizerInterface::class)) {
$container->removeDefinition('serializer.normalizer.lock_key');
}
foreach ($config['resources'] as $resourceName => $resourceStores) {
if (!$resourceStores) {
continue;
}
// Generate stores
$storeDefinitions = [];
foreach ($resourceStores as $resourceStore) {
if (\in_array($resourceStore, ['flock', 'flock+remove', 'semaphore'], true)) {
$storeDefinitionId = \sprintf('.lock.%s.store', $resourceStore);
$storeDefinitions[] = new Reference($storeDefinitionId);
$container->getDefinition($storeDefinitionId)->addTag('lock.store');
continue;
}
$usedEnvs = [];
$storeDsn = $container->resolveEnvPlaceholders($resourceStore, null, $usedEnvs);
$advisory = false;
if (\is_array($resourceStore)) {
$advisory = $resourceStore['advisory'];
$resourceStore = new Reference($resourceStore['service_id']);
} elseif (!$usedEnvs && !str_contains($resourceStore, ':') && !\in_array($resourceStore, ['flock', 'semaphore', 'in-memory', 'null'], true)) {
$resourceStore = new Reference($resourceStore);
}
$storeDefinition = new Definition(PersistingStoreInterface::class);
$storeDefinition
->setFactory([StoreFactory::class, 'createStore'])
->setArguments($advisory ? [$resourceStore, true] : [$resourceStore])
->addTag('lock.store');
$container->setDefinition($storeDefinitionId = '.lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition);
$storeDefinitions[] = new Reference($storeDefinitionId);
}
// Wrap array of stores with CombinedStore
if (\count($storeDefinitions) > 1) {
$combinedDefinition = new ChildDefinition('lock.store.combined.abstract');
$combinedDefinition->replaceArgument(0, $storeDefinitions);
$container->setDefinition($storeDefinitionId = '.lock.'.$resourceName.'.store.'.$container->hash($resourceStores), $combinedDefinition);
}
// Generate factories for each resource
$factoryDefinition = new ChildDefinition('lock.factory.abstract');
$factoryDefinition->replaceArgument(0, new Reference($storeDefinitionId));
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition);
// provide alias for default resource
if ('default' === $resourceName) {
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false));
$container->setAlias(LockFactory::class, new Alias('lock.factory', false));
} else {
$container->registerAliasForArgument('lock.'.$resourceName.'.factory', LockFactory::class, $resourceName.'.lock.factory', $resourceName);
}
}
}
}