mirror of
https://github.com/flarum/framework.git
synced 2024-11-27 19:13:37 +08:00
Merge pull request #1697 from flarum/fl/1578-speed-up-extenders
Do not resolve services in extenders
This commit is contained in:
commit
b0e996e7ff
|
@ -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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'])
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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))) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user