Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6d4e433

Browse filesBrowse files
committed
New config format
1 parent f039018 commit 6d4e433
Copy full SHA for 6d4e433

File tree

11 files changed

+209
-80
lines changed
Filter options

11 files changed

+209
-80
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+28-28Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1919
use Symfony\Component\Config\Definition\ConfigurationInterface;
2020
use Symfony\Component\Form\Form;
21+
use Symfony\Component\Lock\Lock;
2122
use Symfony\Component\Lock\Store\SemaphoreStore;
2223
use Symfony\Component\Serializer\Serializer;
2324
use Symfony\Component\Translation\Translator;
@@ -883,36 +884,35 @@ private function addLockSection(ArrayNodeDefinition $rootNode)
883884
->children()
884885
->arrayNode('lock')
885886
->info('Lock configuration')
886-
->canBeEnabled()
887+
->{!class_exists(FullStack::class) && class_exists(Lock::class) ? 'canBeDisabled' : 'canBeEnabled'}()
888+
->beforeNormalization()
889+
->ifString()->then(function ($v) { return array('enabled' => true, 'resources' => $v); })
890+
->end()
891+
->beforeNormalization()
892+
->ifTrue(function ($v) { return is_array($v) && !isset($v['resources']); })
893+
->then(function ($v) {
894+
$e = $v['enabled'];
895+
unset($v['enabled']);
896+
897+
return array('enabled' => $e, 'resources' => $v);
898+
})
899+
->end()
900+
->addDefaultsIfNotSet()
901+
->fixXmlConfig('resource')
887902
->children()
888-
->arrayNode('flock')
889-
->canBeDisabled()
890-
->end()
891-
->arrayNode('semaphore')
892-
->{class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'canBeDisabled' : 'canBeEnabled'}()
893-
->end()
894-
->arrayNode('memcached')
895-
->canBeEnabled()
896-
->children()
897-
->arrayNode('hosts')
898-
->beforeNormalization()
899-
->ifTrue(function ($v) { return !is_array($v) && null !== $v; })
900-
->then(function ($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
901-
->end()
902-
->prototype('scalar')->end()
903-
->end()
903+
->arrayNode('resources')
904+
->requiresAtLeastOneElement()
905+
->defaultValue(array('default' => array(class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphore' : 'flock')))
906+
->beforeNormalization()
907+
->ifString()->then(function ($v) { return array('default' => $v); })
904908
->end()
905-
->end()
906-
->arrayNode('redis')
907-
->canBeEnabled()
908-
->children()
909-
->arrayNode('hosts')
910-
->beforeNormalization()
911-
->ifTrue(function ($v) { return !is_array($v) && null !== $v; })
912-
->then(function ($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
913-
->end()
914-
->prototype('scalar')->end()
915-
->end()
909+
->beforeNormalization()
910+
->ifTrue(function ($v) { return is_array($v) && array_keys($v) === range(0, count($v) - 1); })
911+
->then(function ($v) { return array('default' => $v); })
912+
->end()
913+
->prototype('array')
914+
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
915+
->prototype('scalar')->end()
916916
->end()
917917
->end()
918918
->end()

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+60-51Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Symfony\Component\DependencyInjection\ContainerInterface;
3838
use Symfony\Component\DependencyInjection\Definition;
3939
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
40+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
4041
use Symfony\Component\DependencyInjection\Exception\LogicException;
4142
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
4243
use Symfony\Component\DependencyInjection\Reference;
@@ -53,6 +54,8 @@
5354
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
5455
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
5556
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
57+
use Symfony\Component\Lock\Lock;
58+
use Symfony\Component\Lock\Store\StoreFactory;
5659
use Symfony\Component\PropertyAccess\PropertyAccessor;
5760
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
5861
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -1680,69 +1683,75 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
16801683

16811684
$container->getDefinition('lock.store.flock')->replaceArgument(0, sys_get_temp_dir());
16821685

1683-
// configure connectable stores
1684-
foreach (array('redis', 'memcached') as $store) {
1685-
if ($this->isConfigEnabled($container, $config[$store]) && count($config[$store]['hosts']) > 0) {
1686-
/** @var Reference[] $hostsDefinitions */
1687-
$hostsDefinitions = array();
1688-
foreach ($config[$store]['hosts'] as $host) {
1689-
$definition = new ChildDefinition('lock.store.'.$store.'.abstract');
1690-
1691-
// generate a service connection for the host
1692-
$container->resolveEnvPlaceholders($host, null, $usedEnvs);
1693-
if ($usedEnvs || preg_match('#^[a-z]++://#', $host)) {
1694-
$dsn = $host;
1686+
foreach ($config['resources'] as $resourceName => $resourceStores) {
1687+
if (0 === count($resourceStores)) {
1688+
continue;
1689+
}
16951690

1696-
if (!$container->hasDefinition($host = 'lock.connection.'.$store.'.'.md5($dsn))) {
1691+
// Generate stores
1692+
$storeDefinitions = array();
1693+
foreach ($resourceStores as $storeDsn) {
1694+
$storeDsn = $container->resolveEnvPlaceholders($storeDsn, null, $usedEnvs);
1695+
switch (true) {
1696+
case 'flock' === $storeDsn:
1697+
$storeDefinition = new Reference('lock.store.flock');
1698+
break;
1699+
case 'semaphore' === $storeDsn:
1700+
$storeDefinition = new Reference('lock.store.semaphore');
1701+
break;
1702+
case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn):
1703+
if (!$container->hasDefinition($connectionDefinitionId = md5($storeDsn))) {
16971704
$connectionDefinition = new Definition(\stdClass::class);
16981705
$connectionDefinition->setPublic(false);
1699-
$connectionDefinition->setFactory(array($container->getDefinition($definition->getParent())->getClass(), 'createConnection'));
1700-
$connectionDefinition->setArguments(array($dsn));
1701-
$container->setDefinition($host, $connectionDefinition);
1706+
$connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection'));
1707+
$connectionDefinition->setArguments(array($storeDsn));
1708+
$container->setDefinition($connectionDefinitionId, $connectionDefinition);
17021709
}
1703-
}
17041710

1705-
$definition->replaceArgument(0, new Reference($host));
1706-
$container->setDefinition($name = 'lock.store.'.$store.'.'.md5($host), $definition);
1711+
$storeDefinition = new Definition(\stdClass::class);
1712+
$storeDefinition->setFactory(array(StoreFactory::class, 'createStore'));
1713+
$storeDefinition->setArguments(array(new Reference($connectionDefinitionId)));
17071714

1708-
$hostsDefinitions[] = new Reference($name);
1709-
}
1715+
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.md5($storeDsn), $storeDefinition);
1716+
1717+
$storeDefinition = new Reference($storeDefinitionId);
1718+
break;
1719+
case $usedEnvs:
17101720

1711-
if (count($hostsDefinitions) > 1) {
1712-
$definition = new ChildDefinition('lock.store.combined.abstract');
1713-
$definition->replaceArgument(0, $hostsDefinitions);
1714-
$container->setDefinition('lock.store.'.$store, $definition);
1715-
} else {
1716-
$container->setAlias('lock.store.'.$store, new Alias((string) $hostsDefinitions[0]));
1721+
break;
1722+
default:
1723+
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName));
17171724
}
1718-
}
1719-
}
17201725

1721-
// wrap non blocking store with retry mechanism
1722-
foreach (array('redis', 'memcached') as $store) {
1723-
if ($container->has($name = 'lock.store.'.$store)) {
1724-
$container->register($name.'.retry', 'Symfony\\Component\\Lock\\Store\\RetryTillSaveStore')
1725-
->setDecoratedService($name)
1726-
->addArgument(new Reference($name.'.retry.inner'))
1727-
->setPublic(false)
1728-
;
1726+
$storeDefinitions[] = $storeDefinition;
17291727
}
1730-
}
17311728

1732-
// generate factory for activated stores
1733-
$hasAlias = false;
1734-
// Order of stores matters: First enabled will be used in the default "lock.factory"
1735-
foreach (array('redis', 'memcached', 'semaphore', 'flock') as $store) {
1736-
if ($this->isConfigEnabled($container, $config[$store]) && $container->has('lock.store.'.$store)) {
1737-
$definition = new ChildDefinition('lock.factory.abstract');
1738-
$definition->replaceArgument(0, new Reference('lock.store.'.$store));
1739-
$definition->setPublic(true);
1740-
$container->setDefinition('lock.factory.'.$store, $definition);
1729+
// Wrap array of stores with CombinedStore
1730+
if (count($storeDefinitions) > 1) {
1731+
$combinedDefinition = new ChildDefinition('lock.store.combined.abstract');
1732+
$combinedDefinition->replaceArgument(0, $storeDefinitions);
1733+
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition);
1734+
} else {
1735+
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0]));
1736+
}
17411737

1742-
if (!$hasAlias) {
1743-
$container->setAlias('lock.factory', new Alias('lock.factory.'.$store));
1744-
$hasAlias = true;
1745-
}
1738+
// Generate factories for each resource
1739+
$factoryDefinition = new ChildDefinition('lock.factory.abstract');
1740+
$factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store'));
1741+
$factoryDefinition->setPublic(true);
1742+
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition);
1743+
1744+
// Generate services for lock instances
1745+
$lockDefinition = new Definition(Lock::class);
1746+
$lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock'));
1747+
$lockDefinition->setArguments(array($resourceName));
1748+
$container->setDefinition('lock.'.$resourceName, $lockDefinition);
1749+
1750+
// provide alias for default resource
1751+
if ('default' === $resourceName) {
1752+
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store'));
1753+
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory'));
1754+
$container->setAlias('lock', new Alias('lock.'.$resourceName));
17461755
}
17471756
}
17481757
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<argument type="service" id="lock.strategy.majority" /> <!-- Strategy -->
2626
</service>
2727

28-
<service id="lock.strategy.majority" class="Symfony\Component\Lock\Strategy\MajorityStrategy" public="false" />
28+
<service id="lock.strategy.majority" class="Symfony\Component\Lock\Strategy\ConsensusStrategy" public="false" />
2929

3030
<service id="lock.factory.abstract" class="Symfony\Component\Lock\Factory" abstract="true" public="false">
3131
<tag name="monolog.logger" channel="lock" />

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<xsd:element name="property-info" type="property_info" minOccurs="0" maxOccurs="1" />
3030
<xsd:element name="cache" type="cache" minOccurs="0" maxOccurs="1" />
3131
<xsd:element name="workflow" type="workflow" minOccurs="0" maxOccurs="unbounded" />
32+
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3233
</xsd:choice>
3334

3435
<xsd:attribute name="http-method-override" type="xsd:boolean" />
@@ -296,4 +297,19 @@
296297
<xsd:enumeration value="workflow" />
297298
</xsd:restriction>
298299
</xsd:simpleType>
300+
301+
<xsd:complexType name="lock">
302+
<xsd:sequence>
303+
<xsd:element name="resource" type="lock_resource" minOccurs="1" maxOccurs="unbounded" />
304+
</xsd:sequence>
305+
<xsd:attribute name="enabled" type="xsd:boolean" />
306+
</xsd:complexType>
307+
308+
<xsd:complexType name="lock_resource">
309+
<xsd:simpleContent>
310+
<xsd:extension base="xsd:string">
311+
<xsd:attribute name="name" type="xsd:string" />
312+
</xsd:extension>
313+
</xsd:simpleContent>
314+
</xsd:complexType>
299315
</xsd:schema>

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FullStack;
1717
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1818
use Symfony\Component\Config\Definition\Processor;
19+
use Symfony\Component\Lock\Store\SemaphoreStore;
1920

2021
class ConfigurationTest extends TestCase
2122
{
@@ -343,6 +344,14 @@ protected static function getBundleDefaultConfig()
343344
'web_link' => array(
344345
'enabled' => !class_exists(FullStack::class),
345346
),
347+
'lock' => array(
348+
'enabled' => !class_exists(FullStack::class),
349+
'resources' => array(
350+
'default' => array(
351+
class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphore' : 'flock',
352+
),
353+
),
354+
),
346355
);
347356
}
348357
}
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config>
9+
<framework:lock/>
10+
</framework:config>
11+
</container>
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
9+
<parameters>
10+
<parameter key="env(REDIS_URL)">redis://paas.com</parameter>
11+
</parameters>
12+
13+
<framework:config>
14+
<framework:lock>
15+
<framework:resource name="foo">semaphore</framework:resource>
16+
<framework:resource name="bar">flock</framework:resource>
17+
<framework:resource name="baz">semaphore</framework:resource>
18+
<framework:resource name="baz">flock</framework:resource>
19+
<framework:resource name="qux">%env(REDIS_URL)%</framework:resource>
20+
</framework:lock>
21+
</framework:config>
22+
</container>
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
lock: ~
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
env(REDIS_DSN): redis://paas.com
3+
4+
framework:
5+
lock:
6+
foo: semaphore
7+
bar: flock
8+
baz: [semaphore, flock]
9+
qux: "%env(REDIS_DSN)%"

‎src/Symfony/Bundle/FrameworkBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"symfony/workflow": "~3.3|~4.0",
5555
"symfony/yaml": "~3.2|~4.0",
5656
"symfony/property-info": "~3.3|~4.0",
57+
"symfony/lock": "~3.4|~4.0",
5758
"symfony/web-link": "~3.3|~4.0",
5859
"doctrine/annotations": "~1.0",
5960
"phpdocumentor/reflection-docblock": "^3.0|^4.0",
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Component\Lock\Store;
13+
14+
use Symfony\Component\Lock\Exception\InvalidArgumentException;
15+
16+
/**
17+
* StoreFactory create stores and connections.
18+
*
19+
* @author Jérémy Derussé <jeremy@derusse.com>
20+
*/
21+
class StoreFactory
22+
{
23+
public static function createConnection($dsn, array $options = array())
24+
{
25+
if (!is_string($dsn)) {
26+
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
27+
}
28+
if (0 === strpos($dsn, 'redis://')) {
29+
return RedisStore::createConnection($dsn, $options);
30+
}
31+
if (0 === strpos($dsn, 'memcached://')) {
32+
return MemcachedStore::createConnection($dsn, $options);
33+
}
34+
35+
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
36+
}
37+
38+
public static function createStore($connection)
39+
{
40+
if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client) {
41+
return new RedisStore($connection);
42+
}
43+
44+
if ($connection instanceof \Memcached) {
45+
return new MemcachedStore($connection);
46+
}
47+
48+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', get_class($connection)));
49+
}
50+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.