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.
This commit is contained in:
Franz Liedke 2020-05-01 14:54:08 +02:00
parent 41a56c4ad1
commit 260e7cd48f
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
4 changed files with 31 additions and 36 deletions

View File

@ -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'
);
}
}

View File

@ -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
);
});
}

View File

@ -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();
}
}

View File

@ -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/*'));