From 244f61f5d3f9eef4973af456ee17f425c2f28e7f Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 1 May 2020 14:54:08 +0200 Subject: [PATCH] Inject new Paths class instead of Application This (and similar work in other areas) will allow us to further reduce the API surface of the Application class. Separation of concerns etc. --- .../src/Database/Console/MigrateCommand.php | 15 ++++---- .../src/Database/MigrationServiceProvider.php | 3 +- .../core/src/Extension/ExtensionManager.php | 35 ++++++++----------- .../Foundation/Console/CacheClearCommand.php | 14 ++++---- 4 files changed, 31 insertions(+), 36 deletions(-) diff --git a/framework/core/src/Database/Console/MigrateCommand.php b/framework/core/src/Database/Console/MigrateCommand.php index 8e1357c3c..18631fac1 100644 --- a/framework/core/src/Database/Console/MigrateCommand.php +++ b/framework/core/src/Database/Console/MigrateCommand.php @@ -13,6 +13,7 @@ use Flarum\Console\AbstractCommand; use Flarum\Database\Migrator; use Flarum\Extension\ExtensionManager; use Flarum\Foundation\Application; +use Flarum\Foundation\Paths; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Container\Container; use Illuminate\Database\ConnectionInterface; @@ -26,18 +27,18 @@ class MigrateCommand extends AbstractCommand protected $container; /** - * @var Application + * @var Paths */ - protected $app; + protected $paths; /** * @param Container $container - * @param Application $application + * @param Paths $paths */ - public function __construct(Container $container, Application $application) + public function __construct(Container $container, Paths $paths) { $this->container = $container; - $this->app = $application; + $this->paths = $paths; parent::__construct(); } @@ -91,8 +92,8 @@ class MigrateCommand extends AbstractCommand $this->info('Publishing assets...'); $this->container->make('files')->copyDirectory( - $this->app->vendorPath().'/components/font-awesome/webfonts', - $this->app->publicPath().'/assets/fonts' + $this->paths->vendor.'/components/font-awesome/webfonts', + $this->paths->public.'/assets/fonts' ); } } diff --git a/framework/core/src/Database/MigrationServiceProvider.php b/framework/core/src/Database/MigrationServiceProvider.php index fb536c9f9..131522975 100644 --- a/framework/core/src/Database/MigrationServiceProvider.php +++ b/framework/core/src/Database/MigrationServiceProvider.php @@ -10,6 +10,7 @@ namespace Flarum\Database; use Flarum\Foundation\AbstractServiceProvider; +use Flarum\Foundation\Paths; use Illuminate\Filesystem\Filesystem; class MigrationServiceProvider extends AbstractServiceProvider @@ -26,7 +27,7 @@ class MigrationServiceProvider extends AbstractServiceProvider $this->app->bind(MigrationCreator::class, function () { return new MigrationCreator( $this->app->make(Filesystem::class), - $this->app->basePath() + $this->app->make(Paths::class)->base ); }); } diff --git a/framework/core/src/Extension/ExtensionManager.php b/framework/core/src/Extension/ExtensionManager.php index ac5dcfd8a..40136e348 100644 --- a/framework/core/src/Extension/ExtensionManager.php +++ b/framework/core/src/Extension/ExtensionManager.php @@ -15,7 +15,7 @@ use Flarum\Extension\Event\Disabling; use Flarum\Extension\Event\Enabled; use Flarum\Extension\Event\Enabling; use Flarum\Extension\Event\Uninstalled; -use Flarum\Foundation\Application; +use Flarum\Foundation\Paths; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Events\Dispatcher; @@ -29,7 +29,10 @@ class ExtensionManager { protected $config; - protected $app; + /** + * @var Paths + */ + protected $paths; protected $container; @@ -52,14 +55,14 @@ class ExtensionManager public function __construct( SettingsRepositoryInterface $config, - Application $app, + Paths $paths, Container $container, Migrator $migrator, Dispatcher $dispatcher, Filesystem $filesystem ) { $this->config = $config; - $this->app = $app; + $this->paths = $paths; $this->container = $container; $this->migrator = $migrator; $this->dispatcher = $dispatcher; @@ -71,11 +74,11 @@ class ExtensionManager */ public function getExtensions() { - if (is_null($this->extensions) && $this->filesystem->exists($this->app->vendorPath().'/composer/installed.json')) { + if (is_null($this->extensions) && $this->filesystem->exists($this->paths->vendor.'/composer/installed.json')) { $extensions = new Collection(); // Load all packages installed by composer. - $installed = json_decode($this->filesystem->get($this->app->vendorPath().'/composer/installed.json'), true); + $installed = json_decode($this->filesystem->get($this->paths->vendor.'/composer/installed.json'), true); // Composer 2.0 changes the structure of the installed.json manifest $installed = $installed['packages'] ?? $installed; @@ -86,8 +89,8 @@ class ExtensionManager } $path = isset($package['install-path']) - ? $this->getExtensionsDir().'/composer/'.$package['install-path'] - : $this->getExtensionsDir().'/'.Arr::get($package, 'name'); + ? $this->paths->vendor.'/composer/'.$package['install-path'] + : $this->paths->vendor.'/'.Arr::get($package, 'name'); // Instantiates an Extension object using the package path and composer.json file. $extension = new Extension($path, $package); @@ -203,7 +206,7 @@ class ExtensionManager if ($extension->hasAssets()) { $this->filesystem->copyDirectory( $extension->getPath().'/assets', - $this->app->publicPath().'/assets/extensions/'.$extension->getId() + $this->paths->public.'/assets/extensions/'.$extension->getId() ); } } @@ -215,7 +218,7 @@ class ExtensionManager */ protected function unpublishAssets(Extension $extension) { - $this->filesystem->deleteDirectory($this->app->publicPath().'/assets/extensions/'.$extension->getId()); + $this->filesystem->deleteDirectory($this->paths->public.'/assets/extensions/'.$extension->getId()); } /** @@ -227,7 +230,7 @@ class ExtensionManager */ public function getAsset(Extension $extension, $path) { - return $this->app->publicPath().'/assets/extensions/'.$extension->getId().$path; + return $this->paths->public.'/assets/extensions/'.$extension->getId().$path; } /** @@ -332,14 +335,4 @@ class ExtensionManager return isset($enabled[$extension]); } - - /** - * The extensions path. - * - * @return string - */ - protected function getExtensionsDir() - { - return $this->app->vendorPath(); - } } diff --git a/framework/core/src/Foundation/Console/CacheClearCommand.php b/framework/core/src/Foundation/Console/CacheClearCommand.php index 15e049724..1229f74fa 100644 --- a/framework/core/src/Foundation/Console/CacheClearCommand.php +++ b/framework/core/src/Foundation/Console/CacheClearCommand.php @@ -10,8 +10,8 @@ namespace Flarum\Foundation\Console; use Flarum\Console\AbstractCommand; -use Flarum\Foundation\Application; use Flarum\Foundation\Event\ClearingCache; +use Flarum\Foundation\Paths; use Illuminate\Contracts\Cache\Store; class CacheClearCommand extends AbstractCommand @@ -22,18 +22,18 @@ class CacheClearCommand extends AbstractCommand protected $cache; /** - * @var Application + * @var Paths */ - protected $app; + protected $paths; /** * @param Store $cache - * @param Application $app + * @param Paths $paths */ - public function __construct(Store $cache, Application $app) + public function __construct(Store $cache, Paths $paths) { $this->cache = $cache; - $this->app = $app; + $this->paths = $paths; parent::__construct(); } @@ -57,7 +57,7 @@ class CacheClearCommand extends AbstractCommand $this->cache->flush(); - $storagePath = $this->app->storagePath(); + $storagePath = $this->paths->storage; array_map('unlink', glob($storagePath.'/formatter/*')); array_map('unlink', glob($storagePath.'/locale/*'));