Skip to content

Commit

Permalink
Create VisibilityConverter dynamically based on options (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead authored Sep 20, 2021
1 parent 7f59e68 commit 3f75bc8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
8 changes: 7 additions & 1 deletion doc/adapter_local.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
29 changes: 27 additions & 2 deletions src/DependencyInjection/Factory/Adapter/LocalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'])
Expand All @@ -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()
Expand Down
15 changes: 11 additions & 4 deletions tests/App/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: ~

Expand Down

0 comments on commit 3f75bc8

Please sign in to comment.