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 0b9c2e6

Browse filesBrowse files
committed
Reduce lock service visibility
1 parent 776200f commit 0b9c2e6
Copy full SHA for 0b9c2e6

File tree

10 files changed

+183
-85
lines changed
Filter options

10 files changed

+183
-85
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
@@ -55,8 +55,11 @@
5555
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
5656
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
5757
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
58+
use Symfony\Component\Lock\Factory;
5859
use Symfony\Component\Lock\Lock;
60+
use Symfony\Component\Lock\LockInterface;
5961
use Symfony\Component\Lock\Store\StoreFactory;
62+
use Symfony\Component\Lock\StoreInterface;
6063
use Symfony\Component\PropertyAccess\PropertyAccessor;
6164
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
6265
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -1679,19 +1682,10 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
16791682
}
16801683
}
16811684

1682-
/**
1683-
* Loads lock configuration.
1684-
*
1685-
* @param array $config
1686-
* @param ContainerBuilder $container
1687-
* @param XmlFileLoader $loader
1688-
*/
16891685
private function registerLockConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
16901686
{
16911687
$loader->load('lock.xml');
16921688

1693-
$container->getDefinition('lock.store.flock')->replaceArgument(0, sys_get_temp_dir());
1694-
16951689
foreach ($config['resources'] as $resourceName => $resourceStores) {
16961690
if (0 === count($resourceStores)) {
16971691
continue;
@@ -1709,24 +1703,22 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
17091703
$storeDefinition = new Reference('lock.store.semaphore');
17101704
break;
17111705
case $usedEnvs || preg_match('#^[a-z]++://#', $storeDsn):
1712-
if (!$container->hasDefinition($connectionDefinitionId = md5($storeDsn))) {
1706+
if (!$container->hasDefinition($connectionDefinitionId = $container->hash($storeDsn))) {
17131707
$connectionDefinition = new Definition(\stdClass::class);
17141708
$connectionDefinition->setPublic(false);
17151709
$connectionDefinition->setFactory(array(StoreFactory::class, 'createConnection'));
17161710
$connectionDefinition->setArguments(array($storeDsn));
17171711
$container->setDefinition($connectionDefinitionId, $connectionDefinition);
17181712
}
17191713

1720-
$storeDefinition = new Definition(\stdClass::class);
1714+
$storeDefinition = new Definition(StoreInterface::class);
1715+
$storeDefinition->setPublic(false);
17211716
$storeDefinition->setFactory(array(StoreFactory::class, 'createStore'));
17221717
$storeDefinition->setArguments(array(new Reference($connectionDefinitionId)));
17231718

1724-
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.md5($storeDsn), $storeDefinition);
1719+
$container->setDefinition($storeDefinitionId = 'lock.'.$resourceName.'.store.'.$container->hash($storeDsn), $storeDefinition);
17251720

17261721
$storeDefinition = new Reference($storeDefinitionId);
1727-
break;
1728-
case $usedEnvs:
1729-
17301722
break;
17311723
default:
17321724
throw new InvalidArgumentException(sprintf('Lock store DSN "%s" is not valid in resource "%s"', $storeDsn, $resourceName));
@@ -1741,26 +1733,29 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
17411733
$combinedDefinition->replaceArgument(0, $storeDefinitions);
17421734
$container->setDefinition('lock.'.$resourceName.'.store', $combinedDefinition);
17431735
} else {
1744-
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0]));
1736+
$container->setAlias('lock.'.$resourceName.'.store', new Alias((string) $storeDefinitions[0], false));
17451737
}
17461738

17471739
// Generate factories for each resource
17481740
$factoryDefinition = new ChildDefinition('lock.factory.abstract');
17491741
$factoryDefinition->replaceArgument(0, new Reference('lock.'.$resourceName.'.store'));
1750-
$factoryDefinition->setPublic(true);
17511742
$container->setDefinition('lock.'.$resourceName.'.factory', $factoryDefinition);
17521743

17531744
// Generate services for lock instances
17541745
$lockDefinition = new Definition(Lock::class);
1746+
$lockDefinition->setPublic(false);
17551747
$lockDefinition->setFactory(array(new Reference('lock.'.$resourceName.'.factory'), 'createLock'));
17561748
$lockDefinition->setArguments(array($resourceName));
17571749
$container->setDefinition('lock.'.$resourceName, $lockDefinition);
17581750

17591751
// provide alias for default resource
17601752
if ('default' === $resourceName) {
1761-
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store'));
1762-
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory'));
1763-
$container->setAlias('lock', new Alias('lock.'.$resourceName));
1753+
$container->setAlias('lock.store', new Alias('lock.'.$resourceName.'.store', false));
1754+
$container->setAlias('lock.factory', new Alias('lock.'.$resourceName.'.factory', false));
1755+
$container->setAlias('lock', new Alias('lock.'.$resourceName, false));
1756+
$container->setAlias(StoreInterface::class, new Alias('lock.store', false));
1757+
$container->setAlias(Factory::class, new Alias('lock.factory', false));
1758+
$container->setAlias(LockInterface::class, new Alias('lock', false));
17641759
}
17651760
}
17661761
}

‎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, defaults to the system's 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/Store/StoreFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Lock/Store/StoreFactory.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class StoreFactory
2323
public static function createConnection($dsn, array $options = array())
2424
{
2525
if (!is_string($dsn)) {
26-
throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
26+
throw new InvalidArgumentException(sprintf('The %s() method expects argument #1 to be string, %s given.', __METHOD__, gettype($dsn)));
2727
}
2828
if (0 === strpos($dsn, 'redis://')) {
2929
return RedisStore::createConnection($dsn, $options);
@@ -35,12 +35,16 @@ public static function createConnection($dsn, array $options = array())
3535
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
3636
}
3737

38+
/**
39+
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Memcached $connection
40+
*
41+
* @return RedisStore|MemcachedStore
42+
*/
3843
public static function createStore($connection)
3944
{
4045
if ($connection instanceof \Redis || $connection instanceof \RedisArray || $connection instanceof \RedisCluster || $connection instanceof \Predis\Client) {
4146
return new RedisStore($connection);
4247
}
43-
4448
if ($connection instanceof \Memcached) {
4549
return new MemcachedStore($connection);
4650
}

‎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.