diff --git a/src/Symfony/Component/Lock/Store/FlockStore.php b/src/Symfony/Component/Lock/Store/FlockStore.php index 76e2b355c1854..0fd55c8456c06 100644 --- a/src/Symfony/Component/Lock/Store/FlockStore.php +++ b/src/Symfony/Component/Lock/Store/FlockStore.php @@ -42,8 +42,12 @@ public function __construct(string $lockPath = null) if (null === $lockPath) { $lockPath = sys_get_temp_dir(); } - if (!is_dir($lockPath) || !is_writable($lockPath)) { - throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $lockPath)); + if (!is_dir($lockPath)) { + if (false === @mkdir($lockPath, 0777, true) && !is_dir($lockPath)) { + throw new InvalidArgumentException(sprintf('The FlockStore directory "%s" does not exists and cannot be created.', $lockPath)); + } + } elseif (!is_writable($lockPath)) { + throw new InvalidArgumentException(sprintf('The FlockStore directory "%s" is not writable.', $lockPath)); } $this->lockPath = $lockPath; diff --git a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php index d879c6ac256ff..dd6a34a0aab89 100644 --- a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php @@ -32,10 +32,10 @@ protected function getStore(): PersistingStoreInterface return new FlockStore(); } - public function testConstructWhenRepositoryDoesNotExist() + public function testConstructWhenRepositoryCannotBeCreated() { $this->expectException('Symfony\Component\Lock\Exception\InvalidArgumentException'); - $this->expectExceptionMessage('The directory "/a/b/c/d/e" is not writable.'); + $this->expectExceptionMessage('The FlockStore directory "/a/b/c/d/e" does not exists and cannot be created.'); if (!getenv('USER') || 'root' === getenv('USER')) { $this->markTestSkipped('This test will fail if run under superuser'); } @@ -46,7 +46,7 @@ public function testConstructWhenRepositoryDoesNotExist() public function testConstructWhenRepositoryIsNotWriteable() { $this->expectException('Symfony\Component\Lock\Exception\InvalidArgumentException'); - $this->expectExceptionMessage('The directory "/" is not writable.'); + $this->expectExceptionMessage('The FlockStore directory "/" is not writable.'); if (!getenv('USER') || 'root' === getenv('USER')) { $this->markTestSkipped('This test will fail if run under superuser'); } @@ -54,6 +54,15 @@ public function testConstructWhenRepositoryIsNotWriteable() new FlockStore('/'); } + public function testConstructWithSubdir() + { + if (!getenv('USER') || 'root' === getenv('USER')) { + $this->markTestSkipped('This test will fail if run under superuser'); + } + + new FlockStore(sys_get_temp_dir().'/sf-flock'); + } + public function testSaveSanitizeName() { $store = $this->getStore();