Skip to content

Commit f351186

Browse files
committed
refactor: encrypted disk now decorates another disk
1 parent 3c7f8c5 commit f351186

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

src/EncryptedDataServiceProvider.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
use Illuminate\Contracts\Foundation\Application;
66
use Illuminate\Filesystem\FilesystemAdapter;
7-
use Illuminate\Support\Arr;
87
use Illuminate\Support\Facades\Storage;
98
use Illuminate\Support\ServiceProvider;
10-
use League\Flysystem\Filesystem;
11-
use League\Flysystem\Local\LocalFilesystemAdapter;
12-
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
13-
use League\Flysystem\Visibility;
9+
use InvalidArgumentException;
1410
use Swis\Flysystem\Encrypted\EncryptedFilesystemAdapter;
1511
use Swis\Laravel\Encrypted\Commands\ReEncryptFiles;
1612
use Swis\Laravel\Encrypted\Commands\ReEncryptModels;
@@ -42,40 +38,28 @@ public function boot(): void
4238
protected function setupStorageDriver(): void
4339
{
4440
Storage::extend(
45-
'local-encrypted',
46-
function (Application $app, array $config) {
47-
$visibility = PortableVisibilityConverter::fromArray(
48-
$config['permissions'] ?? [],
49-
$config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE
50-
);
41+
'encrypted',
42+
(function (Application $app, array $config) {
43+
/** @var \Illuminate\Filesystem\FilesystemManager $this */
44+
if (empty($config['disk'])) {
45+
throw new InvalidArgumentException('Encrypted disk is missing "disk" configuration option.');
46+
}
5147

52-
$links = ($config['links'] ?? null) === 'skip'
53-
? LocalFilesystemAdapter::SKIP_LINKS
54-
: LocalFilesystemAdapter::DISALLOW_LINKS;
48+
$parent = $this->build(
49+
is_string($config['disk']) ? $this->getConfig($config['disk']) : $config['disk']
50+
);
5551

5652
$adapter = new EncryptedFilesystemAdapter(
57-
new LocalFilesystemAdapter(
58-
$config['root'],
59-
$visibility,
60-
$config['lock'] ?? LOCK_EX,
61-
$links
62-
),
53+
$parent->getAdapter(),
6354
$app->make('encrypted-data.encrypter')
6455
);
6556

66-
$driver = new Filesystem(
57+
return new FilesystemAdapter(
58+
$this->createFlysystem($adapter, $parent->getConfig()),
6759
$adapter,
68-
Arr::only($config, [
69-
'directory_visibility',
70-
'disable_asserts',
71-
'temporary_url',
72-
'url',
73-
'visibility',
74-
])
60+
$parent->getConfig()
7561
);
76-
77-
return new FilesystemAdapter($driver, $adapter, $config);
78-
}
62+
})->bindTo(Storage::getFacadeRoot(), Storage::getFacadeRoot())
7963
);
8064
}
8165
}

tests/Feature/ReEncryptFilesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ protected function hasLocalDisk($app): void
5252
protected function hasEncryptedDisk($app): void
5353
{
5454
$app['config']->set('filesystems.default', 'local');
55-
$app['config']->set('filesystems.disks.local', ['driver' => 'local-encrypted', 'root' => $this->diskRoot]);
55+
$app['config']->set('filesystems.disks.local', ['driver' => 'encrypted', 'disk' => ['driver' => 'local', 'root' => $this->diskRoot]]);
5656

5757
$this->filesystem->ensureDirectoryExists($this->diskRoot);
5858
}
5959

6060
protected function hasExtraEncryptedDisk($app): void
6161
{
6262
$diskRoot = dirname($this->diskRoot).'/extra';
63-
$app['config']->set('filesystems.disks.extra', ['driver' => 'local-encrypted', 'root' => $diskRoot]);
63+
$app['config']->set('filesystems.disks.extra', ['driver' => 'encrypted', 'disk' => ['driver' => 'local', 'root' => $diskRoot]]);
6464

6565
$this->filesystem->ensureDirectoryExists($diskRoot);
6666
}

tests/Unit/FilesystemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class FilesystemTest extends TestCase
1212
protected function usesEncryptedDisk($app): void
1313
{
1414
$app['config']->set('filesystems.default', 'local');
15-
$app['config']->set('filesystems.disks.local', ['driver' => 'local-encrypted', 'root' => dirname(__DIR__).'/_files/']);
15+
$app['config']->set('filesystems.disks.local', ['driver' => 'encrypted', 'disk' => ['driver' => 'local', 'root' => dirname(__DIR__).'/_files/']]);
1616
}
1717

1818
#[Test]

0 commit comments

Comments
 (0)