This command did not set theme properly, when THEME was in .env file, but with no value (THEME= only - as default in .env.example).
71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class SetTheme extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
* @var string
|
|
*/
|
|
protected $signature = 'theme:set {theme_name}';
|
|
/**
|
|
* The console command description.
|
|
* @var string
|
|
*/
|
|
protected $description = 'Set theme for visitor area';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$theme = $this->argument('theme_name');
|
|
|
|
if (!is_dir(resource_path('views/visitor/' . $theme))) {
|
|
throw new NotFoundHttpException('Invalid theme ' . $theme . '!');
|
|
}
|
|
|
|
// load .env file
|
|
$envFile = base_path('.env');
|
|
$env = file_get_contents($envFile);
|
|
|
|
if (!Str::contains($env, 'THEME=')) {
|
|
$env = sprintf("%s\nTHEME=%s\n", $env, $theme);
|
|
} else {
|
|
$env = preg_replace('/THEME=(.*)/i', 'THEME=' . $theme, $env, -1, $cnt);
|
|
}
|
|
|
|
// try to write content
|
|
$bytesWritten = file_put_contents($envFile, $env);
|
|
|
|
// check if write was successfull
|
|
if (!$bytesWritten) {
|
|
throw new \Exception('Cannot write to ' . base_path('.env') . '. Add THEME=' . $theme . ' to your .env file manualy.');
|
|
}
|
|
|
|
// theme written
|
|
$this->line('Theme successfully setup.');
|
|
$this->line('');
|
|
$this->line(' ** PUBLISHING ASSETS AND VIEWS **');
|
|
// publish assets and views for module and theme
|
|
$this->call('vendor:publish', ['--tag' => ['public-assets'], '--force' => true]);
|
|
$this->call('vendor:publish', ['--tag' => ['module-views'], '--force' => true]);
|
|
$this->call('vendor:publish');
|
|
}
|
|
}
|