Skip to content

Commit

Permalink
fix: Default visibility for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhelias committed Oct 31, 2023
1 parent bdae49f commit b4924ea
Show file tree
Hide file tree
Showing 26 changed files with 162 additions and 103 deletions.
4 changes: 2 additions & 2 deletions src/Adapter/AdapterDefinitionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function __construct()
];
}

public function createDefinition(string $name, array $options): ?Definition
public function createDefinition(string $name, array $options, ?string $defaultVisibilityForDirectories): ?Definition
{
foreach ($this->builders as $builder) {
if ($builder->getName() === $name) {
return $builder->createDefinition($options);
return $builder->createDefinition($options, $defaultVisibilityForDirectories);
}
}

Expand Down
49 changes: 45 additions & 4 deletions src/Adapter/Builder/AbstractAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace League\FlysystemBundle\Adapter\Builder;

use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\FlysystemBundle\Exception\MissingPackageException;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -22,7 +23,7 @@
*/
abstract class AbstractAdapterDefinitionBuilder implements AdapterDefinitionBuilderInterface
{
final public function createDefinition(array $options): Definition
final public function createDefinition(array $options, ?string $defaultVisibilityForDirectories): Definition
{
$this->ensureRequiredPackagesAvailable();

Expand All @@ -31,7 +32,7 @@ final public function createDefinition(array $options): Definition

$definition = new Definition();
$definition->setPublic(false);
$this->configureDefinition($definition, $resolver->resolve($options));
$this->configureDefinition($definition, $resolver->resolve($options), $defaultVisibilityForDirectories);

return $definition;
}
Expand All @@ -40,9 +41,49 @@ abstract protected function getRequiredPackages(): array;

abstract protected function configureOptions(OptionsResolver $resolver);

abstract protected function configureDefinition(Definition $definition, array $options);
abstract protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories);

private function ensureRequiredPackagesAvailable()
protected function configureUnixOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('permissions', function (OptionsResolver $subResolver) {
$subResolver->setDefault('file', function (OptionsResolver $permsResolver) {
$permsResolver->setDefault('public', 0644);
$permsResolver->setAllowedTypes('public', 'scalar');

$permsResolver->setDefault('private', 0600);
$permsResolver->setAllowedTypes('private', 'scalar');
});

$subResolver->setDefault('dir', function (OptionsResolver $permsResolver) {
$permsResolver->setDefault('public', 0755);
$permsResolver->setAllowedTypes('public', 'scalar');

$permsResolver->setDefault('private', 0700);
$permsResolver->setAllowedTypes('private', 'scalar');
});
});
}

protected function createUnixDefinition(array $permissions, string $defaultVisibilityForDirectories): Definition
{
return (new Definition(PortableVisibilityConverter::class))
->setFactory([PortableVisibilityConverter::class, 'fromArray'])
->addArgument([
'file' => [
'public' => (int) $permissions['file']['public'],
'private' => (int) $permissions['file']['private'],
],
'dir' => [
'public' => (int) $permissions['dir']['public'],
'private' => (int) $permissions['dir']['private'],
],
])
->addArgument($defaultVisibilityForDirectories)
->setShared(false)
;
}

private function ensureRequiredPackagesAvailable(): void
{
$missingPackages = [];
foreach ($this->getRequiredPackages() as $requiredClass => $packageName) {
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Builder/AdapterDefinitionBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public function getName(): string;
/**
* Create the definition for this builder's adapter given an array of options.
*/
public function createDefinition(array $options): Definition;
public function createDefinition(array $options, ?string $defaultVisibilityForDirectories): Definition;
}
11 changes: 9 additions & 2 deletions src/Adapter/Builder/AsyncAwsAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace League\FlysystemBundle\Adapter\Builder;

use League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter;
use League\Flysystem\AsyncAwsS3\PortableVisibilityConverter;
use League\Flysystem\Visibility;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -36,7 +38,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('client');
$resolver->setAllowedTypes('client', 'string');
Expand All @@ -48,11 +50,16 @@ protected function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('prefix', 'string');
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(AsyncAwsS3Adapter::class);
$definition->setArgument(0, new Reference($options['client']));
$definition->setArgument(1, $options['bucket']);
$definition->setArgument(2, $options['prefix']);
$definition->setArgument(3,
(new Definition(PortableVisibilityConverter::class))
->setArgument(0, $defaultVisibilityForDirectories ?? Visibility::PUBLIC)
->setShared(false)
);
}
}
12 changes: 9 additions & 3 deletions src/Adapter/Builder/AwsAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace League\FlysystemBundle\Adapter\Builder;

use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\AwsS3V3\PortableVisibilityConverter;
use League\Flysystem\Visibility;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -35,7 +37,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('client');
$resolver->setAllowedTypes('client', 'string');
Expand All @@ -53,13 +55,17 @@ protected function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('streamReads', 'bool');
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(AwsS3V3Adapter::class);
$definition->setArgument(0, new Reference($options['client']));
$definition->setArgument(1, $options['bucket']);
$definition->setArgument(2, $options['prefix']);
$definition->setArgument(3, null);
$definition->setArgument(3,
(new Definition(PortableVisibilityConverter::class))
->setArgument(0, $defaultVisibilityForDirectories ?? Visibility::PUBLIC)
->setShared(false)
);
$definition->setArgument(4, null);
$definition->setArgument(5, $options['options']);
$definition->setArgument(6, $options['streamReads']);
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Builder/AzureAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('client');
$resolver->setAllowedTypes('client', 'string');
Expand All @@ -47,7 +47,7 @@ protected function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('prefix', 'string');
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(AzureBlobStorageAdapter::class);
$definition->setArgument(0, new Reference($options['client']));
Expand Down
8 changes: 6 additions & 2 deletions src/Adapter/Builder/FtpAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use League\Flysystem\Visibility;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -36,7 +37,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('host');
$resolver->setAllowedTypes('host', 'string');
Expand Down Expand Up @@ -84,9 +85,11 @@ protected function configureOptions(OptionsResolver $resolver)

$resolver->setDefault('connectivityChecker', null);
$resolver->setAllowedTypes('connectivityChecker', ['string', 'null']);

$this->configureUnixOptions($resolver);
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$options['transferMode'] = $options['transfer_mode'];
$options['systemType'] = $options['system_type'];
Expand Down Expand Up @@ -117,5 +120,6 @@ protected function configureDefinition(Definition $definition, array $options)
);
$definition->setArgument(1, null);
$definition->setArgument(2, $connectivityChecker);
$definition->setArgument(3, $this->createUnixDefinition($options['permissions'], $defaultVisibilityForDirectories ?? Visibility::PRIVATE));
}
}
4 changes: 2 additions & 2 deletions src/Adapter/Builder/GcloudAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('client');
$resolver->setAllowedTypes('client', 'string');
Expand All @@ -47,7 +47,7 @@ protected function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('prefix', 'string');
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$bucketDefinition = new Definition();
$bucketDefinition->setFactory([new Reference($options['client']), 'bucket']);
Expand Down
42 changes: 6 additions & 36 deletions src/Adapter/Builder/LocalAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace League\FlysystemBundle\Adapter\Builder;

use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\Visibility;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -33,58 +33,28 @@ protected function getRequiredPackages(): array
return [];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('directory');
$resolver->setAllowedTypes('directory', 'string');

$this->configureUnixOptions($resolver);

$resolver->setDefault('lock', 0);
$resolver->setAllowedTypes('lock', 'scalar');

$resolver->setDefault('skip_links', false);
$resolver->setAllowedTypes('skip_links', 'scalar');

$resolver->setDefault('permissions', function (OptionsResolver $subResolver) {
$subResolver->setDefault('file', function (OptionsResolver $permsResolver) {
$permsResolver->setDefault('public', 0644);
$permsResolver->setAllowedTypes('public', 'scalar');

$permsResolver->setDefault('private', 0600);
$permsResolver->setAllowedTypes('private', 'scalar');
});

$subResolver->setDefault('dir', function (OptionsResolver $permsResolver) {
$permsResolver->setDefault('public', 0755);
$permsResolver->setAllowedTypes('public', 'scalar');

$permsResolver->setDefault('private', 0700);
$permsResolver->setAllowedTypes('private', 'scalar');
});
});

$resolver->setDefault('lazy_root_creation', false);
$resolver->setAllowedTypes('lazy_root_creation', 'scalar');
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(LocalFilesystemAdapter::class);
$definition->setArgument(0, $options['directory']);
$definition->setArgument(1,
(new Definition(PortableVisibilityConverter::class))
->setFactory([PortableVisibilityConverter::class, 'fromArray'])
->addArgument([
'file' => [
'public' => (int) $options['permissions']['file']['public'],
'private' => (int) $options['permissions']['file']['private'],
],
'dir' => [
'public' => (int) $options['permissions']['dir']['public'],
'private' => (int) $options['permissions']['dir']['private'],
],
])
->setShared(false)
);
$definition->setArgument(1, $this->createUnixDefinition($options['permissions'], $defaultVisibilityForDirectories ?? Visibility::PRIVATE));
$definition->setArgument(2, $options['lock']);
$definition->setArgument(3, $options['skip_links'] ? LocalFilesystemAdapter::SKIP_LINKS : LocalFilesystemAdapter::DISALLOW_LINKS);
$definition->setArgument(4, null);
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Builder/MemoryAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function configureOptions(OptionsResolver $resolver)
{
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(InMemoryFilesystemAdapter::class);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Adapter/Builder/SftpAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use League\Flysystem\PhpseclibV2\SftpConnectionProvider as SftpConnectionProviderLegacy;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\Visibility;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected function getRequiredPackages(): array
];
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('host');
$resolver->setAllowedTypes('host', 'string');
Expand Down Expand Up @@ -89,9 +90,11 @@ protected function configureOptions(OptionsResolver $resolver)

$resolver->setDefault('connectivityChecker', null);
$resolver->setAllowedTypes('connectivityChecker', ['string', 'null']);

$this->configureUnixOptions($resolver);
}

protected function configureDefinition(Definition $definition, array $options)
protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
// Prevent BC
$adapterFqcn = SftpAdapter::class;
Expand All @@ -113,5 +116,6 @@ protected function configureDefinition(Definition $definition, array $options)
->setShared(false)
);
$definition->setArgument(1, $options['root']);
$definition->setArgument(2, $this->createUnixDefinition($options['permissions'], $defaultVisibilityForDirectories ?? Visibility::PRIVATE));
}
}
Loading

0 comments on commit b4924ea

Please sign in to comment.