-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Lock] Include lock component in framework bundle #22113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
@@ -54,6 +55,11 @@ | |
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\Lock\Factory; | ||
use Symfony\Component\Lock\Lock; | ||
use Symfony\Component\Lock\LockInterface; | ||
use Symfony\Component\Lock\Store\StoreFactory; | ||
use Symfony\Component\Lock\StoreInterface; | ||
use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; | ||
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface; | ||
|
@@ -298,6 +304,10 @@ public function load(array $configs, ContainerBuilder $container) | |
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader); | ||
} | ||
|
||
if ($this->isConfigEnabled($container, $config['lock'])) { | ||
$this->registerLockConfiguration($config['lock'], $container, $loader); | ||
} | ||
|
||
if ($this->isConfigEnabled($container, $config['web_link'])) { | ||
if (!class_exists(HttpHeaderSerializer::class)) { | ||
throw new LogicException('WebLink support cannot be enabled as the WebLink component is not installed.'); | ||
|
@@ -1672,6 +1682,84 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild | |
} | ||
} | ||
|
||
private function registerLockConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) | ||
{ | ||
$loader->load('lock.xml'); | ||
|
||
foreach ($config['resources'] as $resourceName => $resourceStores) { | ||
if (0 === count($resourceStores)) { | ||
continue; | ||
} | ||
|
||
// Generate stores | ||
$storeDefinitions = array(); | ||
foreach ($resourceStores as $storeDsn) { | ||
$storeDsn = $container->resolveEnvPlaceholders($storeDsn, null, $usedEnvs); | ||
switch (true) { | ||
case 'flock' === $storeDsn: | ||
$storeDefinition = new Reference('lock.store.flock'); | ||
break; | ||
case 'semaphore' === $storeDsn: | ||
$storeDefinition = new Reference('lock.store.semaphore'); | ||
break; | ||
case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn): | ||
if (!$container->hasDefinition($connectionDefinitionId = $container->hash($storeDsn))) { | ||
$connectionDefinition = new Definition(\stdClass::class); | ||
$connectionDefinition->setPublic(false); | ||
$connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection')); | ||
$connectionDefinition->setArguments(array($storeDsn)); | ||
$container->setDefinition($connectionDefinitionId, $connectionDefinition); | ||
} | ||
|
||
$storeDefinition = new Definition(StoreInterface::class); | ||
$storeDefinition->setPublic(false); | ||
$storeDefinition->setFactory(array(StoreFactory::class, 'createStore')); | ||
$storeDefinition->setArguments(array(new Reference($connectionDefinitionId))); | ||
|
||
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition); | ||
|
||
$storeDefinition = new Reference($storeDefinitionId); | ||
break; | ||
default: | ||
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName)); | ||
} | ||
|
||
$storeDefinitions[] = $storeDefinition; | ||
} | ||
|
||
// Wrap array of stores with CombinedStore | ||
if (count($storeDefinitions) > 1) { | ||
$combinedDefinition = new ChildDefinition('lock.store.combined.abstract'); | ||
$combinedDefinition->replaceArgument(0, $storeDefinitions); | ||
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition); | ||
} else { | ||
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0], false)); | ||
} | ||
|
||
// Generate factories for each resource | ||
$factoryDefinition = new ChildDefinition('lock.factory.abstract'); | ||
$factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store')); | ||
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition); | ||
|
||
// Generate services for lock instances | ||
$lockDefinition = new Definition(Lock::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
$lockDefinition->setPublic(false); | ||
$lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock')); | ||
$lockDefinition->setArguments(array($resourceName)); | ||
$container->setDefinition('lock.'.$resourceName, $lockDefinition); | ||
|
||
// provide alias for default resource | ||
if ('default' === $resourceName) { | ||
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store', false)); | ||
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false)); | ||
$container->setAlias('lock', new Alias('lock.'.$resourceName, false)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing FQCN aliases that could be use by autowiring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
$container->setAlias(StoreInterface::class, new Alias('lock.store', false)); | ||
$container->setAlias(Factory::class, new Alias('lock.factory', false)); | ||
$container->setAlias(LockInterface::class, new Alias('lock', false)); | ||
} | ||
} | ||
} | ||
|
||
private function registerCacheConfiguration(array $config, ContainerBuilder $container) | ||
{ | ||
$version = substr(str_replace('/', '-', base64_encode(hash('sha256', uniqid(mt_rand(), true), true))), 0, 22); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<defaults public="false" /> | ||
|
||
<service id="lock.store.flock" class="Symfony\Component\Lock\Store\FlockStore" /> | ||
|
||
<service id="lock.store.semaphore" class="Symfony\Component\Lock\Store\SemaphoreStore" /> | ||
|
||
<service id="lock.store.memcached.abstract" class="Symfony\Component\Lock\Store\MemcachedStore" abstract="true"> | ||
<argument /> <!-- Memcached connection service --> | ||
</service> | ||
|
||
<service id="lock.store.redis.abstract" class="Symfony\Component\Lock\Store\RedisStore" abstract="true"> | ||
<argument /> <!-- Redis connection service --> | ||
</service> | ||
|
||
<service id="lock.store.combined.abstract" class="Symfony\Component\Lock\Store\CombinedStore" abstract="true"> | ||
<argument /> <!-- List of stores --> | ||
<argument type="service" id="lock.strategy.majority" /> <!-- Strategy --> | ||
</service> | ||
|
||
<service id="lock.strategy.majority" class="Symfony\Component\Lock\Strategy\ConsensusStrategy" /> | ||
|
||
<service id="lock.factory.abstract" class="Symfony\Component\Lock\Factory" abstract="true"> | ||
<tag name="monolog.logger" channel="lock" /> | ||
<argument /> <!-- Store --> | ||
<call method="setLogger"> | ||
<argument type="service" id="logger" on-invalid="ignore" /> | ||
</call> | ||
</service> | ||
|
||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
<framework:config> | ||
<framework:lock/> | ||
</framework:config> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:framework="http://symfony.com/schema/dic/symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
|
||
|
||
<parameters> | ||
<parameter key="env(REDIS_URL)">redis://paas.com</parameter> | ||
</parameters> | ||
|
||
<framework:config> | ||
<framework:lock> | ||
<framework:resource name="foo">semaphore</framework:resource> | ||
<framework:resource name="bar">flock</framework:resource> | ||
<framework:resource name="baz">semaphore</framework:resource> | ||
<framework:resource name="baz">flock</framework:resource> | ||
<framework:resource name="qux">%env(REDIS_URL)%</framework:resource> | ||
</framework:lock> | ||
</framework:config> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
framework: | ||
lock: ~ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
parameters: | ||
env(REDIS_DSN): redis://paas.com | ||
|
||
framework: | ||
lock: | ||
foo: semaphore | ||
bar: flock | ||
baz: [semaphore, flock] | ||
qux: "%env(REDIS_DSN)%" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the definition should me made private
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed