Extract installation prerequisites into composable classes and use those in the web-based installer

This commit is contained in:
Franz Liedke 2015-09-03 08:23:34 +02:00
parent bd47653377
commit 942db77416
8 changed files with 195 additions and 34 deletions

View File

@ -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' => '<strong>PHP 5.5+</strong> 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 <strong>'.$extension.'</strong> 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 <strong>'.realpath($path).'</strong> 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');

View File

@ -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()
);
}
);
}
/**

View File

@ -0,0 +1,23 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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;
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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', []);
}
}

View File

@ -0,0 +1,25 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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 <strong>$extension</strong> extension is required.",
];
}
}
}
}

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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' => '<strong>PHP 5.5+</strong> is required.',
'detail' => 'You are running version '.PHP_VERSION.'. Talk to your hosting provider about upgrading to the latest PHP version.'
];
}
}
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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();
}

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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 <strong>'.realpath($path).'</strong> directory is not writable.',
'detail' => 'Please chmod this directory '.($path !== public_path() ? ' and its contents' : '').' to 0775.'
];
}
}
}
}