Merge pull request #1697 from flarum/fl/1578-speed-up-extenders

Do not resolve services in extenders
This commit is contained in:
Franz Liedke 2018-12-13 10:33:00 +01:00 committed by GitHub
commit b0e996e7ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 140 additions and 71 deletions

View File

@ -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;
@ -22,6 +25,8 @@ use Flarum\Http\Middleware as HttpMiddleware;
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
@ -102,11 +107,28 @@ class AdminServiceProvider extends AbstractServiceProvider
$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);
}
);
}

View File

@ -11,12 +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
@ -107,11 +113,29 @@ class Frontend implements ExtenderInterface
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);
}
);
}
}
@ -122,15 +146,20 @@ class Frontend implements ExtenderInterface
return;
}
$routes = $container->make("flarum.$this->frontend.routes");
$factory = $container->make(RouteHandlerFactory::class);
$container->resolving(
"flarum.{$this->frontend}.routes",
function (RouteCollection $collection, Container $container) {
/** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class);
foreach ($this->routes as $route) {
$routes->get(
$route['path'], $route['name'],
$factory->toFrontend($this->frontend, $route['content'])
);
}
foreach ($this->routes as $route) {
$collection->get(
$route['path'], $route['name'],
$factory->toFrontend($this->frontend, $route['content'])
);
}
}
);
}
private function registerContent(Container $container)

View File

@ -37,8 +37,16 @@ class LanguagePack implements ExtenderInterface, LifecycleInterface
);
}
/** @var LocaleManager $locales */
$locales = $container->make(LocaleManager::class);
$container->resolving(
LocaleManager::class,
function (LocaleManager $locales) use ($extension, $locale, $title) {
$this->registerLocale($locales, $extension, $locale, $title);
}
);
}
private function registerLocale(LocaleManager $locales, Extension $extension, $locale, $title)
{
$locales->addLocale($locale, $title);
$directory = $extension->getPath().'/locale';

View File

@ -27,33 +27,35 @@ class Locales implements ExtenderInterface, LifecycleInterface
public function extend(Container $container, Extension $extension = null)
{
/** @var LocaleManager $locales */
$locales = $container->make(LocaleManager::class);
$container->resolving(
LocaleManager::class,
function (LocaleManager $locales) {
foreach (new DirectoryIterator($this->directory) as $file) {
if (! $file->isFile()) {
continue;
}
foreach (new DirectoryIterator($this->directory) as $file) {
if (! $file->isFile()) {
continue;
$extension = $file->getExtension();
if (! in_array($extension, ['yml', 'yaml'])) {
continue;
}
$locales->addTranslations(
$file->getBasename(".$extension"),
$file->getPathname()
);
}
}
$extension = $file->getExtension();
if (! in_array($extension, ['yml', 'yaml'])) {
continue;
}
$locales->addTranslations(
$file->getBasename(".$extension"),
$file->getPathname()
);
}
);
}
public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
$container->make(LocaleManager::class)->clearCache();
}
public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
$container->make(LocaleManager::class)->clearCache();
}
}

View File

@ -12,6 +12,7 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
@ -69,19 +70,21 @@ class Routes implements ExtenderInterface
return;
}
/** @var \Flarum\Http\RouteCollection $collection */
$collection = $container->make("flarum.{$this->appName}.routes");
$container->resolving(
"flarum.{$this->appName}.routes",
function (RouteCollection $collection, Container $container) {
/** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class);
/** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class);
foreach ($this->routes as $route) {
$collection->addRoute(
$route['method'],
$route['path'],
$route['name'],
$factory->toController($route['handler'])
);
}
foreach ($this->routes as $route) {
$collection->addRoute(
$route['method'],
$route['path'],
$route['name'],
$factory->toController($route['handler'])
);
}
}
);
}
}

View File

@ -13,9 +13,12 @@ namespace Flarum\Forum;
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;
@ -25,6 +28,8 @@ use Flarum\Http\Middleware as HttpMiddleware;
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;
@ -116,11 +121,26 @@ class ForumServiceProvider extends AbstractServiceProvider
$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(

View File

@ -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
{
@ -40,17 +36,6 @@ class RecompileFrontendAssets
$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))) {