Extract a class to hold / determine paths

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

View File

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

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

View File

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

View File

@ -0,0 +1,94 @@
<?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\Tests\unit\Foundation;
use Flarum\Foundation\Paths;
use Flarum\Tests\unit\TestCase;
use InvalidArgumentException;
class PathsTest extends TestCase
{
/** @test */
public function it_complains_when_paths_are_missing()
{
$this->expectException(InvalidArgumentException::class);
new Paths([
'base' => '/var/www/flarum',
]);
}
/** @test */
public function it_makes_paths_available_as_properties()
{
$paths = new Paths([
'base' => '/var/www/flarum',
'public' => '/var/www/flarum/public',
'storage' => '/var/www/flarum/storage',
]);
$this->assertEquals('/var/www/flarum', $paths->base);
$this->assertEquals('/var/www/flarum/public', $paths->public);
$this->assertEquals('/var/www/flarum/storage', $paths->storage);
}
/** @test */
public function it_derives_the_vendor_dir_from_the_base_path()
{
$paths = new Paths([
'base' => '/var/www/flarum',
'public' => '/var/www/flarum/public',
'storage' => '/var/www/flarum/storage',
]);
$this->assertEquals('/var/www/flarum/vendor', $paths->vendor);
}
/** @test */
public function it_allows_setting_a_custom_vendor_dir()
{
$paths = new Paths([
'base' => '/var/www/flarum',
'public' => '/var/www/flarum/public',
'storage' => '/var/www/flarum/storage',
'vendor' => '/share/composer-vendor',
]);
$this->assertEquals('/share/composer-vendor', $paths->vendor);
}
/** @test */
public function it_strips_trailing_forward_slashes_from_paths()
{
$paths = new Paths([
'base' => '/var/www/flarum/',
'public' => '/var/www/flarum/public/',
'storage' => '/var/www/flarum/storage/',
]);
$this->assertEquals('/var/www/flarum', $paths->base);
$this->assertEquals('/var/www/flarum/public', $paths->public);
$this->assertEquals('/var/www/flarum/storage', $paths->storage);
}
/** @test */
public function it_strips_trailing_backslashes_from_paths()
{
$paths = new Paths([
'base' => 'C:\\flarum\\',
'public' => 'C:\\flarum\\public\\',
'storage' => 'C:\\flarum\\storage\\',
]);
$this->assertEquals('C:\\flarum', $paths->base);
$this->assertEquals('C:\\flarum\\public', $paths->public);
$this->assertEquals('C:\\flarum\\storage', $paths->storage);
}
}