From d0ae2839f09c29e91c3137e21d9626826443bbb2 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 1 May 2020 14:23:44 +0200 Subject: [PATCH] Extract a class to hold / determine paths --- src/Foundation/InstalledSite.php | 25 ++++---- src/Foundation/Paths.php | 44 ++++++++++++++ src/Foundation/Site.php | 13 ++-- src/Foundation/UninstalledSite.php | 17 +++--- tests/unit/Foundation/PathsTest.php | 94 +++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 33 deletions(-) create mode 100644 src/Foundation/Paths.php create mode 100644 tests/unit/Foundation/PathsTest.php diff --git a/src/Foundation/InstalledSite.php b/src/Foundation/InstalledSite.php index d61037767..2afb716c7 100644 --- a/src/Foundation/InstalledSite.php +++ b/src/Foundation/InstalledSite.php @@ -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); } diff --git a/src/Foundation/Paths.php b/src/Foundation/Paths.php new file mode 100644 index 000000000..9f7186a4e --- /dev/null +++ b/src/Foundation/Paths.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/src/Foundation/Site.php b/src/Foundation/Site.php index ba44d459c..13b715b2c 100644 --- a/src/Foundation/Site.php +++ b/src/Foundation/Site.php @@ -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); } diff --git a/src/Foundation/UninstalledSite.php b/src/Foundation/UninstalledSite.php index 64c1b329c..5e8f61f58 100644 --- a/src/Foundation/UninstalledSite.php +++ b/src/Foundation/UninstalledSite.php @@ -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)); diff --git a/tests/unit/Foundation/PathsTest.php b/tests/unit/Foundation/PathsTest.php new file mode 100644 index 000000000..7e6faada5 --- /dev/null +++ b/tests/unit/Foundation/PathsTest.php @@ -0,0 +1,94 @@ +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); + } +}