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 2c0db9f

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

File tree

5 files changed

+149
-81
lines changed
Filter options

5 files changed

+149
-81
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+22-28Lines changed: 22 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,29 @@ 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) { $e = $v['enabled']; unset($v['enabled']); return array('enabled' => $e, 'resources' => $v); })
865+
->end()
866+
->addDefaultsIfNotSet()
858867
->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()
868+
->arrayNode('resources')
869+
->requiresAtLeastOneElement()
870+
->defaultValue(array('default' => array(class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphore' : 'flock')))
871+
->beforeNormalization()
872+
->ifString()->then(function ($v) { return array('default' => $v); })
875873
->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()
874+
->beforeNormalization()
875+
->ifTrue(function($v) { return is_array($v) && array_keys($v) === range(0, count($v) - 1); })
876+
->then(function ($v) { return array('default' => $v); })
877+
->end()
878+
->prototype('array')
879+
->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end()
880+
->prototype('scalar')->end()
887881
->end()
888882
->end()
889883
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+64-52Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1717
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1818
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
19+
use Symfony\Component\Cache\Adapter\AbstractAdapter;
1920
use Symfony\Component\Cache\Adapter\AdapterInterface;
2021
use Symfony\Component\Cache\Adapter\ArrayAdapter;
2122
use Symfony\Component\Config\FileLocator;
@@ -29,6 +30,7 @@
2930
use Symfony\Component\DependencyInjection\ContainerBuilder;
3031
use Symfony\Component\DependencyInjection\ContainerInterface;
3132
use Symfony\Component\DependencyInjection\Definition;
33+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
3234
use Symfony\Component\DependencyInjection\Exception\LogicException;
3335
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3436
use Symfony\Component\DependencyInjection\Reference;
@@ -44,6 +46,10 @@
4446
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
4547
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
4648
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
49+
use Symfony\Component\Lock\Lock;
50+
use Symfony\Component\Lock\Store\MemcachedStore;
51+
use Symfony\Component\Lock\Store\RedisStore;
52+
use Symfony\Component\Lock\Store\StoreFactory;
4753
use Symfony\Component\PropertyAccess\PropertyAccessor;
4854
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
4955
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -1479,69 +1485,75 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
14791485

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

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;
1488+
foreach ($config['resources'] as $resourceName => $resourceStores) {
1489+
if (0 === count($resourceStores)) {
1490+
continue;
1491+
}
14941492

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

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

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

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]));
1523+
break;
1524+
default:
1525+
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName));
15161526
}
1517-
}
1518-
}
15191527

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-
;
1528+
$storeDefinitions[] = $storeDefinition;
15281529
}
1529-
}
15301530

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);
1531+
// Wrap array of stores with CombinedStore
1532+
if (count($storeDefinitions) > 1) {
1533+
$combinedDefinition = new ChildDefinition('lock.store.combined.abstract');
1534+
$combinedDefinition->replaceArgument(0, $storeDefinitions);
1535+
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition);
1536+
} else {
1537+
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0]));
1538+
}
15401539

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

‎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/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
}
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerAwareTrait;
16+
use Psr\Log\NullLogger;
17+
use Symfony\Component\Lock\Exception\InvalidArgumentException;
18+
19+
/**
20+
* StoreFactory create stores and connections
21+
*
22+
* @author Jérémy Derussé <jeremy@derusse.com>
23+
*/
24+
class StoreFactory
25+
{
26+
public static function createConnection($dsn, array $options = [])
27+
{
28+
if (!is_string($dsn)) {
29+
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
30+
}
31+
if (0 === strpos($dsn, 'redis://')) {
32+
return RedisStore::createConnection($dsn, $options);
33+
}
34+
if (0 === strpos($dsn, 'memcached://')) {
35+
return MemcachedStore::createConnection($dsn, $options);
36+
}
37+
38+
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
39+
}
40+
41+
public static function createStore($connection)
42+
{
43+
if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client) {
44+
return new RedisStore($connection);
45+
}
46+
47+
if ($connection instanceof \Memcached) {
48+
return new MemcachedStore($connection);
49+
}
50+
51+
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', get_class($connection)));
52+
}
53+
}

0 commit comments

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