FEATURE #2503: add easier installer
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user