Files
core/app/Console/Commands/InstallCMS.php
T

154 lines
4.1 KiB
PHP

<?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()
{
// check and eventualy create .env file
if (!file_exists(base_path('.env'))) {
$this->createEnv();
$this->line('Rerun this script again, we need to load changes.');
return;
}
// 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');
// generate APP_KEY in .env
$this->call('key:generate');
// create user
$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.');
}
/**
* Insert value into .env config file
*
* @param $envConstant
* @param $value
*/
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);
}
/**
* Create .env file with all needed constants
*/
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);
}
// set app url
do {
$url = $this->ask('Application URL');
$url = trim($url, ' /');
} while (empty($url));
$this->setEnvConstant('SITE_URL', $url);
$this->setEnvConstant('UPLOAD_URL', $url . '/public/uploads');
$this->setEnvConstant('THUMBNAIL_URL', $url . '/public/uploads/thumbnails');
}
/**
* Recursively change chmod to 777 in given directory
*
* @param $directory
*/
private function changeChmod($directory)
{
@chmod($directory, 0777);
if (!is_dir($directory)) {
return;
}
$subdirectories = \File::allFiles($directory);
foreach ($subdirectories as $subdirectory) {
$this->changeChmod($subdirectory);
}
}
}