Skip to content

Commit 069151e

Browse files
committed
Refactor - create filesystems with a factory and provider will retreive the service which is the filesystem inc config
1 parent 4d0d79a commit 069151e

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

src/DependencyInjection/CompilerPass/FlysystemCompilerPass.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Silverback\ApiComponentsBundle\DependencyInjection\CompilerPass;
1515

1616
use League\Flysystem\Filesystem;
17+
use Silverback\ApiComponentsBundle\Flysystem\FilesystemFactory;
1718
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider;
1819
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -33,13 +34,16 @@ public function process(ContainerBuilder $container): void
3334
$definition = new Definition();
3435
$definition
3536
->setClass(Filesystem::class)
36-
->setFactory([new Reference(FilesystemProvider::class), 'getFilesystem'])
37+
->setFactory([new Reference(FilesystemFactory::class), 'create'])
3738
->setArguments([
3839
$attributes['alias'],
3940
$attributes['config'] ?? []
4041
]);
4142
$serviceName = sprintf('api_components.filesystem.%s', $attributes['alias']);
42-
$container->setDefinition($serviceName, $definition)->addTag(FilesystemProvider::FILESYSTEM_TAG, [ 'alias' => $attributes['alias'] ]);
43+
$container
44+
->setDefinition($serviceName, $definition)
45+
->addTag(FilesystemProvider::FILESYSTEM_TAG, [ 'alias' => $attributes['alias'] ])
46+
;
4347
}
4448
}
4549
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Silverback API Components Bundle Project
5+
*
6+
* (c) Daniel West <daniel@silverback.is>
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+
declare(strict_types=1);
13+
14+
namespace Silverback\ApiComponentsBundle\Flysystem;
15+
16+
use League\Flysystem\Filesystem;
17+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18+
use Symfony\Component\DependencyInjection\ServiceLocator;
19+
20+
/**
21+
* @author Daniel West <daniel@silverback.is>
22+
*/
23+
class FilesystemFactory
24+
{
25+
public function __construct(private readonly ServiceLocator $adapters)
26+
{}
27+
28+
/**
29+
* @throws RuntimeException
30+
*/
31+
public function create(string $name, array $config = []): Filesystem
32+
{
33+
return new Filesystem($this->adapters->get($name), $config);
34+
}
35+
}

src/Flysystem/FilesystemProvider.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace Silverback\ApiComponentsBundle\Flysystem;
1515

1616
use League\Flysystem\Filesystem;
17+
use Psr\Container\ContainerExceptionInterface;
18+
use Psr\Container\NotFoundExceptionInterface;
1719
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1820
use Symfony\Component\DependencyInjection\ServiceLocator;
1921

@@ -24,23 +26,17 @@
2426
class FilesystemProvider
2527
{
2628
public const FILESYSTEM_ADAPTER_TAG = 'silverback.api_components.filesystem_adapter';
27-
public const FILESYSTEM_TAG = 'silverback.api_components.filesystem_adapter';
29+
public const FILESYSTEM_TAG = 'silverback.api_components.filesystem';
2830

29-
private array $filesystems = [];
30-
31-
public function __construct(private readonly ServiceLocator $adapters)
31+
public function __construct(private readonly ServiceLocator $filesystems)
3232
{}
3333

3434
/**
35-
* @throws RuntimeException
35+
* @throws ContainerExceptionInterface
36+
* @throws NotFoundExceptionInterface
3637
*/
37-
private function createFilesystem(string $name, array $config = []): Filesystem
38-
{
39-
return $this->filesystems[$name] = new Filesystem($this->adapters->get($name), $config);
40-
}
41-
42-
public function getFilesystem(string $name, array $config = []): ?Filesystem
38+
public function getFilesystem(string $name): ?Filesystem
4339
{
44-
return $this->filesystems[$name] ?? $this->createFilesystem($name,$config);
40+
return $this->filesystems->get($name);
4541
}
4642
}

src/Resources/config/services.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
use Silverback\ApiComponentsBundle\Factory\User\Mailer\WelcomeEmailFactory;
9494
use Silverback\ApiComponentsBundle\Factory\User\UserFactory;
9595
use Silverback\ApiComponentsBundle\Filter\OrSearchFilter;
96+
use Silverback\ApiComponentsBundle\Flysystem\FilesystemFactory;
9697
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider;
9798
use Silverback\ApiComponentsBundle\Form\Type\User\ChangePasswordType;
9899
use Silverback\ApiComponentsBundle\Form\Type\User\NewEmailAddressType;
@@ -387,9 +388,13 @@
387388
->tag('doctrine.repository_service');
388389

389390
$services
390-
->set(FilesystemProvider::class)
391+
->set(FilesystemFactory::class)
391392
->args([tagged_locator(FilesystemProvider::FILESYSTEM_ADAPTER_TAG, 'alias')]);
392393

394+
$services
395+
->set(FilesystemProvider::class)
396+
->args([tagged_locator(FilesystemProvider::FILESYSTEM_TAG, 'alias')]);
397+
393398
$services
394399
->set(FlysystemDataLoader::class)
395400
->args(

0 commit comments

Comments
 (0)