|
15 | 15 | use League\Flysystem\FilesystemOperator;
|
16 | 16 | use League\Flysystem\FilesystemReader;
|
17 | 17 | use League\Flysystem\FilesystemWriter;
|
| 18 | +use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter; |
18 | 19 | use League\FlysystemBundle\Adapter\AdapterDefinitionFactory;
|
| 20 | +use League\FlysystemBundle\Exception\MissingPackageException; |
19 | 21 | use League\FlysystemBundle\Lazy\LazyFactory;
|
20 | 22 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
21 | 23 | use Symfony\Component\DependencyInjection\Definition;
|
@@ -63,16 +65,29 @@ private function createStoragesDefinitions(array $config, ContainerBuilder $cont
|
63 | 65 | // Create adapter definition
|
64 | 66 | if ($adapter = $definitionFactory->createDefinition($storageConfig['adapter'], $storageConfig['options'], $storageConfig['directory_visibility'] ?? null)) {
|
65 | 67 | // Native adapter
|
66 |
| - $container->setDefinition('flysystem.adapter.'.$storageName, $adapter)->setPublic(false); |
| 68 | + $container->setDefinition($id = 'flysystem.adapter.'.$storageName, $adapter)->setPublic(false); |
67 | 69 | } else {
|
68 | 70 | // Custom adapter
|
69 |
| - $container->setAlias('flysystem.adapter.'.$storageName, $storageConfig['adapter'])->setPublic(false); |
| 71 | + $container->setAlias($id = 'flysystem.adapter.'.$storageName, $storageConfig['adapter'])->setPublic(false); |
| 72 | + } |
| 73 | + |
| 74 | + // Create ReadOnly adapter |
| 75 | + if ($storageConfig['read_only']) { |
| 76 | + if (!class_exists(ReadOnlyFilesystemAdapter::class)) { |
| 77 | + throw new MissingPackageException("Missing package, to use the readonly option, run:\n\ncomposer require league/flysystem-read-only"); |
| 78 | + } |
| 79 | + |
| 80 | + $originalAdapterId = $id; |
| 81 | + $container->setDefinition( |
| 82 | + $id = $id.'.read_only', |
| 83 | + $this->createReadOnlyAdapterDefinition(new Reference($originalAdapterId)) |
| 84 | + ); |
70 | 85 | }
|
71 | 86 |
|
72 | 87 | // Create storage definition
|
73 | 88 | $container->setDefinition(
|
74 | 89 | $storageName,
|
75 |
| - $this->createStorageDefinition($storageName, new Reference('flysystem.adapter.'.$storageName), $storageConfig) |
| 90 | + $this->createStorageDefinition($storageName, new Reference($id), $storageConfig) |
76 | 91 | );
|
77 | 92 |
|
78 | 93 | // Register named autowiring alias
|
@@ -122,4 +137,13 @@ private function createStorageDefinition(string $storageName, Reference $adapter
|
122 | 137 |
|
123 | 138 | return $definition;
|
124 | 139 | }
|
| 140 | + |
| 141 | + private function createReadOnlyAdapterDefinition(Reference $adapter): Definition |
| 142 | + { |
| 143 | + $definition = new Definition(ReadOnlyFilesystemAdapter::class); |
| 144 | + $definition->setPublic(false); |
| 145 | + $definition->setArgument(0, $adapter); |
| 146 | + |
| 147 | + return $definition; |
| 148 | + } |
125 | 149 | }
|
0 commit comments