Use Config class for data from config.php

This commit is contained in:
Franz Liedke 2020-08-21 18:21:33 +02:00
parent 63f011f67a
commit 5500753278
6 changed files with 33 additions and 35 deletions

View File

@ -95,7 +95,9 @@ class Application
*/ */
public function config($key, $default = null) public function config($key, $default = null)
{ {
return Arr::get($this->container->make('flarum.config'), $key, $default); $config = $this->container->make('flarum.config');
return $config[$key] ?? $default;
} }
/** /**
@ -117,18 +119,10 @@ class Application
public function url($path = null) public function url($path = null)
{ {
$config = $this->container->make('flarum.config'); $config = $this->container->make('flarum.config');
$url = Arr::get($config, 'url', Arr::get($_SERVER, 'REQUEST_URI')); $url = (string) $config->url();
if (is_array($url)) {
if (isset($url[$path])) {
return $url[$path];
}
$url = $url['base'];
}
if ($path) { if ($path) {
$url .= '/'.Arr::get($config, "paths.$path", $path); $url .= '/'.($config["paths.$path"] ?? $path);
} }
return $url; return $url;

View File

@ -27,11 +27,11 @@ class InstalledApp implements AppInterface
protected $container; protected $container;
/** /**
* @var array * @var Config
*/ */
protected $config; protected $config;
public function __construct(Container $container, array $config) public function __construct(Container $container, Config $config)
{ {
$this->container = $container; $this->container = $container;
$this->config = $config; $this->config = $config;
@ -47,7 +47,7 @@ class InstalledApp implements AppInterface
*/ */
public function getRequestHandler() public function getRequestHandler()
{ {
if ($this->inMaintenanceMode()) { if ($this->config->inMaintenanceMode()) {
return new MaintenanceModeHandler(); return new MaintenanceModeHandler();
} elseif ($this->needsUpdate()) { } elseif ($this->needsUpdate()) {
return $this->getUpdaterHandler(); return $this->getUpdaterHandler();
@ -69,11 +69,6 @@ class InstalledApp implements AppInterface
return $pipe; return $pipe;
} }
protected function inMaintenanceMode(): bool
{
return $this->config['offline'] ?? false;
}
protected function needsUpdate(): bool protected function needsUpdate(): bool
{ {
$settings = $this->container->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
@ -98,7 +93,7 @@ class InstalledApp implements AppInterface
protected function basePath(): string protected function basePath(): string
{ {
return parse_url($this->config['url'], PHP_URL_PATH) ?: '/'; return $this->config->url()->getPath() ?: '/';
} }
protected function subPath($pathName): string protected function subPath($pathName): string

View File

@ -55,7 +55,7 @@ class InstalledSite implements SiteInterface
protected $paths; protected $paths;
/** /**
* @var array * @var Config
*/ */
protected $config; protected $config;
@ -64,7 +64,7 @@ class InstalledSite implements SiteInterface
*/ */
protected $extenders = []; protected $extenders = [];
public function __construct(Paths $paths, array $config) public function __construct(Paths $paths, Config $config)
{ {
$this->paths = $paths; $this->paths = $paths;
$this->config = $config; $this->config = $config;
@ -101,7 +101,8 @@ class InstalledSite implements SiteInterface
$container->instance('env', 'production'); $container->instance('env', 'production');
$container->instance('flarum.config', $this->config); $container->instance('flarum.config', $this->config);
$container->instance('flarum.debug', $laravel->inDebugMode()); $container->alias('flarum.config', Config::class);
$container->instance('flarum.debug', $this->config->inDebugMode());
$container->instance('config', $config = $this->getIlluminateConfig($laravel)); $container->instance('config', $config = $this->getIlluminateConfig($laravel));
$this->registerLogger($container); $this->registerLogger($container);

View File

@ -24,13 +24,13 @@ class Site
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
if (static::hasConfigFile($paths->base)) { if (! static::hasConfigFile($paths->base)) {
return ( return new UninstalledSite($paths, $_SERVER['REQUEST_URI']);
new InstalledSite($paths, static::loadConfig($paths->base))
)->extendWith(static::loadExtenders($paths->base));
} else {
return new UninstalledSite($paths);
} }
return (
new InstalledSite($paths, static::loadConfig($paths->base))
)->extendWith(static::loadExtenders($paths->base));
} }
protected static function hasConfigFile($basePath) protected static function hasConfigFile($basePath)
@ -38,7 +38,7 @@ class Site
return file_exists("$basePath/config.php"); return file_exists("$basePath/config.php");
} }
protected static function loadConfig($basePath): array protected static function loadConfig($basePath): Config
{ {
$config = include "$basePath/config.php"; $config = include "$basePath/config.php";
@ -46,7 +46,7 @@ class Site
throw new RuntimeException('config.php should return an array'); throw new RuntimeException('config.php should return an array');
} }
return $config; return new Config($config);
} }
protected static function loadExtenders($basePath): array protected static function loadExtenders($basePath): array

View File

@ -35,9 +35,15 @@ class UninstalledSite implements SiteInterface
*/ */
protected $paths; protected $paths;
public function __construct(Paths $paths) /**
* @var string
*/
private $baseUrl;
public function __construct(Paths $paths, string $baseUrl)
{ {
$this->paths = $paths; $this->paths = $paths;
$this->baseUrl = $baseUrl;
} }
/** /**
@ -58,8 +64,9 @@ class UninstalledSite implements SiteInterface
$laravel = new Application($container, $this->paths); $laravel = new Application($container, $this->paths);
$container->instance('env', 'production'); $container->instance('env', 'production');
$container->instance('flarum.config', []); $container->instance('flarum.config', new Config(['url' => $this->baseUrl]));
$container->instance('flarum.debug', $laravel->inDebugMode()); $container->alias('flarum.config', Config::class);
$container->instance('flarum.debug', true);
$container->instance('config', $config = $this->getIlluminateConfig()); $container->instance('config', $config = $this->getIlluminateConfig());
$this->registerLogger($container); $this->registerLogger($container);

View File

@ -10,6 +10,7 @@
namespace Flarum\Tests\integration; namespace Flarum\Tests\integration;
use Flarum\Extend\ExtenderInterface; use Flarum\Extend\ExtenderInterface;
use Flarum\Foundation\Config;
use Flarum\Foundation\InstalledSite; use Flarum\Foundation\InstalledSite;
use Flarum\Foundation\Paths; use Flarum\Foundation\Paths;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
@ -40,7 +41,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
'public' => __DIR__.'/tmp/public', 'public' => __DIR__.'/tmp/public',
'storage' => __DIR__.'/tmp/storage', 'storage' => __DIR__.'/tmp/storage',
]), ]),
include __DIR__.'/tmp/config.php' new Config(include __DIR__.'/tmp/config.php')
); );
$site->extendWith($this->extenders); $site->extendWith($this->extenders);