Skip to content

Commit

Permalink
WIP: Use Laravel filesystem interface where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Mar 22, 2021
1 parent 706eaed commit 5e12cfe
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 120 deletions.
13 changes: 7 additions & 6 deletions src/Api/Controller/DeleteFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteFaviconController extends AbstractDeleteController
Expand All @@ -22,18 +23,18 @@ class DeleteFaviconController extends AbstractDeleteController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -47,7 +48,7 @@ protected function delete(ServerRequestInterface $request)

$this->settings->set('favicon_path', null);

if ($this->uploadDir->has($path)) {
if ($this->uploadDir->exists($path)) {
$this->uploadDir->delete($path);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Api/Controller/DeleteLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteLogoController extends AbstractDeleteController
Expand All @@ -22,18 +23,18 @@ class DeleteLogoController extends AbstractDeleteController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -47,7 +48,7 @@ protected function delete(ServerRequestInterface $request)

$this->settings->set('logo_path', null);

if ($this->uploadDir->has($path)) {
if ($this->uploadDir->exists($path)) {
$this->uploadDir->delete($path);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Api/Controller/UploadImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Intervention\Image\Image;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -26,7 +27,7 @@ abstract class UploadImageController extends ShowForumController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

Expand All @@ -47,12 +48,12 @@ abstract class UploadImageController extends ShowForumController

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -72,7 +73,7 @@ public function data(ServerRequestInterface $request, Document $document)

$uploadName = $this->filenamePrefix.'-'.Str::lower(Str::random(8)).'.'.$this->fileExtension;

$this->uploadDir->write($uploadName, $encodedImage);
$this->uploadDir->put($uploadName, $encodedImage);

$this->settings->set($this->filePathSettingKey, $uploadName);

Expand Down
9 changes: 6 additions & 3 deletions src/Extend/LanguagePack.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Extension\ExtensionManager;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Factory;
use InvalidArgumentException;
use RuntimeException;
use SplFileInfo;
Expand Down Expand Up @@ -66,19 +67,21 @@ private function registerLocale(Container $container, LocaleManager $locales, Ex
{
$locales->addLocale($locale, $title);

$vendorFilesystem = $container->make(Factory::class)->disk('flarum-vendor');

$directory = $extension->getPath().$this->path;

if (! is_dir($directory)) {
if (! $vendorFilesystem->exists($directory)) {
throw new RuntimeException(
'Expected to find "'.$this->path.'" directory in language pack.'
);
}

if (file_exists($file = $directory.'/config.js')) {
if ($vendorFilesystem->exists($file = $directory.'/config.js')) {
$locales->addJsFile($locale, $file);
}

if (file_exists($file = $directory.'/config.css')) {
if ($vendorFilesystem->exists($file = $directory.'/config.css')) {
$locales->addCssFile($locale, $file);
}

Expand Down
53 changes: 19 additions & 34 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
use Flarum\Database\Migrator;
use Flarum\Extend\LifecycleInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use League\Flysystem\Plugin\ListFiles;

/**
* @property string $name
Expand Down Expand Up @@ -112,14 +108,20 @@ protected static function nameToId($name)
*/
protected $version;

/**
* @var Filesystem
*/
protected $vendorFilesystem;

/**
* @param $path
* @param array $composerJson
*/
public function __construct($path, $composerJson)
public function __construct($path, $composerJson, $vendorFilesystem)
{
$this->path = $path;
$this->composerJson = $composerJson;
$this->vendorFilesystem = $vendorFilesystem;
$this->assignId();
}

Expand Down Expand Up @@ -250,16 +252,16 @@ public function getIcon()
return $icon;
}

$file = $this->path.'/'.$file;
$file = "$this->path/$file";

if (file_exists($file)) {
if ($this->vendorFilesystem->exists($file)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (! array_key_exists($extension, self::LOGO_MIMETYPES)) {
throw new \RuntimeException('Invalid image type');
}

$mimetype = self::LOGO_MIMETYPES[$extension];
$data = base64_encode(file_get_contents($file));
$data = base64_encode($this->vendorFilesystem->get($file));

$icon['backgroundImage'] = "url('data:$mimetype;base64,$data')";
}
Expand Down Expand Up @@ -330,13 +332,13 @@ public function getOptionalDependencyIds(): array

private function getExtenders(): array
{
$extenderFile = $this->getExtenderFile();
$extenderPath = "$this->path/extend.php";

if (! $extenderFile) {
if (! $this->vendorFilesystem->exists($extenderPath)) {
return [];
}

$extenders = require $extenderFile;
$extenders = eval('?>' . $this->vendorFilesystem->get($extenderPath));

if (! is_array($extenders)) {
$extenders = [$extenders];
Expand All @@ -358,17 +360,6 @@ function ($extender) {
);
}

private function getExtenderFile(): ?string
{
$filename = "{$this->path}/extend.php";

if (file_exists($filename)) {
return $filename;
}

return null;
}

/**
* Compile a list of links for this extension.
*/
Expand Down Expand Up @@ -419,25 +410,19 @@ public function getLinks()
*/
public function hasAssets()
{
return realpath($this->path.'/assets/') !== false;
return $this->vendorFilesystem->exists("$this->path/assets");
}

public function copyAssetsTo(FilesystemInterface $target)
public function copyAssetsTo(Filesystem $assetsFilesystem)
{
if (! $this->hasAssets()) {
return;
}

$mount = new MountManager([
'source' => $source = new Filesystem(new Local($this->getPath().'/assets')),
'target' => $target,
]);

$source->addPlugin(new ListFiles);
$assetFiles = $source->listFiles('/', true);
$assetFiles = $this->vendorFilesystem->allFiles("$this->path/assets");

foreach ($assetFiles as $file) {
$mount->copy("source://$file[path]", "target://extensions/$this->id/$file[path]");
$assetsFilesystem->put("extensions/$this->id/$file", $this->vendorFilesystem->get("$this->path/assets/$file"));
}
}

Expand All @@ -448,7 +433,7 @@ public function copyAssetsTo(FilesystemInterface $target)
*/
public function hasMigrations()
{
return realpath($this->path.'/migrations/') !== false;
return $this->vendorFilesystem->exists("$this->path/migrations");
}

public function migrate(Migrator $migrator, $direction = 'up')
Expand Down
34 changes: 18 additions & 16 deletions src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

Expand All @@ -46,7 +47,12 @@ class ExtensionManager
/**
* @var Filesystem
*/
protected $filesystem;
protected $vendorFilesystem;

/**
* @var Filesystem
*/
protected $assetsFilesystem;

/**
* @var Collection|null
Expand All @@ -59,26 +65,27 @@ public function __construct(
Container $container,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
Factory $filesystemFactory
) {
$this->config = $config;
$this->paths = $paths;
$this->container = $container;
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
$this->vendorFilesystem = $filesystemFactory->disk('flarum-vendor');
$this->assetsFilesystem = $filesystemFactory->disk('flarum-assets');
}

/**
* @return Collection
*/
public function getExtensions()
{
if (is_null($this->extensions) && $this->filesystem->exists($this->paths->vendor.'/composer/installed.json')) {
if (is_null($this->extensions) && $this->vendorFilesystem->exists('composer/installed.json')) {
$extensions = new Collection();

// Load all packages installed by composer.
$installed = json_decode($this->filesystem->get($this->paths->vendor.'/composer/installed.json'), true);
$installed = json_decode($this->vendorFilesystem->get('composer/installed.json'), true);

// Composer 2.0 changes the structure of the installed.json manifest
$installed = $installed['packages'] ?? $installed;
Expand All @@ -98,11 +105,11 @@ public function getExtensions()
$installedSet[Arr::get($package, 'name')] = true;

$path = isset($package['install-path'])
? $this->paths->vendor.'/composer/'.$package['install-path']
: $this->paths->vendor.'/'.Arr::get($package, 'name');
? '/composer/'.$package['install-path']
: '/'.Arr::get($package, 'name');

// Instantiates an Extension object using the package path and composer.json file.
$extension = new Extension($path, $package);
$extension = new Extension($path, $package, $this->vendorFilesystem);

// Per default all extensions are installed if they are registered in composer.
$extension->setInstalled(true);
Expand Down Expand Up @@ -252,12 +259,7 @@ public function uninstall($name)
*/
protected function publishAssets(Extension $extension)
{
if ($extension->hasAssets()) {
$this->filesystem->copyDirectory(
$extension->getPath().'/assets',
$this->paths->public.'/assets/extensions/'.$extension->getId()
);
}
return $extension->copyAssetsTo($this->vendorFilesystem, $this->assetsFilesystem);
}

/**
Expand All @@ -267,7 +269,7 @@ protected function publishAssets(Extension $extension)
*/
protected function unpublishAssets(Extension $extension)
{
$this->filesystem->deleteDirectory($this->paths->public.'/assets/extensions/'.$extension->getId());
$this->assetsFilesystem->deleteDirectory('extensions/'.$extension->getId());
}

/**
Expand Down
Loading

0 comments on commit 5e12cfe

Please sign in to comment.