|
|
@ -10,10 +10,13 @@ use Jenssegers\Blade\Blade; |
|
|
|
use App\Facades\Install; |
|
|
|
use App\Facades\NginxVhost; |
|
|
|
|
|
|
|
use App\Factories\NginxVhostFactory; |
|
|
|
|
|
|
|
use PhpSchool\CliMenu\Builder\CliMenuBuilder; |
|
|
|
use PhpSchool\CliMenu\CliMenu; |
|
|
|
use PhpSchool\CliMenu\MenuItem\CheckboxItem; |
|
|
|
use PhpSchool\CliMenu\Style\SelectableStyle; |
|
|
|
use PhpSchool\CliMenu\MenuItem\MenuMenuItem; |
|
|
|
|
|
|
|
use Log; |
|
|
|
use Closure; |
|
|
@ -29,6 +32,9 @@ class NginxVhostsCommand extends Command |
|
|
|
// path templates
|
|
|
|
const TEMPLATES_DIR = '/resources/nginx/templates'; |
|
|
|
|
|
|
|
// index for vhosts
|
|
|
|
const VHOST_INDEX = 2; |
|
|
|
|
|
|
|
/** |
|
|
|
* The signature of the command. |
|
|
|
* |
|
|
@ -175,113 +181,28 @@ class NginxVhostsCommand extends Command |
|
|
|
->addMenuItem($checkboxRedirect) |
|
|
|
->addLineBreak('-') |
|
|
|
->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) { |
|
|
|
$content = $blade->render($template['name'], $configuration); |
|
|
|
file_put_contents('/etc/nginx/sites-available/'.$configuration['domain'].'.conf', $content); |
|
|
|
|
|
|
|
$parent = $menu->getParent(); |
|
|
|
$menu->closeThis(); |
|
|
|
|
|
|
|
$mainmenu = $parent->getParent(); |
|
|
|
$mainmenu->open(); |
|
|
|
}) |
|
|
|
->addLineBreak('-'); |
|
|
|
}; |
|
|
|
|
|
|
|
return $menu; |
|
|
|
} |
|
|
|
// render configuration
|
|
|
|
$content = $blade->render($template['name'], $configuration); |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
// create filename
|
|
|
|
$filename = $configuration['domain'].'.conf'; |
|
|
|
|
|
|
|
// write configuration to file
|
|
|
|
file_put_contents('/etc/nginx/sites-available/'.$filename, $content); |
|
|
|
|
|
|
|
/** |
|
|
|
* create submenu for vhost |
|
|
|
* |
|
|
|
* |
|
|
|
* @param array $vhost |
|
|
|
* @return CliMenuBuilder |
|
|
|
* |
|
|
|
*/ |
|
|
|
private function vhost($vhost) |
|
|
|
{ |
|
|
|
$currentMenu = 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!'); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
// prepare for close current submenu and open mainmenu
|
|
|
|
$parent = $menu->getParent(); |
|
|
|
$menu->closeThis(); |
|
|
|
|
|
|
|
// adding checkbox
|
|
|
|
if ($vhost['enabled']) { |
|
|
|
$checkbox->setChecked(true); |
|
|
|
} |
|
|
|
$mainmenu = $parent->getParent(); |
|
|
|
|
|
|
|
$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!'); |
|
|
|
|
|
|
|
// get
|
|
|
|
$parent = $menu->getParent(); |
|
|
|
$menu->closeThis(); |
|
|
|
|
|
|
|
// remove current vhost from mainmenu
|
|
|
|
$parent->removeItem($parent->getSelectedItem()); |
|
|
|
$parent->open(); |
|
|
|
} |
|
|
|
$mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX); |
|
|
|
$mainmenu->open(); |
|
|
|
}) |
|
|
|
->addLineBreak('-') |
|
|
|
->addMenuItem($checkbox) |
|
|
|
->addLineBreak('-'); |
|
|
|
}; |
|
|
|
|
|
|
|
return $currentMenu; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
protected function updateVhosts($menu) |
|
|
|
{ |
|
|
|
// getting vhosts
|
|
|
|
$vhosts = NginxVhost::find(); |
|
|
|
|
|
|
|
// add submenu for each vhost
|
|
|
|
foreach($vhosts as $vhost) { |
|
|
|
$submenuEditVhost = $this->vhost($vhost); |
|
|
|
$menu->addSubMenu($vhost['domain'], $submenuEditVhost); |
|
|
|
} |
|
|
|
|
|
|
|
return $menu; |
|
|
|
} |
|
|
|
|
|
|
@ -295,15 +216,23 @@ class NginxVhostsCommand extends Command |
|
|
|
$submenuSelectTemplate = $this->selectTemplate(); |
|
|
|
|
|
|
|
// create menu
|
|
|
|
$main = $this->menu('Nginx') |
|
|
|
$mainmenu = $this->menu('Nginx') |
|
|
|
|
|
|
|
// add Submenu for select templates
|
|
|
|
->addSubMenu('add', $submenuSelectTemplate) |
|
|
|
->addLineBreak('-'); |
|
|
|
|
|
|
|
$main = $this->updateVhosts($main); |
|
|
|
$mainmenu->addLineBreak('-'); |
|
|
|
|
|
|
|
// apperance
|
|
|
|
$mainmenu->setWidth($mainmenu->getTerminal()->getWidth()); |
|
|
|
$mainmenu->setMargin(2); |
|
|
|
|
|
|
|
$mainmenu = $mainmenu->build(); |
|
|
|
|
|
|
|
// adding current vhosts
|
|
|
|
$mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX); |
|
|
|
|
|
|
|
$main->addLineBreak('-'); |
|
|
|
$main->open(); |
|
|
|
$mainmenu->open(); |
|
|
|
} |
|
|
|
} |