1
0
mirror of https://github.com/flarum/core.git synced 2025-08-01 06:00:24 +02:00

Extract a class to hold / determine paths

This commit is contained in:
Franz Liedke
2020-05-01 14:23:44 +02:00
parent d31a747631
commit d0ae2839f0
5 changed files with 160 additions and 33 deletions

View File

@@ -51,7 +51,7 @@ use Psr\Log\LoggerInterface;
class InstalledSite implements SiteInterface
{
/**
* @var array
* @var Paths
*/
private $paths;
@@ -65,7 +65,7 @@ class InstalledSite implements SiteInterface
*/
private $extenders = [];
public function __construct(array $paths, array $config)
public function __construct(Paths $paths, array $config)
{
$this->paths = $paths;
$this->config = $config;
@@ -97,13 +97,10 @@ class InstalledSite implements SiteInterface
private function bootLaravel(): Application
{
$laravel = new Application($this->paths['base'], $this->paths['public']);
$laravel = new Application($this->paths->base, $this->paths->public);
$laravel->useStoragePath($this->paths['storage']);
if (isset($this->paths['vendor'])) {
$laravel->useVendorPath($this->paths['vendor']);
}
$laravel->useStoragePath($this->paths->storage);
$laravel->useVendorPath($this->paths->vendor);
$laravel->instance('env', 'production');
$laravel->instance('flarum.config', $this->config);
@@ -164,7 +161,7 @@ class InstalledSite implements SiteInterface
return new ConfigRepository([
'view' => [
'paths' => [],
'compiled' => $this->paths['storage'].'/views',
'compiled' => $this->paths->storage.'/views',
],
'mail' => [
'driver' => 'mail',
@@ -175,18 +172,18 @@ class InstalledSite implements SiteInterface
'disks' => [
'flarum-assets' => [
'driver' => 'local',
'root' => $this->paths['public'].'/assets',
'root' => $this->paths->public.'/assets',
'url' => $app->url('assets')
],
'flarum-avatars' => [
'driver' => 'local',
'root' => $this->paths['public'].'/assets/avatars'
'root' => $this->paths->public.'/assets/avatars'
]
]
],
'session' => [
'lifetime' => 120,
'files' => $this->paths['storage'].'/sessions',
'files' => $this->paths->storage.'/sessions',
'cookie' => 'session'
]
]);
@@ -194,7 +191,7 @@ class InstalledSite implements SiteInterface
private function registerLogger(Application $app)
{
$logPath = $this->paths['storage'].'/logs/flarum.log';
$logPath = $this->paths->storage.'/logs/flarum.log';
$handler = new RotatingFileHandler($logPath, Logger::INFO);
$handler->setFormatter(new LineFormatter(null, null, true, true));
@@ -210,7 +207,7 @@ class InstalledSite implements SiteInterface
$app->alias('cache.store', Repository::class);
$app->singleton('cache.filestore', function () {
return new FileStore(new Filesystem, $this->paths['storage'].'/cache');
return new FileStore(new Filesystem, $this->paths->storage.'/cache');
});
$app->alias('cache.filestore', Store::class);
}

44
src/Foundation/Paths.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation;
use InvalidArgumentException;
/**
* @property-read string base
* @property-read string public
* @property-read string storage
* @property-read string vendor
*/
class Paths
{
private $paths;
public function __construct(array $paths)
{
if (! isset($paths['base'], $paths['public'], $paths['storage'])) {
throw new InvalidArgumentException(
'Paths array requires keys base, public and storage'
);
}
$this->paths = array_map(function ($path) {
return rtrim($path, '\/');
}, $paths);
// Assume a standard Composer directory structure unless specified
$this->paths['vendor'] = $this->vendor ?? $this->base.DIRECTORY_SEPARATOR.'vendor';
}
public function __get($name): ?string
{
return $this->paths[$name] ?? null;
}
}

View File

@@ -9,7 +9,6 @@
namespace Flarum\Foundation;
use InvalidArgumentException;
use RuntimeException;
class Site
@@ -20,18 +19,14 @@ class Site
*/
public static function fromPaths(array $paths)
{
if (! isset($paths['base'], $paths['public'], $paths['storage'])) {
throw new InvalidArgumentException(
'Paths array requires keys base, public and storage'
);
}
$paths = new Paths($paths);
date_default_timezone_set('UTC');
if (static::hasConfigFile($paths['base'])) {
if (static::hasConfigFile($paths->base)) {
return (
new InstalledSite($paths, static::loadConfig($paths['base']))
)->extendWith(static::loadExtenders($paths['base']));
new InstalledSite($paths, static::loadConfig($paths->base))
)->extendWith(static::loadExtenders($paths->base));
} else {
return new UninstalledSite($paths);
}

View File

@@ -30,11 +30,11 @@ use Psr\Log\LoggerInterface;
class UninstalledSite implements SiteInterface
{
/**
* @var array
* @var Paths
*/
private $paths;
public function __construct(array $paths)
public function __construct(Paths $paths)
{
$this->paths = $paths;
}
@@ -53,13 +53,10 @@ class UninstalledSite implements SiteInterface
private function bootLaravel(): Application
{
$laravel = new Application($this->paths['base'], $this->paths['public']);
$laravel = new Application($this->paths->base, $this->paths->public);
$laravel->useStoragePath($this->paths['storage']);
if (isset($this->paths['vendor'])) {
$laravel->useVendorPath($this->paths['vendor']);
}
$laravel->useStoragePath($this->paths->storage);
$laravel->useVendorPath($this->paths->vendor);
$laravel->instance('env', 'production');
$laravel->instance('flarum.config', []);
@@ -108,7 +105,7 @@ class UninstalledSite implements SiteInterface
return new ConfigRepository([
'session' => [
'lifetime' => 120,
'files' => $this->paths['storage'].'/sessions',
'files' => $this->paths->storage.'/sessions',
'cookie' => 'session'
],
'view' => [
@@ -119,7 +116,7 @@ class UninstalledSite implements SiteInterface
private function registerLogger(Application $app)
{
$logPath = $this->paths['storage'].'/logs/flarum-installer.log';
$logPath = $this->paths->storage.'/logs/flarum-installer.log';
$handler = new StreamHandler($logPath, Logger::DEBUG);
$handler->setFormatter(new LineFormatter(null, null, true, true));