From 3f75bc8297770e44d9f6f7e6927e15aefcf3ed9f Mon Sep 17 00:00:00 2001 From: David Greminger Date: Mon, 20 Sep 2021 10:06:18 +0200 Subject: [PATCH] Create VisibilityConverter dynamically based on options (#247) --- doc/adapter_local.md | 8 ++++- .../Factory/Adapter/LocalFactory.php | 29 +++++++++++++++++-- tests/App/config/config.yml | 15 +++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/doc/adapter_local.md b/doc/adapter_local.md index b579d62..c47c3e1 100644 --- a/doc/adapter_local.md +++ b/doc/adapter_local.md @@ -13,7 +13,13 @@ oneup_flysystem: lazy: ~ # boolean (default "false") writeFlags: ~ linkHandling: ~ - permissions: ~ + permissions: + files: + public: 0644 + private: 0600 + directories: + public: 0755 + private: 0700 ``` For more details on the `lazy` parameter, take a look at the [Symfony documentation](http://symfony.com/doc/current/components/dependency_injection/lazy_services.html). diff --git a/src/DependencyInjection/Factory/Adapter/LocalFactory.php b/src/DependencyInjection/Factory/Adapter/LocalFactory.php index e6f5245..7f25b0e 100644 --- a/src/DependencyInjection/Factory/Adapter/LocalFactory.php +++ b/src/DependencyInjection/Factory/Adapter/LocalFactory.php @@ -5,10 +5,12 @@ namespace Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter; use League\Flysystem\Local\LocalFilesystemAdapter; +use League\Flysystem\UnixVisibility\PortableVisibilityConverter; use Oneup\FlysystemBundle\DependencyInjection\Factory\AdapterFactoryInterface; use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; class LocalFactory implements AdapterFactoryInterface { @@ -19,11 +21,19 @@ public function getKey(): string public function create(ContainerBuilder $container, string $id, array $config): void { + $visibilityConverter = null; + + if (isset($config['visibilityConverter']) && null !== $config['visibilityConverter']) { + $visibilityConverter = new Definition(PortableVisibilityConverter::class); + $visibilityConverter->setFactory([PortableVisibilityConverter::class, 'fromArray']); + $visibilityConverter->setArgument(0, $config['permissions'] ?? []); + } + $container ->setDefinition($id, new ChildDefinition('oneup_flysystem.adapter.local')) ->setLazy($config['lazy']) ->replaceArgument(0, $config['location']) - ->replaceArgument(1, $config['visibilityConverter']) + ->replaceArgument(1, $visibilityConverter) ->replaceArgument(2, $config['writeFlags']) ->replaceArgument(3, $config['linkHandling']) ->replaceArgument(4, $config['mimeTypeDetector']) @@ -36,7 +46,22 @@ public function addConfiguration(NodeDefinition $node): void ->children() ->booleanNode('lazy')->defaultValue(false)->end() ->scalarNode('location')->isRequired()->end() - ->scalarNode('visibilityConverter')->defaultNull()->end() + ->arrayNode('permissions') + ->children() + ->arrayNode('files') + ->children() + ->scalarNode('public')->defaultNull()->end() + ->scalarNode('private')->defaultNull()->end() + ->end() + ->end() + ->arrayNode('directories') + ->children() + ->scalarNode('public')->defaultNull()->end() + ->scalarNode('private')->defaultNull()->end() + ->end() + ->end() + ->end() + ->end() ->scalarNode('writeFlags')->defaultValue(\LOCK_EX)->end() ->scalarNode('linkHandling')->defaultValue(LocalFilesystemAdapter::DISALLOW_LINKS)->end() ->scalarNode('mimeTypeDetector')->defaultNull()->end() diff --git a/tests/App/config/config.yml b/tests/App/config/config.yml index 217706e..edd497a 100644 --- a/tests/App/config/config.yml +++ b/tests/App/config/config.yml @@ -9,13 +9,12 @@ services: google_cloud_storage_client: class: Google\Cloud\Storage\StorageClient - framework: translator: { fallback: en } secret: secret router: - resource: "%kernel.project_dir%/config/routing.yml" - strict_requirements: "%kernel.debug%" + resource: '%kernel.project_dir%/config/routing.yml' + strict_requirements: '%kernel.debug%' default_locale: en session: ~ test: true @@ -25,7 +24,15 @@ oneup_flysystem: adapters: local: local: - location: "%kernel.project_dir%/cache" + location: '%kernel.project_dir%/cache' + permissions: + files: + public: 0644 + private: 0600 + directories: + public: 0755 + private: 0700 + memory: memory: ~