1
0
mirror of https://github.com/flarum/core.git synced 2025-05-31 11:39:56 +02:00
php-flarum/src/Forum/ForumServiceProvider.php
2017-10-03 18:45:40 +02:00

113 lines
3.0 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Forum;
use Flarum\Event\ConfigureForumRoutes;
use Flarum\Event\ExtensionWasDisabled;
use Flarum\Event\ExtensionWasEnabled;
use Flarum\Event\SettingWasSet;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Http\Handler\RouteHandlerFactory;
use Flarum\Http\RouteCollection;
class ForumServiceProvider extends AbstractServiceProvider
{
/**
* {@inheritdoc}
*/
public function register()
{
$this->app->singleton(UrlGenerator::class, function () {
return new UrlGenerator($this->app, $this->app->make('flarum.forum.routes'));
});
$this->app->singleton('flarum.forum.routes', function () {
return new RouteCollection;
});
}
/**
* {@inheritdoc}
*/
public function boot()
{
$this->populateRoutes($this->app->make('flarum.forum.routes'));
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum');
$this->flushWebAppAssetsWhenThemeChanged();
$this->flushWebAppAssetsWhenExtensionsChanged();
}
/**
* Populate the forum client routes.
*
* @param RouteCollection $routes
*/
protected function populateRoutes(RouteCollection $routes)
{
$factory = $this->app->make(RouteHandlerFactory::class);
$callback = include __DIR__.'/routes.php';
$callback($routes, $factory);
$this->app->make('events')->fire(
new ConfigureForumRoutes($routes, $factory)
);
$defaultRoute = $this->app->make('flarum.settings')->get('default_route');
if (isset($routes->getRouteData()[0]['GET'][$defaultRoute])) {
$toDefaultController = $routes->getRouteData()[0]['GET'][$defaultRoute];
} else {
$toDefaultController = $factory->toController(Controller\IndexController::class);
}
$routes->get(
'/',
'default',
$toDefaultController
);
}
protected function flushWebAppAssetsWhenThemeChanged()
{
$this->app->make('events')->listen(SettingWasSet::class, function (SettingWasSet $event) {
if (preg_match('/^theme_|^custom_less$/i', $event->key)) {
$this->getWebAppAssets()->flushCss();
}
});
}
protected function flushWebAppAssetsWhenExtensionsChanged()
{
$events = $this->app->make('events');
$events->listen(ExtensionWasEnabled::class, [$this, 'flushWebAppAssets']);
$events->listen(ExtensionWasDisabled::class, [$this, 'flushWebAppAssets']);
}
public function flushWebAppAssets()
{
$this->getWebAppAssets()->flush();
}
/**
* @return \Flarum\Frontend\FrontendAssets
*/
protected function getWebAppAssets()
{
return $this->app->make(Frontend::class)->getAssets();
}
}