Browse Source

adding #13

release/0.1
Björn 4 years ago
parent
commit
49d36a52d5
2 changed files with 180 additions and 101 deletions
  1. +30
    -101
      app/Commands/NginxVhostsCommand.php
  2. +150
    -0
      app/Factories/NginxVhostFactory.php

+ 30
- 101
app/Commands/NginxVhostsCommand.php View File

@ -10,10 +10,13 @@ use Jenssegers\Blade\Blade;
use App\Facades\Install; use App\Facades\Install;
use App\Facades\NginxVhost; use App\Facades\NginxVhost;
use App\Factories\NginxVhostFactory;
use PhpSchool\CliMenu\Builder\CliMenuBuilder; use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu; use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckboxItem; use PhpSchool\CliMenu\MenuItem\CheckboxItem;
use PhpSchool\CliMenu\Style\SelectableStyle; use PhpSchool\CliMenu\Style\SelectableStyle;
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
use Log; use Log;
use Closure; use Closure;
@ -29,6 +32,9 @@ class NginxVhostsCommand extends Command
// path templates // path templates
const TEMPLATES_DIR = '/resources/nginx/templates'; const TEMPLATES_DIR = '/resources/nginx/templates';
// index for vhosts
const VHOST_INDEX = 2;
/** /**
* The signature of the command. * The signature of the command.
* *
@ -175,113 +181,28 @@ class NginxVhostsCommand extends Command
->addMenuItem($checkboxRedirect) ->addMenuItem($checkboxRedirect)
->addLineBreak('-') ->addLineBreak('-')
->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) { ->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('-'); ->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; return $menu;
} }
@ -295,15 +216,23 @@ class NginxVhostsCommand extends Command
$submenuSelectTemplate = $this->selectTemplate(); $submenuSelectTemplate = $this->selectTemplate();
// create menu // create menu
$main = $this->menu('Nginx')
$mainmenu = $this->menu('Nginx')
// add Submenu for select templates // add Submenu for select templates
->addSubMenu('add', $submenuSelectTemplate) ->addSubMenu('add', $submenuSelectTemplate)
->addLineBreak('-'); ->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();
} }
} }

+ 150
- 0
app/Factories/NginxVhostFactory.php View File

@ -0,0 +1,150 @@
<?php
namespace App\Factories;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
use PhpSchool\CliMenu\Style\SelectableStyle;
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
class NginxVhostFactory
{
/**
*
* @param [type] $vhost
* @param [type] $mainmenu
* @return [type]
*/
public static function createVhostSubmenu($vhost, $mainmenu)
{
$builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal());
// create checkbox for disable / enabled
$checkbox = self::createVhostCheckbox($vhost);
// 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!');
// get
$parent = $menu->getParent();
$menu->closeThis();
// remove current vhost from mainmenu
$parent->removeItem($parent->getSelectedItem());
$parent->open();
}
})
->addLineBreak('-')
->addMenuItem($checkbox)
->addLineBreak('-');
$submenu = $builder->build();
$submenu->setParent($mainmenu);
$submenu->setStyle($mainmenu->getStyle());
$item = new MenuMenuItem(
$vhost['domain'],
$submenu,
$builder->isMenuDisabled()
);
return $item;
}
/**
*
*
*/
public static function addVhosts($mainmenu, $vhosts, $vhostIndex)
{
// get items
$items = $mainmenu->getItems();
// new items
$newItems = [];
foreach($items as $index => $item) {
if ($index <= $vhostIndex) {
$newItems[] = $item;
} else {
break;
}
}
// check for linebreak
foreach($items as $index => $item) {
if ($index > $vhostIndex && get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
$startIndex = $index;
break;
}
}
// add submenu for each vhost
foreach($vhosts as $vhost) {
$newItems[] = self::createVhostSubmenu($vhost, $mainmenu);
}
// fillup last items from mainmenu
foreach($items as $index => $item) {
if ($index >= $startIndex) {
$newItems[] = $item;
}
}
$mainmenu->setItems($newItems);
return $mainmenu;
}
/**
*
* @param [type] $vhost
* @return [type]
*/
private static function createVhostCheckbox($vhost)
{
// create checkbox for enabled / disabled
$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!');
}
});
return $checkbox;
}
}

Loading…
Cancel
Save