Skip to content

Commit ab674be

Browse files
authored
Merge pull request thephpleague#108 from linaori/fix/lazy-factory-recursion
Prevent recursion when the lazy adapter refers to itself
2 parents ee16e8e + a1ba2e3 commit ab674be

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Lazy/LazyFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function __construct(ContainerInterface $storages)
2929

3030
public function createStorage(string $source, string $storageName)
3131
{
32+
if ($source === $storageName) {
33+
throw new \InvalidArgumentException('The "lazy" adapter source is referring to itself as "'.$source.'", which would lead to infinite recursion.');
34+
}
35+
3236
if (!$this->storages->has($source)) {
3337
throw new \InvalidArgumentException('You have requested a non-existent source storage "'.$source.'" in lazy storage "'.$storageName.'".');
3438
}

tests/Lazy/LazyFactoryTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.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 Tests\League\FlysystemBundle\Lazy;
13+
14+
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
15+
use League\FlysystemBundle\Lazy\LazyFactory;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
19+
class LazyFactoryTest extends TestCase
20+
{
21+
public function testItPreventsInfiniteRecursion(): void
22+
{
23+
$containerBuilder = new ContainerBuilder();
24+
$factory = new LazyFactory($containerBuilder);
25+
26+
$this->expectException(\InvalidArgumentException::class);
27+
$this->expectExceptionMessage('The "lazy" adapter source is referring to itself as "lazy_storage", which would lead to infinite recursion.');
28+
$factory->createStorage('lazy_storage', 'lazy_storage');
29+
}
30+
31+
public function testItErrorsWhenServiceIsNotAvailable(): void
32+
{
33+
$containerBuilder = new ContainerBuilder();
34+
$factory = new LazyFactory($containerBuilder);
35+
36+
$this->expectException(\InvalidArgumentException::class);
37+
$this->expectExceptionMessage('You have requested a non-existent source storage "source_storage" in lazy storage "lazy_storage".');
38+
$factory->createStorage('source_storage', 'lazy_storage');
39+
}
40+
41+
public function testItReturnsTheRequestedStorageService(): void
42+
{
43+
$actual = new InMemoryFilesystemAdapter();
44+
$lazy = new InMemoryFilesystemAdapter();
45+
46+
$containerBuilder = new ContainerBuilder();
47+
$containerBuilder->set('source_storage', $actual);
48+
$containerBuilder->set('lazy_storage', $lazy);
49+
50+
$factory = new LazyFactory($containerBuilder);
51+
52+
$adapter = $factory->createStorage('source_storage', 'lazy_storage');
53+
self::assertSame($actual, $adapter);
54+
}
55+
}

0 commit comments

Comments
 (0)