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 afc0fae966
commit af932c3c20
5 changed files with 47 additions and 33 deletions

View File

@ -9,9 +9,9 @@
namespace Flarum\Console\Event;
use Flarum\Foundation\Application;
use Illuminate\Console\Command;
use Symfony\Component\Console\Application as ConsoleApplication;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Application;
/**
* @deprecated
@ -19,22 +19,22 @@ use Symfony\Component\Console\Application as ConsoleApplication;
class Configuring
{
/**
* @var Application
* @var Container
*/
public $app;
/**
* @var ConsoleApplication
* @var Application
*/
public $console;
/**
* @param Application $app
* @param ConsoleApplication $console
* @param Container $container
* @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;
}

View File

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

View File

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

View File

@ -31,6 +31,8 @@ class ExtensionManager
protected $app;
protected $container;
protected $migrator;
/**
@ -51,12 +53,14 @@ class ExtensionManager
public function __construct(
SettingsRepositoryInterface $config,
Application $app,
Container $container,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
) {
$this->config = $config;
$this->app = $app;
$this->container = $container;
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
@ -138,7 +142,7 @@ class ExtensionManager
$this->setEnabled($enabled);
$extension->enable($this->app);
$extension->enable($this->container);
$this->dispatcher->dispatch(new Enabled($extension));
}
@ -164,7 +168,7 @@ class ExtensionManager
$this->setEnabled($enabled);
$extension->disable($this->app);
$extension->disable($this->container);
$this->dispatcher->dispatch(new Disabled($extension));
}
@ -235,7 +239,7 @@ class ExtensionManager
*/
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();
});

View File

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