<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use Illuminate\Console\Scheduling\Schedule;
|
|
use LaravelZero\Framework\Commands\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
use App\Facades\Install;
|
|
|
|
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
|
|
use PhpSchool\CliMenu\CliMenu;
|
|
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
|
|
use PhpSchool\CliMenu\Style\SelectableStyle;
|
|
|
|
use Log;
|
|
|
|
class NginxVhostsCommand extends Command
|
|
{
|
|
// path templates
|
|
const TEMPLATES_DIR = '/resources/templates/nginx';
|
|
|
|
/**
|
|
* 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 add(CliMenuBuilder $builder)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
// create menu
|
|
$main = $this->menu(self::MENU_TITLE)
|
|
->addSubMenu('add', function(CliMenuBuilder $builder) {
|
|
|
|
// getting templates
|
|
$templates = scandir(base_path().self::TEMPLATES_DIR);
|
|
|
|
foreach($templates as $template) {
|
|
$builder
|
|
->addSubMenu($template, function(CliMenuBuilder $builder) use ($template) {
|
|
|
|
$configuration = [
|
|
'domain' => '',
|
|
'ssl' => true,
|
|
'redirect_www' => true
|
|
];
|
|
|
|
$checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
|
|
$configuration['ssl'] = $menu->getSelectedItem()->getChecked();
|
|
});
|
|
|
|
$checkboxSSL->setChecked($configuration['ssl']);
|
|
|
|
$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)
|
|
->addItem('domain', function(CliMenu $menu) use (&$configuration) {
|
|
$result = $menu->askText()->ask();
|
|
$configuration['domain'] = $result->fetch();
|
|
|
|
$menu->getSelectedItem()->setText('domain -> '.$result->fetch());
|
|
$menu->redraw();
|
|
})
|
|
->addLineBreak('-')
|
|
->addMenuItem($checkboxSSL)
|
|
->addMenuItem($checkboxRedirect)
|
|
->addLineBreak('-')
|
|
->addItem('save', function() use (&$configuration) {
|
|
var_dump($configuration);
|
|
})
|
|
->addLineBreak('-');
|
|
});
|
|
}
|
|
|
|
//exec('touch /etc/nginx/sites-available/');
|
|
})
|
|
->addLineBreak('-');
|
|
|
|
/***
|
|
|
|
$itemCallable = function(CliMenu $menu) {
|
|
|
|
// getting sites-available
|
|
$sitesAvailable = scandir('/etc/nginx/sites-available');
|
|
|
|
// getting sites-endabled
|
|
$sitesEnabled = scandir('/etc/nginx/sites-enabled');
|
|
|
|
foreach($sitesAvailable as $site) {
|
|
if ($site !== '.' && $site !== '..') {
|
|
|
|
// getting title
|
|
$title = str_replace('.conf', '', $site);
|
|
|
|
$checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($site, $title) {
|
|
|
|
// check status
|
|
if ($menu->getSelectedItem()->getChecked()) {
|
|
symlink('/etc/nginx/sites-available/'.$site, '/etc/nginx/sites-enabled/'.$site);
|
|
$status = 'enabled';
|
|
} else {
|
|
unlink('/etc/nginx/sites-enabled/'.$site);
|
|
$status = 'disabled';
|
|
}
|
|
|
|
// starting
|
|
exec('service nginx restart');
|
|
exec('service nginx status', $output);
|
|
|
|
if (strpos(implode(' ', $output), 'active') !== false) {
|
|
$menu->confirm($title.' is '.$status.'!')->display('OK!');
|
|
} else {
|
|
$menu->confirm('Error! Something not working!')->display('OK!');
|
|
}
|
|
|
|
});
|
|
|
|
// adding checkbox
|
|
if (in_array($site, $sitesEnabled)) {
|
|
$checkbox->setChecked(true);
|
|
}
|
|
|
|
$menuMain
|
|
->addSubmenu($title, function(CliMenuBuilder $builder) use ($site, $title, $checkbox) {
|
|
$builder->setTitle("Nginx > $title")
|
|
->addItem('edit', function(CliMenu $menu) use ($site, $title) {
|
|
system('nano /etc/nginx/sites-available/'.$site.' > `tty`');
|
|
})
|
|
->addItem('delete', function(CliMenu $menu) use ($site) {
|
|
if (file_exists('/etc/nginx/sites-enabled/'.$site)) {
|
|
$menu->confirm('Error! Please disable '.$title.' first!')->display('OK!');
|
|
} else {
|
|
unlink('/etc/nginx/sites-available/'.$site);
|
|
$menu->confirm("$site is deleted!")->display('OK!');
|
|
}
|
|
})
|
|
->addLineBreak('-')
|
|
->addMenuItem($checkbox)
|
|
->addLineBreak('-');
|
|
});
|
|
|
|
}
|
|
}
|
|
};*/
|
|
|
|
$main->addLineBreak('-')->open();
|
|
}
|
|
}
|