1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 23:14:29 +02:00

Filesystem Extender and Tests (#2732)

This commit is contained in:
Alexander Skvortsov
2021-04-19 16:25:08 -04:00
committed by GitHub
parent 4974c91481
commit c84939b19c
7 changed files with 442 additions and 30 deletions

View File

@@ -0,0 +1,39 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Filesystem;
use Flarum\Foundation\Config;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;
interface DriverInterface
{
/**
* Construct a Laravel Cloud filesystem for this filesystem driver.
* Settings and configuration can either be pulled from the Flarum settings repository
* or the config.php file.
*
* Typically, this is done by wrapping a Flysystem adapter in Laravel's
* `Illuminate\Filesystem\FilesystemAdapter` class.
* You should ensure that the Flysystem adapter you use has a `getUrl` method.
* If it doesn't, you should create a subclass implementing that method.
* Otherwise, this driver won't work for public-facing disks
* like `flarum-assets` or `flarum-avatars`.
*
* @param string $diskName: The name of a disk this driver is being used for.
* This is generally used to locate disk-specific settings.
* @param SettingsRepositoryInterface $settings: An instance of the Flarum settings repository.
* @param Config $config: An instance of the wrapper class around `config.php`.
* @param array $localConfig: The configuration array that would have been used
* if this disk were using the 'local' filesystem driver.
* Some of these settings might be useful (e.g. visibility, )
*/
public function build(string $diskName, SettingsRepositoryInterface $settings, Config $config, array $localConfig): Cloud;
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Filesystem;
use Flarum\Foundation\Config;
use Flarum\Foundation\Paths;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager as LaravelFilesystemManager;
use Illuminate\Support\Arr;
use InvalidArgumentException;
class FilesystemManager extends LaravelFilesystemManager
{
protected $diskLocalConfig = [];
protected $drivers = [];
public function __construct(Container $app, array $diskLocalConfig, array $drivers)
{
parent::__construct($app);
$this->diskLocalConfig = $diskLocalConfig;
$this->drivers = $drivers;
}
/**
* @inheritDoc
*/
protected function resolve($name): Filesystem
{
$driver = $this->getDriver($name);
$localConfig = $this->getLocalConfig($name);
if (empty($localConfig)) {
throw new InvalidArgumentException("Disk [{$name}] has not been declared. Use the Filesystem extender to do this.");
}
if ($driver === 'local') {
return $this->createLocalDriver($localConfig);
}
$settings = $this->app->make(SettingsRepositoryInterface::class);
$config = $this->app->make(Config::class);
return $driver->build($name, $settings, $config, $localConfig);
}
/**
* @return string|DriverInterface
*/
protected function getDriver(string $name)
{
$config = $this->app->make(Config::class);
$settings = $this->app->make(SettingsRepositoryInterface::class);
$key = "disk_driver.$name";
$configuredDriver = Arr::get($config, $key, $settings->get($key, 'local'));
return Arr::get($this->drivers, $configuredDriver, 'local');
}
protected function getLocalConfig(string $name): array
{
if (! array_key_exists($name, $this->diskLocalConfig)) {
return [];
}
$paths = $this->app->make(Paths::class);
$url = $this->app->make(UrlGenerator::class);
return $this->diskLocalConfig[$name]($paths, $url);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Filesystem;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Paths;
use Flarum\Http\UrlGenerator;
use Illuminate\Filesystem\Filesystem;
class FilesystemServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->container->singleton('files', function () {
return new Filesystem;
});
$this->container->singleton('flarum.filesystem.disks', function () {
return [
'flarum-assets' => function (Paths $paths, UrlGenerator $url) {
return [
'root' => "$paths->public/assets",
'url' => $url->to('forum')->path('assets')
];
},
'flarum-avatars' => function (Paths $paths, UrlGenerator $url) {
return [
'root' => "$paths->public/assets/avatars",
'url' => $url->to('forum')->path('assets/avatars')
];
},
];
});
$this->container->singleton('flarum.filesystem.drivers', function () {
return [];
});
$this->container->singleton('flarum.filesystem.resolved_drivers', function () {
return array_map(function ($driverClass) {
return $this->container->make($driverClass);
}, $this->container->make('flarum.filesystem.drivers'));
});
$this->container->singleton('filesystem', function () {
return new FilesystemManager(
$this->container,
$this->container->make('flarum.filesystem.disks'),
$this->container->make('flarum.filesystem.resolved_drivers')
);
});
}
}