Skip to content

Commit

Permalink
Inject dependencies when firing events, not before
Browse files Browse the repository at this point in the history
The event subscriber approach means that dependencies have to be
injected (and thus instantiated, along with all *their* dependencies) at
the time of registering event listeners - even when events are never
fired within a request's lifecycle.

This is unnecessary and causes more classes than necessary to be loaded.

In this case, we can explicitly register event listeners that will
resolve their dependencies when the event is fired, not before.

Refs #1578.
  • Loading branch information
franzliedke committed Dec 13, 2018
1 parent ed02eed commit b41d9fb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
32 changes: 27 additions & 5 deletions src/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Flarum\Admin;

use Flarum\Event\ConfigureMiddleware;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Application;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Frontend\AddLocaleAssets;
use Flarum\Frontend\AddTranslations;
use Flarum\Frontend\Compiler\Source\SourceCollector;
Expand All @@ -22,6 +25,8 @@
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Zend\Stratigility\MiddlewarePipe;

class AdminServiceProvider extends AbstractServiceProvider
Expand Down Expand Up @@ -102,11 +107,28 @@ public function boot()

$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');

$this->app->make('events')->subscribe(
new RecompileFrontendAssets(
$this->app->make('flarum.assets.admin'),
$this->app->make('flarum.locales')
)
$events = $this->app->make('events');

$events->listen(
[Enabled::class, Disabled::class, ClearingCache::class],
function () {
$recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.admin'),
$this->app->make(LocaleManager::class)
);
$recompile->flush();
}
);

$events->listen(
Saved::class,
function (Saved $event) {
$recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.admin'),
$this->app->make(LocaleManager::class)
);
$recompile->whenSettingsSaved($event);
}
);
}

Expand Down
33 changes: 28 additions & 5 deletions src/Extend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace Flarum\Extend;

use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Extension\Extension;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Frontend\Assets;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\Frontend as ActualFrontend;
use Flarum\Frontend\RecompileFrontendAssets;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Illuminate\Contracts\Container\Container;

class Frontend implements ExtenderInterface
Expand Down Expand Up @@ -108,11 +113,29 @@ private function registerAssets(Container $container, string $moduleName)
return $container->make('flarum.assets.factory')($this->frontend);
});

$container->make('events')->subscribe(
new RecompileFrontendAssets(
$container->make($abstract),
$container->make('flarum.locales')
)
/** @var \Illuminate\Contracts\Events\Dispatcher $events */
$events = $container->make('events');

$events->listen(
[Enabled::class, Disabled::class, ClearingCache::class],
function () use ($container, $abstract) {
$recompile = new RecompileFrontendAssets(
$container->make($abstract),
$container->make(LocaleManager::class)
);
$recompile->flush();
}
);

$events->listen(
Saved::class,
function (Saved $event) use ($container, $abstract) {
$recompile = new RecompileFrontendAssets(
$container->make($abstract),
$container->make(LocaleManager::class)
);
$recompile->whenSettingsSaved($event);
}
);
}
}
Expand Down
30 changes: 25 additions & 5 deletions src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use Flarum\Event\ConfigureForumRoutes;
use Flarum\Event\ConfigureMiddleware;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Formatter\Formatter;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Application;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Frontend\AddLocaleAssets;
use Flarum\Frontend\AddTranslations;
use Flarum\Frontend\Assets;
Expand All @@ -25,6 +28,8 @@
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Flarum\Settings\SettingsRepositoryInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Zend\Stratigility\MiddlewarePipe;
Expand Down Expand Up @@ -116,11 +121,26 @@ public function boot()

$events = $this->app->make('events');

$events->subscribe(
new RecompileFrontendAssets(
$this->app->make('flarum.assets.forum'),
$this->app->make('flarum.locales')
)
$events->listen(
[Enabled::class, Disabled::class, ClearingCache::class],
function () {
$recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.forum'),
$this->app->make(LocaleManager::class)
);
$recompile->flush();
}
);

$events->listen(
Saved::class,
function (Saved $event) {
$recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.forum'),
$this->app->make(LocaleManager::class)
);
$recompile->whenSettingsSaved($event);
}
);

$events->subscribe(
Expand Down
15 changes: 0 additions & 15 deletions src/Frontend/RecompileFrontendAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@

namespace Flarum\Frontend;

use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Illuminate\Contracts\Events\Dispatcher;

class RecompileFrontendAssets
{
Expand All @@ -40,17 +36,6 @@ public function __construct(Assets $assets, LocaleManager $locales)
$this->locales = $locales;
}

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Saved::class, [$this, 'whenSettingsSaved']);
$events->listen(Enabled::class, [$this, 'flush']);
$events->listen(Disabled::class, [$this, 'flush']);
$events->listen(ClearingCache::class, [$this, 'flush']);
}

public function whenSettingsSaved(Saved $event)
{
if (preg_grep('/^theme_/i', array_keys($event->settings))) {
Expand Down

0 comments on commit b41d9fb

Please sign in to comment.