1
0
mirror of https://github.com/flarum/core.git synced 2025-07-22 09:11:19 +02:00

Inject/use new config class where applicable

This commit is contained in:
Franz Liedke
2020-03-28 11:03:19 +01:00
parent 6c91ade4c6
commit 63f011f67a
10 changed files with 47 additions and 52 deletions

View File

@@ -63,7 +63,7 @@ class AdminServiceProvider extends AbstractServiceProvider
$this->app->bind('flarum.admin.error_handler', function () { $this->app->bind('flarum.admin.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->app->make(Registry::class),
$this->app['flarum']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->app['flarum.config']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class),
$this->app->tagged(Reporter::class) $this->app->tagged(Reporter::class)
); );
}); });

View File

@@ -59,7 +59,7 @@ class ApiServiceProvider extends AbstractServiceProvider
$this->app->bind('flarum.api.error_handler', function () { $this->app->bind('flarum.api.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->app->make(Registry::class),
new JsonApiFormatter($this->app['flarum']->inDebugMode()), new JsonApiFormatter($this->app['flarum.config']->inDebugMode()),
$this->app->tagged(Reporter::class) $this->app->tagged(Reporter::class)
); );
}); });

View File

@@ -10,6 +10,7 @@
namespace Flarum\Api\Serializer; namespace Flarum\Api\Serializer;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Foundation\Config;
use Flarum\Http\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
@@ -21,9 +22,9 @@ class ForumSerializer extends AbstractSerializer
protected $type = 'forums'; protected $type = 'forums';
/** /**
* @var Application * @var Config
*/ */
protected $app; protected $config;
/** /**
* @var SettingsRepositoryInterface * @var SettingsRepositoryInterface
@@ -36,13 +37,13 @@ class ForumSerializer extends AbstractSerializer
protected $url; protected $url;
/** /**
* @param Application $app * @param Config $config
* @param SettingsRepositoryInterface $settings * @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url * @param UrlGenerator $url
*/ */
public function __construct(Application $app, SettingsRepositoryInterface $settings, UrlGenerator $url) public function __construct(Config $config, SettingsRepositoryInterface $settings, UrlGenerator $url)
{ {
$this->app = $app; $this->config = $config;
$this->settings = $settings; $this->settings = $settings;
$this->url = $url; $this->url = $url;
} }
@@ -66,7 +67,7 @@ class ForumSerializer extends AbstractSerializer
'showLanguageSelector' => (bool) $this->settings->get('show_language_selector', true), 'showLanguageSelector' => (bool) $this->settings->get('show_language_selector', true),
'baseUrl' => $url = $this->url->to('forum')->base(), 'baseUrl' => $url = $this->url->to('forum')->base(),
'basePath' => parse_url($url, PHP_URL_PATH) ?: '', 'basePath' => parse_url($url, PHP_URL_PATH) ?: '',
'debug' => $this->app->inDebugMode(), 'debug' => $this->config->inDebugMode(),
'apiUrl' => $this->url->to('api')->base(), 'apiUrl' => $this->url->to('api')->base(),
'welcomeTitle' => $this->settings->get('welcome_title'), 'welcomeTitle' => $this->settings->get('welcome_title'),
'welcomeMessage' => $this->settings->get('welcome_message'), 'welcomeMessage' => $this->settings->get('welcome_message'),

View File

@@ -14,6 +14,7 @@ use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand; use Flarum\Database\Console\ResetCommand;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Console\CacheClearCommand; use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Foundation\Console\InfoCommand;
class ConsoleServiceProvider extends AbstractServiceProvider class ConsoleServiceProvider extends AbstractServiceProvider
{ {
@@ -26,6 +27,7 @@ class ConsoleServiceProvider extends AbstractServiceProvider
return [ return [
CacheClearCommand::class, CacheClearCommand::class,
GenerateMigrationCommand::class, GenerateMigrationCommand::class,
InfoCommand::class,
MigrateCommand::class, MigrateCommand::class,
ResetCommand::class, ResetCommand::class,
]; ];

View File

@@ -73,7 +73,7 @@ class ForumServiceProvider extends AbstractServiceProvider
$this->app->bind('flarum.forum.error_handler', function () { $this->app->bind('flarum.forum.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->app->make(Registry::class),
$this->app['flarum']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->app['flarum.config']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class),
$this->app->tagged(Reporter::class) $this->app->tagged(Reporter::class)
); );
}); });

View File

@@ -12,6 +12,7 @@ namespace Flarum\Foundation\Console;
use Flarum\Console\AbstractCommand; use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Foundation\Config;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableStyle; use Symfony\Component\Console\Helper\TableStyle;
@@ -23,15 +24,15 @@ class InfoCommand extends AbstractCommand
protected $extensions; protected $extensions;
/** /**
* @var array * @var Config
*/ */
protected $config; protected $config;
/** /**
* @param ExtensionManager $extensions * @param ExtensionManager $extensions
* @param array $config * @param Config config
*/ */
public function __construct(ExtensionManager $extensions, array $config) public function __construct(ExtensionManager $extensions, Config $config)
{ {
$this->extensions = $extensions; $this->extensions = $extensions;
$this->config = $config; $this->config = $config;
@@ -64,11 +65,11 @@ class InfoCommand extends AbstractCommand
$this->getExtensionTable()->render(); $this->getExtensionTable()->render();
$this->output->writeln('<info>Base URL:</info> '.$this->config['url']); $this->output->writeln('<info>Base URL:</info> '.$this->config->url());
$this->output->writeln('<info>Installation path:</info> '.getcwd()); $this->output->writeln('<info>Installation path:</info> '.getcwd());
$this->output->writeln('<info>Debug mode:</info> '.($this->config['debug'] ? 'ON' : 'off')); $this->output->writeln('<info>Debug mode:</info> '.($this->config->inDebugMode() ? 'ON' : 'off'));
if ($this->config['debug']) { if ($this->config->inDebugMode()) {
$this->error( $this->error(
"Don't forget to turn off debug mode! It should never be turned on in a production system." "Don't forget to turn off debug mode! It should never be turned on in a production system."
); );

View File

@@ -9,7 +9,6 @@
namespace Flarum\Foundation; namespace Flarum\Foundation;
use Flarum\Foundation\Console\InfoCommand;
use Flarum\Http\Middleware\DispatchRoute; use Flarum\Http\Middleware\DispatchRoute;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@@ -112,21 +111,14 @@ class InstalledApp implements AppInterface
*/ */
public function getConsoleCommands() public function getConsoleCommands()
{ {
$commands = []; return array_map(function ($command) {
$command = $this->container->make($command);
// The info command is a special case, as it requires a config parameter that's only available here. if ($command instanceof Command) {
$commands[] = $this->container->make(InfoCommand::class, ['config' => $this->config]); $command->setLaravel($this->container);
foreach ($this->container->make('flarum.console.commands') as $command) {
$newCommand = $this->container->make($command);
if ($newCommand instanceof Command) {
$newCommand->setLaravel($this->container);
} }
$commands[] = $newCommand; return $command;
} }, $this->container->make('flarum.console.commands'));
return $commands;
} }
} }

View File

@@ -9,7 +9,7 @@
namespace Flarum\Frontend\Content; namespace Flarum\Frontend\Content;
use Flarum\Foundation\Application; use Flarum\Foundation\Config;
use Flarum\Frontend\Compiler\CompilerInterface; use Flarum\Frontend\Compiler\CompilerInterface;
use Flarum\Frontend\Document; use Flarum\Frontend\Document;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
@@ -19,17 +19,17 @@ use Psr\Http\Message\ServerRequestInterface as Request;
class Assets class Assets
{ {
protected $container; protected $container;
protected $app; protected $config;
/** /**
* @var \Flarum\Frontend\Assets * @var \Flarum\Frontend\Assets
*/ */
protected $assets; protected $assets;
public function __construct(Container $container, Application $app) public function __construct(Container $container, Config $config)
{ {
$this->container = $container; $this->container = $container;
$this->app = $app; $this->config = $config;
} }
public function forFrontend(string $name) public function forFrontend(string $name)
@@ -48,7 +48,7 @@ class Assets
'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)] 'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)]
]; ];
if ($this->app->inDebugMode()) { if ($this->config->inDebugMode()) {
$this->commit(Arr::flatten($compilers)); $this->commit(Arr::flatten($compilers));
} }

View File

@@ -11,8 +11,7 @@ namespace Flarum\Http;
use Dflydev\FigCookies\Modifier\SameSite; use Dflydev\FigCookies\Modifier\SameSite;
use Dflydev\FigCookies\SetCookie; use Dflydev\FigCookies\SetCookie;
use Flarum\Foundation\Application; use Flarum\Foundation\Config;
use Illuminate\Support\Arr;
class CookieFactory class CookieFactory
{ {
@@ -52,19 +51,19 @@ class CookieFactory
protected $samesite; protected $samesite;
/** /**
* @param Application $app * @param Config $config
*/ */
public function __construct(Application $app) public function __construct(Config $config)
{ {
// Parse the forum's base URL so that we can determine the optimal cookie settings // If necessary, we will use the forum's base URL to determine smart defaults for cookie settings
$url = parse_url(rtrim($app->url(), '/')); $url = $config->url();
// Get the cookie settings from the config or use the default values // Get the cookie settings from the config or use the default values
$this->prefix = $app->config('cookie.name', 'flarum'); $this->prefix = $config['cookie.name'] ?? 'flarum';
$this->path = $app->config('cookie.path', Arr::get($url, 'path') ?: '/'); $this->path = $config['cookie.path'] ?? $url->getPath() ?: '/';
$this->domain = $app->config('cookie.domain'); $this->domain = $config['cookie.domain'];
$this->secure = $app->config('cookie.secure', Arr::get($url, 'scheme') === 'https'); $this->secure = $config['cookie.secure'] ?? $url->getScheme() === 'https';
$this->samesite = $app->config('cookie.samesite'); $this->samesite = $config['cookie.samesite'];
} }
/** /**

View File

@@ -11,7 +11,7 @@ namespace Flarum\Update\Controller;
use Exception; use Exception;
use Flarum\Database\Console\MigrateCommand; use Flarum\Database\Console\MigrateCommand;
use Flarum\Foundation\Application; use Flarum\Foundation\Config;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Laminas\Diactoros\Response; use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\HtmlResponse; use Laminas\Diactoros\Response\HtmlResponse;
@@ -26,18 +26,18 @@ class UpdateController implements RequestHandlerInterface
protected $command; protected $command;
/** /**
* @var Application * @var Config
*/ */
protected $app; protected $config;
/** /**
* @param MigrateCommand $command * @param MigrateCommand $command
* @param Application $app * @param Config $config
*/ */
public function __construct(MigrateCommand $command, Application $app) public function __construct(MigrateCommand $command, Config $config)
{ {
$this->command = $command; $this->command = $command;
$this->app = $app; $this->config = $config;
} }
/** /**
@@ -48,7 +48,7 @@ class UpdateController implements RequestHandlerInterface
{ {
$input = $request->getParsedBody(); $input = $request->getParsedBody();
if (Arr::get($input, 'databasePassword') !== $this->app->config('database.password')) { if (Arr::get($input, 'databasePassword') !== $this->config['database.password']) {
return new HtmlResponse('Incorrect database password.', 500); return new HtmlResponse('Incorrect database password.', 500);
} }