FEATURE #2503: add easier installer

This commit is contained in:
Michal Kváček
2017-08-24 14:15:08 +02:00
parent 8946ef7441
commit ce183d4805
9 changed files with 9921 additions and 6040 deletions
@@ -3,6 +3,7 @@
namespace App\CMS\ModuleManager\Providers;
use App\CMS\Repositories\Module as ModuleRepository;
use Illuminate\Database\QueryException;
use Illuminate\Support\ServiceProvider;
class ModuleManager extends ServiceProvider
@@ -14,7 +15,11 @@ class ModuleManager extends ServiceProvider
public function boot()
{
// this service provider needs published config - config is merged right after this service provider
$modules = app(ModuleRepository::class)->getOnlyModules();
try {
$modules = app(ModuleRepository::class)->getOnlyModules();
} catch (QueryException $e) {
return;
}
// iterate over all modules and enable or disable them
foreach ($modules as $module) {
+9 -6
View File
@@ -13,14 +13,14 @@ class InitDatabaseSchema extends Command
*
* @var string
*/
protected $signature = 'db:init {schema.sql}';
protected $signature = 'db:init';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
protected $description = 'Init database structure';
/**
* Create a new command instance.
@@ -39,13 +39,16 @@ class InitDatabaseSchema extends Command
*/
public function handle()
{
do {
$schema = $this->ask('Path to database schema file', 'database/schema.sql');
} while (!file_exists($schema));
$schema = $this->argument('schema.sql');
if (!file_exists($schema)) throw new NotFoundHttpException('File '.$schema.' does not exist.');
DB::unprepared(file_get_contents($schema));
$this->line('Successfully imported');
$this->call('migrate');
$this->call('db:seed');
$this->line('Database successfully initialized.');
}
}
+124
View File
@@ -0,0 +1,124 @@
<?php
namespace App\Console\Commands;
use App\Models\Language;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
class InstallCMS extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'cms:install';
/**
* The console command description.
* @var string
*/
protected $description = 'Try to make installation of CMS as easy as possible';
/**
* Execute the console command.
* @return mixed
*/
public function handle()
{
if (!file_exists(base_path('.env'))) {
$this->createEnv();
}
// install php dependencies
$parameters = env('APP_DEBUG') ? '' : '--no-dev';
exec('composer install ' . $parameters);
// init database
try {
// does exist something in laguages table? (Or event table itself?)
Language::all();
} catch (QueryException $e) {
// database is not initialized
$this->call('db:init');
}
// install npm dependencies
exec('npm install');
exec('bower install');
exec('gulp');
// change chmod of needed directories
$directories = ['bootstrap/cache', 'storage', 'public'];
foreach ($directories as $directory) {
$this->changeChmod($directory);
}
$this->line('Chmod changed');
$this->call('key:generate');
$this->line('Creating admin user...');
do {
$email = $this->ask('E-mail for admin user');
} while (empty($email));
$this->call('user:new', compact('email'));
$this->line('All done. CMS should be prepared.');
}
private function setEnvConstant($envConstant, $value)
{
$content = file_get_contents(base_path('.env'));
$content = preg_replace('/' . $envConstant . '=(.*)/i', $envConstant . '=' . $value, $content);
file_put_contents(base_path('.env'), $content);
}
private function createEnv()
{
\File::copy(base_path('.env.example'), base_path('.env'));
// set environment
do {
$env = $this->ask('Install environment: ', 'local');
} while (empty($env));
$this->setEnvConstant('APP_ENV', $env);
// set debugging
if ($env == 'local') {
$debug = 'true';
} else {
$debug = $this->confirm('Enable debugging?', true) ? 'true' : 'false';
}
$this->setEnvConstant('APP_DEBUG', $debug);
// setup database
$databaseFields = [
'DB_HOST' => 'Database server',
'DB_DATABASE' => 'Database name',
'DB_USERNAME' => 'Database user',
'DB_PASSWORD' => 'Password'
];
foreach ($databaseFields as $constant => $text) {
do {
$value = $this->ask($text);
} while (empty ($value));
$this->setEnvConstant($constant, $value);
}
}
private function changeChmod($directory)
{
@chmod($directory, 0777);
if (!is_dir($directory)) {
return;
}
$subdirectories = \File::allFiles($directory);
foreach ($subdirectories as $subdirectory) {
$this->changeChmod($subdirectory);
}
}
}
+3 -9
View File
@@ -12,13 +12,11 @@ class NewUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:new {email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates new user';
@@ -28,13 +26,9 @@ class NewUser extends Command
*/
public function handle()
{
$email = $this->argument('email');
// check if username is really email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->error($email . ' is not a valid e-mail!');
return;
}
do {
$email = $this->argument('email');
} while (!filter_var($email, FILTER_VALIDATE_EMAIL));
// ask for password
do {
+3 -1
View File
@@ -2,6 +2,7 @@
namespace App\Console;
use App\Console\Commands\InstallCMS;
use App\Console\Commands\Modules\CreateNew;
use App\Console\Commands\Modules\DisableModule;
use App\Console\Commands\Modules\EnableModule;
@@ -32,7 +33,8 @@ class Kernel extends ConsoleKernel
Commands\SetTemplate::class,
Commands\AddLanguage::class,
PublishTemplateResources::class,
TemplateInfo::class
TemplateInfo::class,
InstallCMS::class
];
/**
@@ -1,5 +1,6 @@
<?php namespace App\Providers;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
@@ -8,8 +9,19 @@ class LaravelLocalizationServiceProvider extends ServiceProvider
{
public function register()
{
$languages = DB::table('languages')
->get();
try {
$languages = DB::table('languages')
->get();
} catch (QueryException $e) {
$supportedLocales[config('app.locale')] = [
'native' => env('LOCALE_NATIVE_NAME', config('app.locale')),
'script' => env('LOCALE_SCRIPT', 'Latn'),
'name' => env('LOCALE_TRANSLATED_NAME', config('app.locale')),
'code' => config('app.locale'),
];
$this->setSupportedLocales($supportedLocales);
return;
}
$supportedLocales = [];
if (empty($languages)) {
@@ -32,15 +44,20 @@ class LaravelLocalizationServiceProvider extends ServiceProvider
];
}
config([
'laravellocalization.supportedLocales' => $supportedLocales,
'laravellocalization.useAcceptLanguageHeader' => true,
'laravellocalization.hideDefaultLocaleInURL' => true
]);
$this->setSupportedLocales($supportedLocales);
}
public function boot()
{
\App::setLocale(LaravelLocalization::getCurrentLocale());
}
private function setSupportedLocales($supportedLocales)
{
config([
'laravellocalization.supportedLocales' => $supportedLocales,
'laravellocalization.useAcceptLanguageHeader' => true,
'laravellocalization.hideDefaultLocaleInURL' => true
]);
}
}
+6 -1
View File
@@ -6,6 +6,7 @@ use App\CMS\Contracts\Repositories\Template as TemplateContract;
use App\CMS\Handlers\Registrators\TemplateRegistrator;
use App\CMS\Helpers\CurrentTemplate;
use App\CMS\Repositories\Module;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
@@ -13,7 +14,11 @@ class TemplateServiceProvider extends ServiceProvider
{
public function boot()
{
$template = CurrentTemplate::get();
try {
$template = CurrentTemplate::get();
} catch (QueryException $e) {
return;
}
// ignore running from artisan
if (App::runningInConsole() || App::environment('testing') || !$template) {
+9746 -6015
View File
File diff suppressed because it is too large Load Diff
Regular → Executable
View File