-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathRegisterFilesystemPass.php
More file actions
78 lines (65 loc) · 3.12 KB
/
RegisterFilesystemPass.php
File metadata and controls
78 lines (65 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Setono\SyliusFeedPlugin\DependencyInjection\Compiler;
use InvalidArgumentException;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use RuntimeException;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Webmozart\Assert\Assert;
final class RegisterFilesystemPass implements CompilerPassInterface
{
private const PARAMETERS = ['setono_sylius_feed.storage.feed', 'setono_sylius_feed.storage.feed_tmp'];
public function process(ContainerBuilder $container): void
{
$hasAny = false;
foreach (self::PARAMETERS as $parameter) {
if ($container->hasParameter($parameter)) {
$hasAny = true;
}
}
if (!$hasAny) {
return;
}
foreach (self::PARAMETERS as $parameter) {
$parameterValue = $container->getParameter($parameter);
Assert::string($parameterValue);
if (!$container->hasDefinition($parameterValue)) {
throw new InvalidArgumentException(sprintf('No service definition exists with id "%s"', $parameterValue));
}
$definition = $container->getDefinition($parameterValue);
if ($definition->getClass() === null && $definition instanceof ChildDefinition) {
$definition = $container->getDefinition($definition->getParent());
}
$definitionClass = $definition->getClass();
Assert::notNull($definitionClass);
if (interface_exists(FilesystemInterface::class)) {
if (!is_a($definitionClass, FilesystemInterface::class, true)) {
throw new InvalidDefinitionException(sprintf(
'The config parameter "%s" references a service %s, which is not an instance of %s. Fix this by creating a valid service that implements %s.',
$parameter,
$definitionClass,
FilesystemInterface::class,
FilesystemInterface::class,
));
}
} elseif (interface_exists(FilesystemOperator::class)) {
if (!is_a($definitionClass, FilesystemOperator::class, true)) {
throw new InvalidDefinitionException(sprintf(
'The config parameter "%s" references a service %s, which is not an instance of %s. Fix this by creating a valid service that implements %s.',
$parameter,
$definitionClass,
FilesystemOperator::class,
FilesystemOperator::class,
));
}
} else {
throw new RuntimeException('It looks like both of league/flysystem v1 and v2 are not installed!');
}
$container->setAlias($parameter, $parameterValue);
}
}
}