diff --git a/src/Install/Actions/IndexAction.php b/src/Install/Actions/IndexAction.php index 812caab54..3b39a2944 100644 --- a/src/Install/Actions/IndexAction.php +++ b/src/Install/Actions/IndexAction.php @@ -10,6 +10,7 @@ namespace Flarum\Install\Actions; +use Flarum\Install\Prerequisites\Prerequisite; use Flarum\Support\HtmlAction; use Psr\Http\Message\ServerRequestInterface as Request; use Illuminate\Contracts\View\Factory; @@ -22,11 +23,18 @@ class IndexAction extends HtmlAction protected $view; /** - * @param Factory $view + * @var Prerequisite */ - public function __construct(Factory $view) + protected $prerequisite; + + /** + * @param Factory $view + * @param Prerequisite $prerequisite + */ + public function __construct(Factory $view, Prerequisite $prerequisite) { $this->view = $view; + $this->prerequisite = $prerequisite; } /** @@ -38,38 +46,8 @@ class IndexAction extends HtmlAction { $view = $this->view->make('flarum.install::app'); - $errors = []; - - if (version_compare(PHP_VERSION, '5.5.0', '<')) { - $errors[] = [ - 'message' => 'PHP 5.5+ is required.', - 'detail' => 'You are running version '.PHP_VERSION.'. Talk to your hosting provider about upgrading to the latest PHP version.' - ]; - } - - foreach (['mbstring', 'pdo_mysql', 'openssl', 'json', 'gd', 'dom', 'fileinfo'] as $extension) { - if (! extension_loaded($extension)) { - $errors[] = [ - 'message' => 'The '.$extension.' extension is required.' - ]; - } - } - - $paths = [ - public_path(), - public_path().'/assets', - public_path().'/extensions', - storage_path() - ]; - - foreach ($paths as $path) { - if (! is_writable($path)) { - $errors[] = [ - 'message' => 'The '.realpath($path).' directory is not writable.', - 'detail' => 'Please chmod this directory '.($path !== public_path() ? ' and its contents' : '').' to 0775.' - ]; - } - } + $this->prerequisite->check(); + $errors = $this->prerequisite->getErrors(); if (count($errors)) { $view->content = $this->view->make('flarum.install::errors'); diff --git a/src/Install/InstallServiceProvider.php b/src/Install/InstallServiceProvider.php index 95fa4c2ff..7607b873f 100644 --- a/src/Install/InstallServiceProvider.php +++ b/src/Install/InstallServiceProvider.php @@ -11,6 +11,10 @@ namespace Flarum\Install; use Flarum\Http\RouteCollection; +use Flarum\Install\Prerequisites\PhpExtensions; +use Flarum\Install\Prerequisites\PhpVersion; +use Flarum\Install\Prerequisites\WritablePaths; +use Flarum\Install\Prerequisites\Composite; use Flarum\Support\ServiceProvider; use Psr\Http\Message\ServerRequestInterface; @@ -24,6 +28,17 @@ class InstallServiceProvider extends ServiceProvider public function register() { $this->app->register('Flarum\Locale\LocaleServiceProvider'); + + $this->app->bind( + 'Flarum\Install\Prerequisites\Prerequisite', + function() { + return new Composite( + new PhpVersion(), + new PhpExtensions(), + new WritablePaths() + ); + } + ); } /** diff --git a/src/Install/Prerequisites/AbstractPrerequisite.php b/src/Install/Prerequisites/AbstractPrerequisite.php new file mode 100644 index 000000000..aec6a622c --- /dev/null +++ b/src/Install/Prerequisites/AbstractPrerequisite.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +abstract class AbstractPrerequisite implements Prerequisite +{ + protected $errors = []; + + abstract public function check(); + + public function getErrors() + { + return $this->errors; + } +} diff --git a/src/Install/Prerequisites/Composite.php b/src/Install/Prerequisites/Composite.php new file mode 100644 index 000000000..8551d32af --- /dev/null +++ b/src/Install/Prerequisites/Composite.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +class Composite implements Prerequisite +{ + /** + * @var AbstractPrerequisite[] + */ + protected $prerequisites = []; + + public function __construct(Prerequisite $first) + { + foreach (func_get_args() as $prerequisite) + { + $this->prerequisites[] = $prerequisite; + } + } + + public function check() + { + return array_reduce( + $this->prerequisites, + function ($previous, Prerequisite $prerequisite) { + return $prerequisite->check() && $previous; + }, + true + ); + } + + public function getErrors() + { + return collect($this->prerequisites)->map(function(Prerequisite $prerequisite) { + return $prerequisite->getErrors(); + })->reduce('array_merge', []); + } +} diff --git a/src/Install/Prerequisites/PhpExtensions.php b/src/Install/Prerequisites/PhpExtensions.php new file mode 100644 index 000000000..f4ff6b29c --- /dev/null +++ b/src/Install/Prerequisites/PhpExtensions.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +class PhpExtensions extends AbstractPrerequisite +{ + public function check() + { + foreach (['mbstring', 'pdo_mysql', 'openssl', 'json', 'gd', 'dom', 'fileinfo'] as $extension) { + if (! extension_loaded($extension)) { + $this->errors[] = [ + 'message' => "The $extension extension is required.", + ]; + } + } + } +} diff --git a/src/Install/Prerequisites/PhpVersion.php b/src/Install/Prerequisites/PhpVersion.php new file mode 100644 index 000000000..4d6c2af3e --- /dev/null +++ b/src/Install/Prerequisites/PhpVersion.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +class PhpVersion extends AbstractPrerequisite +{ + public function check() + { + if (version_compare(PHP_VERSION, '5.5.0', '<')) { + $this->errors[] = [ + 'message' => 'PHP 5.5+ is required.', + 'detail' => 'You are running version '.PHP_VERSION.'. Talk to your hosting provider about upgrading to the latest PHP version.' + ]; + } + } +} diff --git a/src/Install/Prerequisites/Prerequisite.php b/src/Install/Prerequisites/Prerequisite.php new file mode 100644 index 000000000..4eef6c794 --- /dev/null +++ b/src/Install/Prerequisites/Prerequisite.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +interface Prerequisite +{ + public function check(); + + public function getErrors(); +} diff --git a/src/Install/Prerequisites/WritablePaths.php b/src/Install/Prerequisites/WritablePaths.php new file mode 100644 index 000000000..6fcb37213 --- /dev/null +++ b/src/Install/Prerequisites/WritablePaths.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Install\Prerequisites; + +class WritablePaths extends AbstractPrerequisite +{ + public function check() + { + $paths = [ + public_path(), + public_path().'/assets', + public_path().'/extensions', + storage_path() + ]; + + foreach ($paths as $path) { + if (! is_writable($path)) { + $this->errors[] = [ + 'message' => 'The '.realpath($path).' directory is not writable.', + 'detail' => 'Please chmod this directory '.($path !== public_path() ? ' and its contents' : '').' to 0775.' + ]; + } + } + } +}