diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php index e429d62d8..246dbc0ce 100644 --- a/framework/core/src/Install/Console/InstallCommand.php +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -80,21 +80,16 @@ class InstallCommand extends AbstractCommand { $this->init(); - $prerequisites = $this->installation->prerequisites(); - $prerequisites->check(); - $errors = $prerequisites->getErrors(); + $problems = $this->installation->prerequisites()->problems(); - if (empty($errors)) { + if ($problems->isEmpty()) { $this->info('Installing Flarum...'); $this->install(); $this->info('DONE.'); } else { - $this->output->writeln( - 'Please fix the following errors before we can continue with the installation.' - ); - $this->showErrors($errors); + $this->showProblems($problems); } } @@ -178,13 +173,17 @@ class InstallCommand extends AbstractCommand ->run(); } - protected function showErrors($errors) + protected function showProblems($problems) { - foreach ($errors as $error) { - $this->info($error['message']); + $this->output->writeln( + 'Please fix the following problems before we can continue with the installation.' + ); - if (isset($error['detail'])) { - $this->output->writeln(''.$error['detail'].''); + foreach ($problems as $problem) { + $this->info($problem['message']); + + if (isset($problem['detail'])) { + $this->output->writeln(''.$problem['detail'].''); } } } diff --git a/framework/core/src/Install/Controller/IndexController.php b/framework/core/src/Install/Controller/IndexController.php index 03b1e2449..3d66789d2 100644 --- a/framework/core/src/Install/Controller/IndexController.php +++ b/framework/core/src/Install/Controller/IndexController.php @@ -46,14 +46,12 @@ class IndexController extends AbstractHtmlController { $view = $this->view->make('flarum.install::app')->with('title', 'Install Flarum'); - $prerequisites = $this->installation->prerequisites(); - $prerequisites->check(); - $errors = $prerequisites->getErrors(); + $problems = $this->installation->prerequisites()->problems(); - if (count($errors)) { - $view->with('content', $this->view->make('flarum.install::errors')->with('errors', $errors)); - } else { + if ($problems->isEmpty()) { $view->with('content', $this->view->make('flarum.install::install')); + } else { + $view->with('content', $this->view->make('flarum.install::problems')->with('problems', $problems)); } return $view; diff --git a/framework/core/src/Install/Prerequisite/AbstractPrerequisite.php b/framework/core/src/Install/Prerequisite/AbstractPrerequisite.php deleted file mode 100644 index 06c6210b1..000000000 --- a/framework/core/src/Install/Prerequisite/AbstractPrerequisite.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Install\Prerequisite; - -abstract class AbstractPrerequisite implements PrerequisiteInterface -{ - protected $errors = []; - - abstract public function check(); - - public function getErrors() - { - return $this->errors; - } -} diff --git a/framework/core/src/Install/Prerequisite/Composite.php b/framework/core/src/Install/Prerequisite/Composite.php index ed0fb77e4..ed3a36e07 100644 --- a/framework/core/src/Install/Prerequisite/Composite.php +++ b/framework/core/src/Install/Prerequisite/Composite.php @@ -11,6 +11,8 @@ namespace Flarum\Install\Prerequisite; +use Illuminate\Support\Collection; + class Composite implements PrerequisiteInterface { /** @@ -25,21 +27,14 @@ class Composite implements PrerequisiteInterface } } - public function check() + public function problems(): Collection { return array_reduce( $this->prerequisites, - function ($previous, PrerequisiteInterface $prerequisite) { - return $prerequisite->check() && $previous; + function (Collection $errors, PrerequisiteInterface $condition) { + return $errors->concat($condition->problems()); }, - true + collect() ); } - - public function getErrors() - { - return collect($this->prerequisites)->map(function (PrerequisiteInterface $prerequisite) { - return $prerequisite->getErrors(); - })->reduce('array_merge', []); - } } diff --git a/framework/core/src/Install/Prerequisite/PhpExtensions.php b/framework/core/src/Install/Prerequisite/PhpExtensions.php index 434eff927..bac5f0d47 100644 --- a/framework/core/src/Install/Prerequisite/PhpExtensions.php +++ b/framework/core/src/Install/Prerequisite/PhpExtensions.php @@ -11,7 +11,9 @@ namespace Flarum\Install\Prerequisite; -class PhpExtensions extends AbstractPrerequisite +use Illuminate\Support\Collection; + +class PhpExtensions implements PrerequisiteInterface { protected $extensions; @@ -20,14 +22,15 @@ class PhpExtensions extends AbstractPrerequisite $this->extensions = $extensions; } - public function check() + public function problems(): Collection { - foreach ($this->extensions as $extension) { - if (! extension_loaded($extension)) { - $this->errors[] = [ + return collect($this->extensions) + ->reject(function ($extension) { + return extension_loaded($extension); + })->map(function ($extension) { + return [ 'message' => "The PHP extension '$extension' is required.", ]; - } - } + }); } } diff --git a/framework/core/src/Install/Prerequisite/PhpVersion.php b/framework/core/src/Install/Prerequisite/PhpVersion.php index b2960d372..59d21266f 100644 --- a/framework/core/src/Install/Prerequisite/PhpVersion.php +++ b/framework/core/src/Install/Prerequisite/PhpVersion.php @@ -11,7 +11,9 @@ namespace Flarum\Install\Prerequisite; -class PhpVersion extends AbstractPrerequisite +use Illuminate\Support\Collection; + +class PhpVersion implements PrerequisiteInterface { protected $minVersion; @@ -20,13 +22,15 @@ class PhpVersion extends AbstractPrerequisite $this->minVersion = $minVersion; } - public function check() + public function problems(): Collection { if (version_compare(PHP_VERSION, $this->minVersion, '<')) { - $this->errors[] = [ + return collect()->push([ 'message' => "PHP $this->minVersion is required.", 'detail' => 'You are running version '.PHP_VERSION.'. Talk to your hosting provider about upgrading to the latest PHP version.', - ]; + ]); } + + return collect(); } } diff --git a/framework/core/src/Install/Prerequisite/PrerequisiteInterface.php b/framework/core/src/Install/Prerequisite/PrerequisiteInterface.php index 115e9f808..9b7907883 100644 --- a/framework/core/src/Install/Prerequisite/PrerequisiteInterface.php +++ b/framework/core/src/Install/Prerequisite/PrerequisiteInterface.php @@ -11,9 +11,18 @@ namespace Flarum\Install\Prerequisite; +use Illuminate\Support\Collection; + interface PrerequisiteInterface { - public function check(); - - public function getErrors(); + /** + * Verify that this prerequisite is fulfilled. + * + * If everything is okay, this method should return an empty Collection + * instance. When problems are detected, it should return a Collection of + * arrays, each having at least a "message" and optionally a "detail" key. + * + * @return Collection + */ + public function problems(): Collection; } diff --git a/framework/core/src/Install/Prerequisite/WritablePaths.php b/framework/core/src/Install/Prerequisite/WritablePaths.php index 1f356ee6a..99beb2a3d 100644 --- a/framework/core/src/Install/Prerequisite/WritablePaths.php +++ b/framework/core/src/Install/Prerequisite/WritablePaths.php @@ -11,7 +11,9 @@ namespace Flarum\Install\Prerequisite; -class WritablePaths extends AbstractPrerequisite +use Illuminate\Support\Collection; + +class WritablePaths implements PrerequisiteInterface { protected $paths; @@ -20,21 +22,36 @@ class WritablePaths extends AbstractPrerequisite $this->paths = $paths; } - public function check() + public function problems(): Collection { - foreach ($this->paths as $path) { - if (! file_exists($path)) { - $this->errors[] = [ + return $this->getMissingPaths() + ->concat($this->getNonWritablePaths()); + } + + private function getMissingPaths(): Collection + { + return collect($this->paths) + ->reject(function ($path) { + return file_exists($path); + })->map(function ($path) { + return [ 'message' => 'The '.$this->getAbsolutePath($path).' directory doesn\'t exist', 'detail' => 'This directory is necessary for the installation. Please create the folder.', ]; - } elseif (! is_writable($path)) { - $this->errors[] = [ + }); + } + + private function getNonWritablePaths(): Collection + { + return collect($this->paths) + ->filter(function ($path) { + return file_exists($path) && ! is_writable($path); + })->map(function ($path) { + return [ 'message' => 'The '.$this->getAbsolutePath($path).' directory is not writable.', 'detail' => 'Please chmod this directory'.($path !== public_path() ? ' and its contents' : '').' to 0775.' ]; - } - } + }); } private function getAbsolutePath($path) diff --git a/framework/core/views/install/app.php b/framework/core/views/install/app.php index 2e8601022..3e4f572de 100644 --- a/framework/core/views/install/app.php +++ b/framework/core/views/install/app.php @@ -129,30 +129,30 @@ animation-name: fadeIn; } - .Errors { + .Problems { margin-top: 50px; } - .Errors .Error:first-child { + .Problems .Problem:first-child { border-top-left-radius: 4px; border-top-right-radius: 4px; } - .Errors .Error:last-child { + .Problems .Problem:last-child { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; } - .Error { + .Problem { background: #EDF2F7; margin: 0 0 1px; padding: 20px 25px; text-align: left; } - .Error-message { + .Problem-message { font-size: 16px; color: #3C5675; font-weight: normal; margin: 0; } - .Error-detail { + .Problem-detail { font-size: 13px; margin: 5px 0 0; } diff --git a/framework/core/views/install/errors.php b/framework/core/views/install/errors.php deleted file mode 100644 index bd73aeb43..000000000 --- a/framework/core/views/install/errors.php +++ /dev/null @@ -1,14 +0,0 @@ -

Hold Up!

- -

These errors must be resolved before you can continue the installation. If you're having trouble, get help on the Flarum website.

- -
- -
-

- -

- -
- -
diff --git a/framework/core/views/install/problems.php b/framework/core/views/install/problems.php new file mode 100644 index 000000000..36b8d6252 --- /dev/null +++ b/framework/core/views/install/problems.php @@ -0,0 +1,14 @@ +

Hold Up!

+ +

These problems must be resolved before you can continue the installation. If you're having trouble, get help on the Flarum website.

+ +
+ +
+

+ +

+ +
+ +