mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
Extract installation prerequisites into composable classes and use those in the web-based installer
This commit is contained in:
parent
e6c96394b7
commit
f8701f1123
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
namespace Flarum\Install\Actions;
|
namespace Flarum\Install\Actions;
|
||||||
|
|
||||||
|
use Flarum\Install\Prerequisites\Prerequisite;
|
||||||
use Flarum\Support\HtmlAction;
|
use Flarum\Support\HtmlAction;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Illuminate\Contracts\View\Factory;
|
use Illuminate\Contracts\View\Factory;
|
||||||
|
@ -22,11 +23,18 @@ class IndexAction extends HtmlAction
|
||||||
protected $view;
|
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->view = $view;
|
||||||
|
$this->prerequisite = $prerequisite;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,38 +46,8 @@ class IndexAction extends HtmlAction
|
||||||
{
|
{
|
||||||
$view = $this->view->make('flarum.install::app');
|
$view = $this->view->make('flarum.install::app');
|
||||||
|
|
||||||
$errors = [];
|
$this->prerequisite->check();
|
||||||
|
$errors = $this->prerequisite->getErrors();
|
||||||
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.'
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($errors)) {
|
if (count($errors)) {
|
||||||
$view->content = $this->view->make('flarum.install::errors');
|
$view->content = $this->view->make('flarum.install::errors');
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
namespace Flarum\Install;
|
namespace Flarum\Install;
|
||||||
|
|
||||||
use Flarum\Http\RouteCollection;
|
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 Flarum\Support\ServiceProvider;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
@ -24,6 +28,17 @@ class InstallServiceProvider extends ServiceProvider
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->app->register('Flarum\Locale\LocaleServiceProvider');
|
$this->app->register('Flarum\Locale\LocaleServiceProvider');
|
||||||
|
|
||||||
|
$this->app->bind(
|
||||||
|
'Flarum\Install\Prerequisites\Prerequisite',
|
||||||
|
function() {
|
||||||
|
return new Composite(
|
||||||
|
new PhpVersion(),
|
||||||
|
new PhpExtensions(),
|
||||||
|
new WritablePaths()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
45
framework/core/src/Install/Prerequisites/Composite.php
Normal file
45
framework/core/src/Install/Prerequisites/Composite.php
Normal 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', []);
|
||||||
|
}
|
||||||
|
}
|
25
framework/core/src/Install/Prerequisites/PhpExtensions.php
Normal file
25
framework/core/src/Install/Prerequisites/PhpExtensions.php
Normal 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.",
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
framework/core/src/Install/Prerequisites/PhpVersion.php
Normal file
24
framework/core/src/Install/Prerequisites/PhpVersion.php
Normal 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.'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
framework/core/src/Install/Prerequisites/Prerequisite.php
Normal file
18
framework/core/src/Install/Prerequisites/Prerequisite.php
Normal 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();
|
||||||
|
}
|
33
framework/core/src/Install/Prerequisites/WritablePaths.php
Normal file
33
framework/core/src/Install/Prerequisites/WritablePaths.php
Normal 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.'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user