|
| 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 League\FlysystemBundle\DependencyInjection; |
| 13 | + |
| 14 | +use League\Flysystem\Cached\CachedAdapter; |
| 15 | +use League\Flysystem\Filesystem; |
| 16 | +use League\Flysystem\FilesystemInterface; |
| 17 | +use Symfony\Component\Config\FileLocator; |
| 18 | +use Symfony\Component\DependencyInjection\Alias; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | +use Symfony\Component\DependencyInjection\Definition; |
| 21 | +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 22 | +use Symfony\Component\DependencyInjection\Reference; |
| 23 | +use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Titouan Galopin <galopintitouan@gmail.com> |
| 27 | + * |
| 28 | + * @final |
| 29 | + */ |
| 30 | +class FlysystemExtension extends Extension |
| 31 | +{ |
| 32 | + public function load(array $configs, ContainerBuilder $container) |
| 33 | + { |
| 34 | + $configuration = new Configuration(); |
| 35 | + $config = $this->processConfiguration($configuration, $configs); |
| 36 | + |
| 37 | + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
| 38 | + $loader->load('services.xml'); |
| 39 | + |
| 40 | + if (!isset($config['filesystems'][$config['default_filesystem']])) { |
| 41 | + throw new \LogicException('Default filesystem "'.$config['default_filesystem'].'" is not defined in the "flysystem.filesystems" configuration key.'); |
| 42 | + } |
| 43 | + |
| 44 | + $container->setParameter( |
| 45 | + 'flysystem.default_local_directory', |
| 46 | + $config['default_local_directory'] ?: $container->getParameter('kernel.project_dir').'/storage' |
| 47 | + ); |
| 48 | + |
| 49 | + foreach ($config['filesystems'] as $fsName => $fsConfig) { |
| 50 | + if (!$fsConfig['adapter'] && !$fsConfig['mounts']) { |
| 51 | + throw new \LogicException('Definition of the filesystem "'.$fsName.'" is invalid: one of the "adapter" and "mounts" keys is required.'); |
| 52 | + } |
| 53 | + |
| 54 | + if ($fsConfig['adapter'] && $fsConfig['mounts']) { |
| 55 | + throw new \LogicException('Definition of the filesystem "'.$fsName.'" is invalid: configuring both "adapter" and "mounts" keys is not allowed.'); |
| 56 | + } |
| 57 | + |
| 58 | + // Create adapter definition |
| 59 | + if ($fsConfig['cache']) { |
| 60 | + $adapterDefinition = new Definition(CachedAdapter::class); |
| 61 | + $adapterDefinition->setPublic(false); |
| 62 | + $adapterDefinition->setArgument(0, new Reference($fsConfig['adapter'])); |
| 63 | + $adapterDefinition->setArgument(1, new Reference($fsConfig['cache'])); |
| 64 | + |
| 65 | + $container->setDefinition('flysystem.filesystem.'.$fsName.'.adapter', $adapterDefinition); |
| 66 | + } else { |
| 67 | + $container->setAlias('flysystem.filesystem.'.$fsName.'.adapter', new Alias($fsConfig['adapter'])); |
| 68 | + } |
| 69 | + |
| 70 | + $definition = new Definition(Filesystem::class); |
| 71 | + $definition->setPublic(false); |
| 72 | + $definition->setArgument(0, new Reference('flysystem.filesystem.'.$fsName.'.adapter')); |
| 73 | + $definition->setArgument(1, $fsConfig['config']); |
| 74 | + |
| 75 | + $container->setDefinition('flysystem.filesystem.'.$fsName, $definition); |
| 76 | + |
| 77 | + $container |
| 78 | + ->registerAliasForArgument('flysystem.filesystem.'.$fsName, FilesystemInterface::class, $fsName) |
| 79 | + ->setPublic(false); |
| 80 | + |
| 81 | + if ($config['default_filesystem'] === $fsName) { |
| 82 | + $container->setAlias(FilesystemInterface::class, 'flysystem.filesystem.'.$fsName)->setPublic(false); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments