<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
use LaravelZero\Framework\Commands\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
use Jenssegers\Blade\Blade;
|
|
|
|
use App\Facades\Install;
|
|
use App\Facades\NginxVhost;
|
|
|
|
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
|
|
use PhpSchool\CliMenu\CliMenu;
|
|
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
|
use PhpSchool\CliMenu\Style\SelectableStyle;
|
|
|
|
use Log;
|
|
use Closure;
|
|
|
|
class NginxVhostsCommand extends Command
|
|
{
|
|
// path templates
|
|
const TEMPLATES_DIR = '/resources/nginx/templates';
|
|
|
|
/**
|
|
* The signature of the command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'nginx:vhosts';
|
|
|
|
/**
|
|
* The description of the command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Create and Manage Nginx Vhosts';
|
|
|
|
/**
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
private function getTemplates($prefix = '')
|
|
{
|
|
// templates
|
|
$templates = [];
|
|
|
|
// getting templates
|
|
$files = scandir(base_path().self::TEMPLATES_DIR);
|
|
|
|
foreach($files as $file) {
|
|
|
|
// create filepath
|
|
$filepath = base_path().self::TEMPLATES_DIR.'/'.$file;
|
|
|
|
// getting info
|
|
$pathinfo = pathinfo($filepath);
|
|
|
|
// if extension isset and php
|
|
if (isset($pathinfo['extension']) && $pathinfo['extension'] === 'php') {
|
|
|
|
$name = str_replace('.blade.php', '', $file);
|
|
|
|
$templates[] = [
|
|
'name' => $name,
|
|
'file' => $file,
|
|
'filepath' => $filepath
|
|
];
|
|
}
|
|
}
|
|
|
|
return $templates;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
private function selectTemplate()
|
|
{
|
|
// getting templates
|
|
$templates = $this->getTemplates();
|
|
|
|
$menu = function(CliMenuBuilder $builder) use ($templates)
|
|
{
|
|
// writing configuration
|
|
$blade = new Blade(base_path().self::TEMPLATES_DIR, base_path().'/storage/cache');
|
|
|
|
$builder
|
|
->setTitle('Nginx > add')
|
|
->setGoBackButtonText('Back');
|
|
|
|
foreach($templates as $template) {
|
|
$submenuCallable = $this->createConfiguration($template, $blade);
|
|
$builder
|
|
->addSubMenu($template['name'], $submenuCallable);
|
|
}
|
|
|
|
$builder->addLineBreak('-');
|
|
};
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @param [type] $template [description]
|
|
* @param [type] $blade [description]
|
|
* @return [type] [description]
|
|
*/
|
|
private function createConfiguration($template, $blade)
|
|
{
|
|
$menu = function(CliMenuBuilder $builder) use ($template, $blade)
|
|
{
|
|
$configuration = [
|
|
'domain' => '',
|
|
'root' => '',
|
|
'index' => 'index.php',
|
|
'ssl' => true,
|
|
'redirect_www' => true
|
|
];
|
|
|
|
// create checkbox for ssl
|
|
$checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
|
|
$configuration['ssl'] = $menu->getSelectedItem()->getChecked();
|
|
});
|
|
|
|
$checkboxSSL->setChecked($configuration['ssl']);
|
|
|
|
// create checkbox for redirect from www
|
|
$checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
|
|
$configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
|
|
});
|
|
|
|
$checkboxRedirect->setChecked($configuration['redirect_www']);
|
|
|
|
$builder
|
|
->setTitle('Nginx Vhosts > add > '.$template['name'])
|
|
->addItem('domain', function(CliMenu $menu) use (&$configuration) {
|
|
$result = $menu->askText()->ask();
|
|
$configuration['domain'] = $result->fetch();
|
|
|
|
$menu->getSelectedItem()->setText('domain -> '.$result->fetch());
|
|
$menu->redraw();
|
|
})
|
|
->addItem('root', function(CliMenu $menu) use (&$configuration) {
|
|
$result = $menu->askText()->ask();
|
|
$configuration['root'] = $result->fetch();
|
|
|
|
$menu->getSelectedItem()->setText('root -> '.$result->fetch());
|
|
$menu->redraw();
|
|
})
|
|
->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) {
|
|
$result = $menu->askText()->ask();
|
|
$configuration['index'] = $result->fetch();
|
|
|
|
$menu->getSelectedItem()->setText($result->fetch());
|
|
$menu->redraw();
|
|
})
|
|
->addLineBreak('-')
|
|
->addMenuItem($checkboxSSL)
|
|
->addMenuItem($checkboxRedirect)
|
|
->addLineBreak('-')
|
|
->addItem('save', function() use (&$configuration, $template, $blade) {
|
|
$content = $blade->render($template['name'], $configuration);
|
|
file_put_contents('/etc/nginx/sites-available/'.$configuration['domain'].'.conf', $content);
|
|
})
|
|
->addLineBreak('-');
|
|
};
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
* create submenu for vhost
|
|
*
|
|
*
|
|
* @param array $vhost
|
|
* @return CliMenuBuilder
|
|
*
|
|
*/
|
|
private function vhost($vhost)
|
|
{
|
|
$menu = function(CliMenuBuilder $builder) use ($vhost)
|
|
{
|
|
$checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
|
|
|
|
// check status
|
|
if ($menu->getSelectedItem()->getChecked()) {
|
|
symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
|
|
$status = 'enabled';
|
|
} else {
|
|
unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
|
|
$status = 'disabled';
|
|
}
|
|
|
|
// restart
|
|
exec('service nginx restart');
|
|
exec('service nginx status', $output);
|
|
|
|
if (strpos(implode(' ', $output), 'active') !== false) {
|
|
$menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
|
|
} else {
|
|
$menu->confirm('Error! Something not working!')->display('OK!');
|
|
}
|
|
|
|
});
|
|
|
|
// adding checkbox
|
|
if ($vhost['enabled']) {
|
|
$checkbox->setChecked(true);
|
|
}
|
|
|
|
$builder
|
|
->setTitle('Nginx > '.$vhost['domain'])
|
|
->setGoBackButtonText('Back')
|
|
->addItem('edit', function(CliMenu $menu) use ($vhost) {
|
|
system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
|
|
})
|
|
->addItem('delete', function(CliMenu $menu) use ($vhost) {
|
|
if (file_exists('/etc/nginx/sites-enabled/'.$vhost['file'])) {
|
|
$menu->confirm('Error! Please disable '.$vhost['domain'].' first!')->display('OK!');
|
|
} else {
|
|
unlink('/etc/nginx/sites-available/'.$vhost['file']);
|
|
$menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
|
|
}
|
|
})
|
|
->addLineBreak('-')
|
|
->addMenuItem($checkbox)
|
|
->addLineBreak('-');
|
|
};
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
* let it rain
|
|
*
|
|
*
|
|
*/
|
|
public function handle()
|
|
{
|
|
$submenuSelectTemplate = $this->selectTemplate();
|
|
|
|
// create menu
|
|
$main = $this->menu('Nginx')
|
|
|
|
// add Submenu for select templates
|
|
->addSubMenu('add', $submenuSelectTemplate)
|
|
->addLineBreak('-');
|
|
|
|
// getting vhosts
|
|
$vhosts = NginxVhost::find();
|
|
|
|
// add submenu for each vhost
|
|
foreach($vhosts as $vhost) {
|
|
$submenuEditVhost = $this->vhost($vhost);
|
|
$main->addSubMenu($vhost['domain'], $submenuEditVhost);
|
|
}
|
|
|
|
$main->addLineBreak('-');
|
|
$main->open();
|
|
}
|
|
}
|