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 06342f4

Browse filesBrowse files
committed
Reduce lock service visibility
1 parent 6d4e433 commit 06342f4
Copy full SHA for 06342f4

File tree

9 files changed

+177
-83
lines changed
Filter options

9 files changed

+177
-83
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+15-20Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
5555
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
5656
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
57+
use Symfony\Component\Lock\Factory;
5758
use Symfony\Component\Lock\Lock;
59+
use Symfony\Component\Lock\LockInterface;
5860
use Symfony\Component\Lock\Store\StoreFactory;
61+
use Symfony\Component\Lock\StoreInterface;
5962
use Symfony\Component\PropertyAccess\PropertyAccessor;
6063
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
6164
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -1670,19 +1673,10 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
16701673
}
16711674
}
16721675

1673-
/**
1674-
* Loads lock configuration.
1675-
*
1676-
* @param array $config
1677-
* @param ContainerBuilder $container
1678-
* @param XmlFileLoader $loader
1679-
*/
16801676
private function registerLockConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
16811677
{
16821678
$loader->load('lock.xml');
16831679

1684-
$container->getDefinition('lock.store.flock')->replaceArgument(0, sys_get_temp_dir());
1685-
16861680
foreach ($config['resources'] as $resourceName => $resourceStores) {
16871681
if (0 === count($resourceStores)) {
16881682
continue;
@@ -1700,24 +1694,22 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
17001694
$storeDefinition = new Reference('lock.store.semaphore');
17011695
break;
17021696
case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn):
1703-
if (!$container->hasDefinition($connectionDefinitionId = md5($storeDsn))) {
1697+
if (!$container->hasDefinition($connectionDefinitionId = $container->hash($storeDsn))) {
17041698
$connectionDefinition = new Definition(\stdClass::class);
17051699
$connectionDefinition->setPublic(false);
17061700
$connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection'));
17071701
$connectionDefinition->setArguments(array($storeDsn));
17081702
$container->setDefinition($connectionDefinitionId, $connectionDefinition);
17091703
}
17101704

1711-
$storeDefinition = new Definition(\stdClass::class);
1705+
$storeDefinition = new Definition(StoreInterface::class);
1706+
$storeDefinition->setPublic(false);
17121707
$storeDefinition->setFactory(array(StoreFactory::class, 'createStore'));
17131708
$storeDefinition->setArguments(array(new Reference($connectionDefinitionId)));
17141709

1715-
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.md5($storeDsn), $storeDefinition);
1710+
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition);
17161711

17171712
$storeDefinition = new Reference($storeDefinitionId);
1718-
break;
1719-
case $usedEnvs:
1720-
17211713
break;
17221714
default:
17231715
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName));
@@ -1732,26 +1724,29 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
17321724
$combinedDefinition->replaceArgument(0, $storeDefinitions);
17331725
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition);
17341726
} else {
1735-
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0]));
1727+
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0], false));
17361728
}
17371729

17381730
// Generate factories for each resource
17391731
$factoryDefinition = new ChildDefinition('lock.factory.abstract');
17401732
$factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store'));
1741-
$factoryDefinition->setPublic(true);
17421733
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition);
17431734

17441735
// Generate services for lock instances
17451736
$lockDefinition = new Definition(Lock::class);
1737+
$lockDefinition->setPublic(false);
17461738
$lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock'));
17471739
$lockDefinition->setArguments(array($resourceName));
17481740
$container->setDefinition('lock.'.$resourceName, $lockDefinition);
17491741

17501742
// provide alias for default resource
17511743
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));
1744+
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store', false));
1745+
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false));
1746+
$container->setAlias('lock', new Alias('lock.'.$resourceName, false));
1747+
$container->setAlias(StoreInterface::class, new Alias('lock.store', false));
1748+
$container->setAlias(Factory::class, new Alias('lock.factory', false));
1749+
$container->setAlias(LockInterface::class, new Alias('lock', false));
17551750
}
17561751
}
17571752
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.xml
+8-9Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8+
<defaults public="false" />
89

9-
<service id="lock.store.flock" class="Symfony\Component\Lock\Store\FlockStore" public="false">
10-
<argument /> <!-- lock directory -->
11-
</service>
10+
<service id="lock.store.flock" class="Symfony\Component\Lock\Store\FlockStore" />
1211

13-
<service id="lock.store.semaphore" class="Symfony\Component\Lock\Store\SemaphoreStore" public="false" />
12+
<service id="lock.store.semaphore" class="Symfony\Component\Lock\Store\SemaphoreStore" />
1413

15-
<service id="lock.store.memcached.abstract" class="Symfony\Component\Lock\Store\MemcachedStore" abstract="true" public="false">
14+
<service id="lock.store.memcached.abstract" class="Symfony\Component\Lock\Store\MemcachedStore" abstract="true">
1615
<argument /> <!-- Memcached connection service -->
1716
</service>
1817

19-
<service id="lock.store.redis.abstract" class="Symfony\Component\Lock\Store\RedisStore" abstract="true" public="false">
18+
<service id="lock.store.redis.abstract" class="Symfony\Component\Lock\Store\RedisStore" abstract="true">
2019
<argument /> <!-- Redis connection service -->
2120
</service>
2221

23-
<service id="lock.store.combined.abstract" class="Symfony\Component\Lock\Store\CombinedStore" abstract="true" public="false">
22+
<service id="lock.store.combined.abstract" class="Symfony\Component\Lock\Store\CombinedStore" abstract="true">
2423
<argument /> <!-- List of stores -->
2524
<argument type="service" id="lock.strategy.majority" /> <!-- Strategy -->
2625
</service>
2726

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

30-
<service id="lock.factory.abstract" class="Symfony\Component\Lock\Factory" abstract="true" public="false">
29+
<service id="lock.factory.abstract" class="Symfony\Component\Lock\Factory" abstract="true">
3130
<tag name="monolog.logger" channel="lock" />
3231
<argument /> <!-- Store -->
3332
<call method="setLogger">

‎src/Symfony/Component/Console/Command/LockableTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/LockableTrait.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private function lock($name = null, $blocking = false)
4646
if (SemaphoreStore::isSupported($blocking)) {
4747
$store = new SemaphoreStore();
4848
} else {
49-
$store = new FlockStore(sys_get_temp_dir());
49+
$store = new FlockStore();
5050
}
5151

5252
$this->lock = (new Factory($store))->createLock($name ?: $this->getName());

‎src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/LockableTraitTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testLockReturnsFalseIfAlreadyLockedByAnotherCommand()
4444
if (SemaphoreStore::isSupported(false)) {
4545
$store = new SemaphoreStore();
4646
} else {
47-
$store = new FlockStore(sys_get_temp_dir());
47+
$store = new FlockStore();
4848
}
4949

5050
$lock = (new Factory($store))->createLock($command->getName());

‎src/Symfony/Component/Lock/Store/FlockStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/FlockStore.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ class FlockStore implements StoreInterface
3232
private $lockPath;
3333

3434
/**
35-
* @param string $lockPath the directory to store the lock
35+
* @param string|null $lockPath the directory to store the lock. Default values will use temporary directory
3636
*
3737
* @throws LockStorageException If the lock directory could not be created or is not writable
3838
*/
39-
public function __construct($lockPath)
39+
public function __construct($lockPath = null)
4040
{
41+
if (null === $lockPath) {
42+
$lockPath = sys_get_temp_dir();
43+
}
4144
if (!is_dir($lockPath) || !is_writable($lockPath)) {
4245
throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $lockPath));
4346
}

‎src/Symfony/Component/Lock/Store/MemcachedStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/MemcachedStore.php
+49-48Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,15 @@ public function __construct(\Memcached $memcached, $initialTtl = 300)
6767
* - 'memcached://user:pass@localhost?weight=33'
6868
* - array(array('localhost', 11211, 33))
6969
*
70-
* @param string $dsn
71-
* @param array $options See self::$defaultConnectionOptions
70+
* @param string $dsn A server or A DSN
71+
* @param array $options An array of options
7272
*
7373
* @return \Memcached
7474
*
75-
* @throws \ErrorEception When invalid options or dsn are provided
75+
* @throws \ErrorEception When invalid options or server are provided
7676
*/
77-
public static function createConnection($dsn, array $options = array())
77+
public static function createConnection($server, array $options = array())
7878
{
79-
if (0 !== strpos($dsn, 'memcached://')) {
80-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s does not start with "memcached://"', $dsn));
81-
}
8279
if (!static::isSupported()) {
8380
throw new InvalidArgumentException('Memcached extension is required');
8481
}
@@ -88,10 +85,46 @@ public static function createConnection($dsn, array $options = array())
8885
$client = new \Memcached($options['persistent_id']);
8986
$username = $options['username'];
9087
$password = $options['password'];
91-
unset($options['persistent_id'], $options['username'], $options['password']);
92-
$options = array_change_key_case($options, CASE_UPPER);
88+
89+
// parse any DSN in $server
90+
if (is_string($server)) {
91+
if (0 !== strpos($server, 'memcached://')) {
92+
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s does not start with "memcached://"', $server));
93+
}
94+
$params = preg_replace_callback('#^memcached://(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
95+
if (!empty($m[1])) {
96+
list($username, $password) = explode(':', $m[1], 2) + array(1 => null);
97+
}
98+
99+
return 'file://';
100+
}, $server);
101+
if (false === $params = parse_url($params)) {
102+
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $server));
103+
}
104+
if (!isset($params['host']) && !isset($params['path'])) {
105+
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $server));
106+
}
107+
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
108+
$params['weight'] = $m[1];
109+
$params['path'] = substr($params['path'], 0, -strlen($m[0]));
110+
}
111+
$params += array(
112+
'host' => isset($params['host']) ? $params['host'] : $params['path'],
113+
'port' => isset($params['host']) ? 11211 : null,
114+
'weight' => 0,
115+
);
116+
if (isset($params['query'])) {
117+
parse_str($params['query'], $query);
118+
$params += $query;
119+
$options = $query + $options;
120+
}
121+
122+
$server = array($params['host'], $params['port'], $params['weight']);
123+
}
93124

94125
// set client's options
126+
unset($options['persistent_id'], $options['username'], $options['password'], $options['weight']);
127+
$options = array_change_key_case($options, CASE_UPPER);
95128
$client->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
96129
$client->setOption(\Memcached::OPT_NO_BLOCK, false);
97130
if (!array_key_exists('LIBKETAMA_COMPATIBLE', $options) && !array_key_exists(\Memcached::OPT_LIBKETAMA_COMPATIBLE, $options)) {
@@ -111,36 +144,6 @@ public static function createConnection($dsn, array $options = array())
111144
}
112145
$client->setOptions($options);
113146

114-
// parse any DSN in $servers
115-
$params = preg_replace_callback('#^memcached://(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
116-
if (!empty($m[1])) {
117-
list($username, $password) = explode(':', $m[1], 2) + array(1 => null);
118-
}
119-
120-
return 'file://';
121-
}, $dsn);
122-
if (false === $params = parse_url($params)) {
123-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $dsn));
124-
}
125-
if (!isset($params['host']) && !isset($params['path'])) {
126-
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: %s', $dsn));
127-
}
128-
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
129-
$params['weight'] = $m[1];
130-
$params['path'] = substr($params['path'], 0, -strlen($m[0]));
131-
}
132-
$params += array(
133-
'host' => isset($params['host']) ? $params['host'] : $params['path'],
134-
'port' => isset($params['host']) ? 11211 : null,
135-
'weight' => 0,
136-
);
137-
if (isset($params['query'])) {
138-
parse_str($params['query'], $query);
139-
$params += $query;
140-
}
141-
142-
$servers = array(array($params['host'], $params['port'], $params['weight']));
143-
144147
// set client's servers, taking care of persistent connections
145148
if (!$client->isPristine()) {
146149
$oldServers = array();
@@ -149,22 +152,20 @@ public static function createConnection($dsn, array $options = array())
149152
}
150153

151154
$newServers = array();
152-
foreach ($servers as $server) {
153-
if (1 < count($server)) {
154-
$server = array_values($server);
155-
unset($server[2]);
156-
$server[1] = (int) $server[1];
157-
}
158-
$newServers[] = $server;
155+
if (1 < count($server)) {
156+
$server = array_values($server);
157+
unset($server[2]);
158+
$server[1] = (int) $server[1];
159159
}
160+
$newServers[] = $server;
160161

161162
if ($oldServers !== $newServers) {
162163
// before resetting, ensure $servers is valid
163-
$client->addServers($servers);
164+
$client->addServers(array($server));
164165
$client->resetServerList();
165166
}
166167
}
167-
$client->addServers($servers);
168+
$client->addServers(array($server));
168169

169170
if (null !== $username || null !== $password) {
170171
if (!method_exists($client, 'setSaslAuthData')) {

‎src/Symfony/Component/Lock/Store/RedisStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/RedisStore.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __construct($redisClient, $initialTtl = 300.0)
6666
* @param string $dsn
6767
* @param array $options See self::$defaultConnectionOptions
6868
*
69-
* @throws InvalidArgumentException When the DSN is invalid.
69+
* @throws InvalidArgumentException When the DSN is invalid
7070
*
7171
* @return \Redis|\Predis\Client According to the "class" option
7272
*/

‎src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FlockStoreTest extends AbstractStoreTest
2626
*/
2727
protected function getStore()
2828
{
29-
return new FlockStore(sys_get_temp_dir());
29+
return new FlockStore();
3030
}
3131

3232
/**

0 commit comments

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