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 68954c3

Browse filesBrowse files
committed
New config format
1 parent 5e9ad2e commit 68954c3
Copy full SHA for 68954c3

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;
@@ -854,36 +855,35 @@ private function addLockSection(ArrayNodeDefinition $rootNode)
854855
->children()
855856
->arrayNode('lock')
856857
->info('Lock configuration')
857-
->canBeEnabled()
858+
->{!class_exists(FullStack::class) && class_exists(Lock::class) ? 'canBeDisabled' : 'canBeEnabled'}()
859+
->beforeNormalization()
860+
->ifString()->then(function ($v) { return array('enabled' => true, 'resources' => $v); })
861+
->end()
862+
->beforeNormalization()
863+
->ifTrue(function ($v) { return is_array($v) && !isset($v['resources']); })
864+
->then(function ($v) {
865+
$e = $v['enabled'];
866+
unset($v['enabled']);
867+
868+
return array('enabled' => $e, 'resources' => $v);
869+
})
870+
->end()
871+
->addDefaultsIfNotSet()
872+
->fixXmlConfig('resource')
858873
->children()
859-
->arrayNode('flock')
860-
->canBeDisabled()
861-
->end()
862-
->arrayNode('semaphore')
863-
->{class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'canBeDisabled' : 'canBeEnabled'}()
864-
->end()
865-
->arrayNode('memcached')
866-
->canBeEnabled()
867-
->children()
868-
->arrayNode('hosts')
869-
->beforeNormalization()
870-
->ifTrue(function ($v) { return !is_array($v) && null !== $v; })
871-
->then(function ($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
872-
->end()
873-
->prototype('scalar')->end()
874-
->end()
874+
->arrayNode('resources')
875+
->requiresAtLeastOneElement()
876+
->defaultValue(array('default' => array(class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphore' : 'flock')))
877+
->beforeNormalization()
878+
->ifString()->then(function ($v) { return array('default' => $v); })
875879
->end()
876-
->end()
877-
->arrayNode('redis')
878-
->canBeEnabled()
879-
->children()
880-
->arrayNode('hosts')
881-
->beforeNormalization()
882-
->ifTrue(function ($v) { return !is_array($v) && null !== $v; })
883-
->then(function ($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
884-
->end()
885-
->prototype('scalar')->end()
886-
->end()
880+
->beforeNormalization()
881+
->ifTrue(function ($v) { return is_array($v) && array_keys($v) === range(0, count($v) - 1); })
882+
->then(function ($v) { return array('default' => $v); })
883+
->end()
884+
->prototype('array')
885+
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
886+
->prototype('scalar')->end()
887887
->end()
888888
->end()
889889
->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
@@ -29,6 +29,7 @@
2929
use Symfony\Component\DependencyInjection\ContainerBuilder;
3030
use Symfony\Component\DependencyInjection\ContainerInterface;
3131
use Symfony\Component\DependencyInjection\Definition;
32+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
3233
use Symfony\Component\DependencyInjection\Exception\LogicException;
3334
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3435
use Symfony\Component\DependencyInjection\Reference;
@@ -44,6 +45,8 @@
4445
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
4546
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
4647
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
48+
use Symfony\Component\Lock\Lock;
49+
use Symfony\Component\Lock\Store\StoreFactory;
4750
use Symfony\Component\PropertyAccess\PropertyAccessor;
4851
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
4952
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -1479,69 +1482,75 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
14791482

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

1482-
// configure connectable stores
1483-
foreach (array('redis', 'memcached') as $store) {
1484-
if ($this->isConfigEnabled($container, $config[$store]) && count($config[$store]['hosts']) > 0) {
1485-
/** @var Reference[] $hostsDefinitions */
1486-
$hostsDefinitions = array();
1487-
foreach ($config[$store]['hosts'] as $host) {
1488-
$definition = new ChildDefinition('lock.store.'.$store.'.abstract');
1489-
1490-
// generate a service connection for the host
1491-
$container->resolveEnvPlaceholders($host, null, $usedEnvs);
1492-
if ($usedEnvs || preg_match('#^[a-z]++://#', $host)) {
1493-
$dsn = $host;
1485+
foreach ($config['resources'] as $resourceName => $resourceStores) {
1486+
if (0 === count($resourceStores)) {
1487+
continue;
1488+
}
14941489

1495-
if (!$container->hasDefinition($host = 'lock.connection.'.$store.'.'.md5($dsn))) {
1490+
// Generate stores
1491+
$storeDefinitions = array();
1492+
foreach ($resourceStores as $storeDsn) {
1493+
$storeDsn = $container->resolveEnvPlaceholders($storeDsn, null, $usedEnvs);
1494+
switch (true) {
1495+
case 'flock' === $storeDsn:
1496+
$storeDefinition = new Reference('lock.store.flock');
1497+
break;
1498+
case 'semaphore' === $storeDsn:
1499+
$storeDefinition = new Reference('lock.store.semaphore');
1500+
break;
1501+
case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn):
1502+
if (!$container->hasDefinition($connectionDefinitionId = md5($storeDsn))) {
14961503
$connectionDefinition = new Definition(\stdClass::class);
14971504
$connectionDefinition->setPublic(false);
1498-
$connectionDefinition->setFactory(array($container->getDefinition($definition->getParent())->getClass(), 'createConnection'));
1499-
$connectionDefinition->setArguments(array($dsn));
1500-
$container->setDefinition($host, $connectionDefinition);
1505+
$connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection'));
1506+
$connectionDefinition->setArguments(array($storeDsn));
1507+
$container->setDefinition($connectionDefinitionId, $connectionDefinition);
15011508
}
1502-
}
15031509

1504-
$definition->replaceArgument(0, new Reference($host));
1505-
$container->setDefinition($name = 'lock.store.'.$store.'.'.md5($host), $definition);
1510+
$storeDefinition = new Definition(\stdClass::class);
1511+
$storeDefinition->setFactory(array(StoreFactory::class, 'createStore'));
1512+
$storeDefinition->setArguments(array(new Reference($connectionDefinitionId)));
15061513

1507-
$hostsDefinitions[] = new Reference($name);
1508-
}
1514+
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.md5($storeDsn), $storeDefinition);
1515+
1516+
$storeDefinition = new Reference($storeDefinitionId);
1517+
break;
1518+
case $usedEnvs:
15091519

1510-
if (count($hostsDefinitions) > 1) {
1511-
$definition = new ChildDefinition('lock.store.combined.abstract');
1512-
$definition->replaceArgument(0, $hostsDefinitions);
1513-
$container->setDefinition('lock.store.'.$store, $definition);
1514-
} else {
1515-
$container->setAlias('lock.store.'.$store, new Alias((string) $hostsDefinitions[0]));
1520+
break;
1521+
default:
1522+
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName));
15161523
}
1517-
}
1518-
}
15191524

1520-
// wrap non blocking store with retry mechanism
1521-
foreach (array('redis', 'memcached') as $store) {
1522-
if ($container->has($name = 'lock.store.'.$store)) {
1523-
$container->register($name.'.retry', 'Symfony\\Component\\Lock\\Store\\RetryTillSaveStore')
1524-
->setDecoratedService($name)
1525-
->addArgument(new Reference($name.'.retry.inner'))
1526-
->setPublic(false)
1527-
;
1525+
$storeDefinitions[] = $storeDefinition;
15281526
}
1529-
}
15301527

1531-
// generate factory for activated stores
1532-
$hasAlias = false;
1533-
// Order of stores matters: First enabled will be used in the default "lock.factory"
1534-
foreach (array('redis', 'memcached', 'semaphore', 'flock') as $store) {
1535-
if ($this->isConfigEnabled($container, $config[$store]) && $container->has('lock.store.'.$store)) {
1536-
$definition = new ChildDefinition('lock.factory.abstract');
1537-
$definition->replaceArgument(0, new Reference('lock.store.'.$store));
1538-
$definition->setPublic(true);
1539-
$container->setDefinition('lock.factory.'.$store, $definition);
1528+
// Wrap array of stores with CombinedStore
1529+
if (count($storeDefinitions) > 1) {
1530+
$combinedDefinition = new ChildDefinition('lock.store.combined.abstract');
1531+
$combinedDefinition->replaceArgument(0, $storeDefinitions);
1532+
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition);
1533+
} else {
1534+
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0]));
1535+
}
15401536

1541-
if (!$hasAlias) {
1542-
$container->setAlias('lock.factory', new Alias('lock.factory.'.$store));
1543-
$hasAlias = true;
1544-
}
1537+
// Generate factories for each resource
1538+
$factoryDefinition = new ChildDefinition('lock.factory.abstract');
1539+
$factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store'));
1540+
$factoryDefinition->setPublic(true);
1541+
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition);
1542+
1543+
// Generate services for lock instances
1544+
$lockDefinition = new Definition(Lock::class);
1545+
$lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock'));
1546+
$lockDefinition->setArguments(array($resourceName));
1547+
$container->setDefinition('lock.'.$resourceName, $lockDefinition);
1548+
1549+
// provide alias for default resource
1550+
if ('default' === $resourceName) {
1551+
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store'));
1552+
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory'));
1553+
$container->setAlias('lock', new Alias('lock.'.$resourceName));
15451554
}
15461555
}
15471556
}

‎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
@@ -28,6 +28,7 @@
2828
<xsd:element name="property-info" type="property_info" minOccurs="0" maxOccurs="1" />
2929
<xsd:element name="cache" type="cache" minOccurs="0" maxOccurs="1" />
3030
<xsd:element name="workflow" type="workflow" minOccurs="0" maxOccurs="unbounded" />
31+
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3132
</xsd:choice>
3233

3334
<xsd:attribute name="http-method-override" type="xsd:boolean" />
@@ -288,4 +289,19 @@
288289
<xsd:enumeration value="workflow" />
289290
</xsd:restriction>
290291
</xsd:simpleType>
292+
293+
<xsd:complexType name="lock">
294+
<xsd:sequence>
295+
<xsd:element name="resource" type="lock_resource" minOccurs="1" maxOccurs="unbounded" />
296+
</xsd:sequence>
297+
<xsd:attribute name="enabled" type="xsd:boolean" />
298+
</xsd:complexType>
299+
300+
<xsd:complexType name="lock_resource">
301+
<xsd:simpleContent>
302+
<xsd:extension base="xsd:string">
303+
<xsd:attribute name="name" type="xsd:string" />
304+
</xsd:extension>
305+
</xsd:simpleContent>
306+
</xsd:complexType>
291307
</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
{
@@ -339,6 +340,14 @@ protected static function getBundleDefaultConfig()
339340
'web_link' => array(
340341
'enabled' => !class_exists(FullStack::class),
341342
),
343+
'lock' => array(
344+
'enabled' => !class_exists(FullStack::class),
345+
'resources' => array(
346+
'default' => array(
347+
class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphore' : 'flock',
348+
),
349+
),
350+
),
342351
);
343352
}
344353
}
+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",
+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.