Skip to content

Commit

Permalink
Merge pull request #108 from linaori/fix/lazy-factory-recursion
Browse files Browse the repository at this point in the history
Prevent recursion when the lazy adapter refers to itself
  • Loading branch information
tgalopin authored Sep 12, 2022
2 parents ee16e8e + a1ba2e3 commit ab674be
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Lazy/LazyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function __construct(ContainerInterface $storages)

public function createStorage(string $source, string $storageName)
{
if ($source === $storageName) {
throw new \InvalidArgumentException('The "lazy" adapter source is referring to itself as "'.$source.'", which would lead to infinite recursion.');
}

if (!$this->storages->has($source)) {
throw new \InvalidArgumentException('You have requested a non-existent source storage "'.$source.'" in lazy storage "'.$storageName.'".');
}
Expand Down
55 changes: 55 additions & 0 deletions tests/Lazy/LazyFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\League\FlysystemBundle\Lazy;

use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use League\FlysystemBundle\Lazy\LazyFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class LazyFactoryTest extends TestCase
{
public function testItPreventsInfiniteRecursion(): void
{
$containerBuilder = new ContainerBuilder();
$factory = new LazyFactory($containerBuilder);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "lazy" adapter source is referring to itself as "lazy_storage", which would lead to infinite recursion.');
$factory->createStorage('lazy_storage', 'lazy_storage');
}

public function testItErrorsWhenServiceIsNotAvailable(): void
{
$containerBuilder = new ContainerBuilder();
$factory = new LazyFactory($containerBuilder);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('You have requested a non-existent source storage "source_storage" in lazy storage "lazy_storage".');
$factory->createStorage('source_storage', 'lazy_storage');
}

public function testItReturnsTheRequestedStorageService(): void
{
$actual = new InMemoryFilesystemAdapter();
$lazy = new InMemoryFilesystemAdapter();

$containerBuilder = new ContainerBuilder();
$containerBuilder->set('source_storage', $actual);
$containerBuilder->set('lazy_storage', $lazy);

$factory = new LazyFactory($containerBuilder);

$adapter = $factory->createStorage('source_storage', 'lazy_storage');
self::assertSame($actual, $adapter);
}
}

0 comments on commit ab674be

Please sign in to comment.