1
0
mirror of https://github.com/flarum/core.git synced 2025-07-24 02:01:19 +02:00

Use Container contract where easily possible

Less usages of the Application god-class simplifies splitting it up.

Refs #2055.
This commit is contained in:
Franz Liedke
2020-04-21 16:47:10 +02:00
parent f0f301c5f4
commit 3f0f89afb1
5 changed files with 47 additions and 33 deletions

View File

@@ -9,9 +9,9 @@
namespace Flarum\Console\Event; namespace Flarum\Console\Event;
use Flarum\Foundation\Application;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Symfony\Component\Console\Application as ConsoleApplication; use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Application;
/** /**
* @deprecated * @deprecated
@@ -19,22 +19,22 @@ use Symfony\Component\Console\Application as ConsoleApplication;
class Configuring class Configuring
{ {
/** /**
* @var Application * @var Container
*/ */
public $app; public $app;
/** /**
* @var ConsoleApplication * @var Application
*/ */
public $console; public $console;
/** /**
* @param Application $app * @param Container $container
* @param ConsoleApplication $console * @param Application $console
*/ */
public function __construct(Application $app, ConsoleApplication $console) public function __construct(Container $container, Application $console)
{ {
$this->app = $app; $this->app = $container;
$this->console = $console; $this->console = $console;
} }

View File

@@ -10,12 +10,12 @@
namespace Flarum\Console; namespace Flarum\Console;
use Flarum\Console\Event\Configuring; use Flarum\Console\Event\Configuring;
use Flarum\Foundation\Application;
use Flarum\Foundation\ErrorHandling\Registry; use Flarum\Foundation\ErrorHandling\Registry;
use Flarum\Foundation\ErrorHandling\Reporter; use Flarum\Foundation\ErrorHandling\Reporter;
use Flarum\Foundation\SiteInterface; use Flarum\Foundation\SiteInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Console\Application as ConsoleApplication; use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleErrorEvent; use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -33,7 +33,7 @@ class Server
{ {
$app = $this->site->bootApp(); $app = $this->site->bootApp();
$console = new ConsoleApplication('Flarum', Application::VERSION); $console = new Application('Flarum', \Flarum\Foundation\Application::VERSION);
foreach ($app->getConsoleCommands() as $command) { foreach ($app->getConsoleCommands() as $command) {
$console->add($command); $console->add($command);
@@ -47,29 +47,28 @@ class Server
/** /**
* @deprecated * @deprecated
*/ */
private function extend(ConsoleApplication $console) private function extend(Application $console)
{ {
$app = Application::getInstance(); $container = \Illuminate\Container\Container::getInstance();
$this->handleErrors($app, $console); $this->handleErrors($container, $console);
$events = $app->make(Dispatcher::class); $events = $container->make(Dispatcher::class);
$events->dispatch(new Configuring($container, $console));
$events->dispatch(new Configuring($app, $console));
} }
private function handleErrors(Application $app, ConsoleApplication $console) private function handleErrors(Container $container, Application $console)
{ {
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) use ($app) { $dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) use ($container) {
/** @var Registry $registry */ /** @var Registry $registry */
$registry = $app->make(Registry::class); $registry = $container->make(Registry::class);
$error = $registry->handle($event->getError()); $error = $registry->handle($event->getError());
/** @var Reporter[] $reporters */ /** @var Reporter[] $reporters */
$reporters = $app->tagged(Reporter::class); $reporters = $container->tagged(Reporter::class);
if ($error->shouldBeReported()) { if ($error->shouldBeReported()) {
foreach ($reporters as $reporter) { foreach ($reporters as $reporter) {

View File

@@ -14,21 +14,29 @@ use Flarum\Database\Migrator;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
class MigrateCommand extends AbstractCommand class MigrateCommand extends AbstractCommand
{ {
/**
* @var Container
*/
protected $container;
/** /**
* @var Application * @var Application
*/ */
protected $app; protected $app;
/** /**
* @param Container $container
* @param Application $application * @param Application $application
*/ */
public function __construct(Application $application) public function __construct(Container $container, Application $application)
{ {
$this->container = $container;
$this->app = $application; $this->app = $application;
parent::__construct(); parent::__construct();
@@ -58,16 +66,16 @@ class MigrateCommand extends AbstractCommand
public function upgrade() public function upgrade()
{ {
$this->app->bind(Builder::class, function ($app) { $this->container->bind(Builder::class, function ($container) {
return $app->make(ConnectionInterface::class)->getSchemaBuilder(); return $container->make(ConnectionInterface::class)->getSchemaBuilder();
}); });
$migrator = $this->app->make(Migrator::class); $migrator = $this->container->make(Migrator::class);
$migrator->setOutput($this->output); $migrator->setOutput($this->output);
$migrator->run(__DIR__.'/../../../migrations'); $migrator->run(__DIR__.'/../../../migrations');
$extensions = $this->app->make(ExtensionManager::class); $extensions = $this->container->make(ExtensionManager::class);
$extensions->getMigrator()->setOutput($this->output); $extensions->getMigrator()->setOutput($this->output);
foreach ($extensions->getEnabledExtensions() as $name => $extension) { foreach ($extensions->getEnabledExtensions() as $name => $extension) {
@@ -78,11 +86,11 @@ class MigrateCommand extends AbstractCommand
} }
} }
$this->app->make(SettingsRepositoryInterface::class)->set('version', $this->app->version()); $this->container->make(SettingsRepositoryInterface::class)->set('version', $this->app->version());
$this->info('Publishing assets...'); $this->info('Publishing assets...');
$this->app->make('files')->copyDirectory( $this->container->make('files')->copyDirectory(
$this->app->vendorPath().'/components/font-awesome/webfonts', $this->app->vendorPath().'/components/font-awesome/webfonts',
$this->app->publicPath().'/assets/fonts' $this->app->publicPath().'/assets/fonts'
); );

View File

@@ -31,6 +31,8 @@ class ExtensionManager
protected $app; protected $app;
protected $container;
protected $migrator; protected $migrator;
/** /**
@@ -51,12 +53,14 @@ class ExtensionManager
public function __construct( public function __construct(
SettingsRepositoryInterface $config, SettingsRepositoryInterface $config,
Application $app, Application $app,
Container $container,
Migrator $migrator, Migrator $migrator,
Dispatcher $dispatcher, Dispatcher $dispatcher,
Filesystem $filesystem Filesystem $filesystem
) { ) {
$this->config = $config; $this->config = $config;
$this->app = $app; $this->app = $app;
$this->container = $container;
$this->migrator = $migrator; $this->migrator = $migrator;
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
@@ -138,7 +142,7 @@ class ExtensionManager
$this->setEnabled($enabled); $this->setEnabled($enabled);
$extension->enable($this->app); $extension->enable($this->container);
$this->dispatcher->dispatch(new Enabled($extension)); $this->dispatcher->dispatch(new Enabled($extension));
} }
@@ -164,7 +168,7 @@ class ExtensionManager
$this->setEnabled($enabled); $this->setEnabled($enabled);
$extension->disable($this->app); $extension->disable($this->container);
$this->dispatcher->dispatch(new Disabled($extension)); $this->dispatcher->dispatch(new Disabled($extension));
} }
@@ -235,7 +239,7 @@ class ExtensionManager
*/ */
public function migrate(Extension $extension, $direction = 'up') public function migrate(Extension $extension, $direction = 'up')
{ {
$this->app->bind(Builder::class, function ($container) { $this->container->bind(Builder::class, function ($container) {
return $container->make(ConnectionInterface::class)->getSchemaBuilder(); return $container->make(ConnectionInterface::class)->getSchemaBuilder();
}); });

View File

@@ -12,10 +12,12 @@ namespace Flarum\Frontend\Content;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Frontend\Compiler\CompilerInterface; use Flarum\Frontend\Compiler\CompilerInterface;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Illuminate\Contracts\Container\Container;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
class Assets class Assets
{ {
protected $container;
protected $app; protected $app;
/** /**
@@ -23,14 +25,15 @@ class Assets
*/ */
protected $assets; protected $assets;
public function __construct(Application $app) public function __construct(Container $container, Application $app)
{ {
$this->container = $container;
$this->app = $app; $this->app = $app;
} }
public function forFrontend(string $name) public function forFrontend(string $name)
{ {
$this->assets = $this->app->make('flarum.assets.'.$name); $this->assets = $this->container->make('flarum.assets.'.$name);
return $this; return $this;
} }