Browse Source

adding #5

release/0.1
Björn 4 years ago
parent
commit
b04082e916
2 changed files with 63 additions and 20 deletions
  1. +23
    -8
      app/Commands/NginxVhostsCommand.php
  2. +40
    -12
      app/Factories/NginxVhostFactory.php

+ 23
- 8
app/Commands/NginxVhostsCommand.php View File

@ -21,7 +21,9 @@ use Log;
use Closure; use Closure;
/** /**
* Add and Edit Configurations of Vhosts from Nginx
* *
* Using php-school/cli-menu
* *
* *
* *
@ -32,7 +34,7 @@ class NginxVhostsCommand extends Command
const TEMPLATES_DIR = '/resources/nginx/templates'; const TEMPLATES_DIR = '/resources/nginx/templates';
// index for vhosts // index for vhosts
const VHOST_INDEX = 2;
const VHOST_INDEX = 1;
/** /**
* The signature of the command. * The signature of the command.
@ -50,8 +52,8 @@ class NginxVhostsCommand extends Command
/** /**
* *
*
*
* @param string $prefix [description]
* @return [type] [description]
*/ */
private function getTemplates($prefix = '') private function getTemplates($prefix = '')
{ {
@ -154,6 +156,8 @@ class NginxVhostsCommand extends Command
$builder $builder
->setTitle('Nginx Vhosts > add > '.$template['name']) ->setTitle('Nginx Vhosts > add > '.$template['name'])
// input domain
->addItem('domain', function(CliMenu $menu) use (&$configuration) { ->addItem('domain', function(CliMenu $menu) use (&$configuration) {
$result = $menu->askText()->ask(); $result = $menu->askText()->ask();
$configuration['domain'] = $result->fetch(); $configuration['domain'] = $result->fetch();
@ -161,6 +165,8 @@ class NginxVhostsCommand extends Command
$menu->getSelectedItem()->setText('domain -> '.$result->fetch()); $menu->getSelectedItem()->setText('domain -> '.$result->fetch());
$menu->redraw(); $menu->redraw();
}) })
// input root
->addItem('root', function(CliMenu $menu) use (&$configuration) { ->addItem('root', function(CliMenu $menu) use (&$configuration) {
$result = $menu->askText()->ask(); $result = $menu->askText()->ask();
$configuration['root'] = $result->fetch(); $configuration['root'] = $result->fetch();
@ -168,6 +174,8 @@ class NginxVhostsCommand extends Command
$menu->getSelectedItem()->setText('root -> '.$result->fetch()); $menu->getSelectedItem()->setText('root -> '.$result->fetch());
$menu->redraw(); $menu->redraw();
}) })
// input index
->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) { ->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) {
$result = $menu->askText()->ask(); $result = $menu->askText()->ask();
$configuration['index'] = $result->fetch(); $configuration['index'] = $result->fetch();
@ -176,10 +184,14 @@ class NginxVhostsCommand extends Command
$menu->redraw(); $menu->redraw();
}) })
->addLineBreak('-') ->addLineBreak('-')
// options
->addMenuItem($checkboxSSL) ->addMenuItem($checkboxSSL)
->addMenuItem($checkboxRedirect) ->addMenuItem($checkboxRedirect)
->addLineBreak('-') ->addLineBreak('-')
->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) {
// create
->addItem('create', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) {
// render configuration // render configuration
$content = $blade->render($template['name'], $configuration); $content = $blade->render($template['name'], $configuration);
@ -217,12 +229,15 @@ class NginxVhostsCommand extends Command
// create menu // create menu
$mainmenu = $this->menu('Nginx') $mainmenu = $this->menu('Nginx')
// change style of menu
->modifySelectableStyle(function(SelectableStyle $style) {
$style->setSelectedMarker("\xE2\x96\xB6 ")
->setUnselectedMarker(' ');
})
// add Submenu for select templates // add Submenu for select templates
->addSubMenu('add', $submenuSelectTemplate)
->addLineBreak('-') ->addLineBreak('-')
->modifySelectableStyle(function (SelectableStyle $style) {
$style->setItemExtra('[ enabled ]');
});
->addSubMenu('add', $submenuSelectTemplate);
$mainmenu->addLineBreak('-'); $mainmenu->addLineBreak('-');


+ 40
- 12
app/Factories/NginxVhostFactory.php View File

@ -6,6 +6,7 @@ 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\MenuItem\MenuMenuItem; use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
use PhpSchool\CliMenu\Style\SelectableStyle;
class NginxVhostFactory class NginxVhostFactory
{ {
@ -24,21 +25,22 @@ class NginxVhostFactory
// new items // new items
$newItems = []; $newItems = [];
// check for linebreak-object
foreach($items as $index => $item) { foreach($items as $index => $item) {
if ($index <= $vhostIndex) {
$newItems[] = $item;
} else {
if (get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
$startIndex = $index + 1;
break; break;
} }
} }
// check for linebreak-object
/***
foreach($items as $index => $item) { foreach($items as $index => $item) {
if ($index > $vhostIndex && get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
$startIndex = $index;
if ($index <= $vhostIndex) {
$newItems[] = $item;
} else {
break; break;
} }
}
}*/
// add submenu for each vhost // add submenu for each vhost
foreach($vhosts as $vhost) { foreach($vhosts as $vhost) {
@ -68,6 +70,12 @@ class NginxVhostFactory
{ {
$builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal()); $builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal());
// style selected and unselected
$builder->modifySelectableStyle(function(SelectableStyle $style) {
$style->setSelectedMarker('> ')
->setUnselectedMarker(' ');
});
// create checkbox for disable / enabled // create checkbox for disable / enabled
$checkbox = self::createVhostCheckbox($vhost); $checkbox = self::createVhostCheckbox($vhost);
@ -79,14 +87,22 @@ class NginxVhostFactory
$builder $builder
->setTitle('Nginx > '.$vhost['domain']) ->setTitle('Nginx > '.$vhost['domain'])
->setGoBackButtonText('Back') ->setGoBackButtonText('Back')
->addLineBreak('')
// edit configuration
->addItem('edit', function(CliMenu $menu) use ($vhost) { ->addItem('edit', function(CliMenu $menu) use ($vhost) {
system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`'); system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
}) })
// delete configuration
->addItem('delete', function(CliMenu $menu) use ($vhost) { ->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 {
// input domain for confirmation
$result = $menu->askText()
->setPromptText('Enter the domain name as confirmation: '.$vhost['domain'].' / [ESC] for Cancel')
->ask();
// if result matching delete vhost an close menu
if ($result->fetch() === $vhost['domain']) {
unlink('/etc/nginx/sites-available/'.$vhost['file']); unlink('/etc/nginx/sites-available/'.$vhost['file']);
$menu->confirm($vhost['domain'].' is deleted!')->display('OK!'); $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
@ -97,8 +113,17 @@ class NginxVhostFactory
// remove current vhost from mainmenu // remove current vhost from mainmenu
$parent->removeItem($parent->getSelectedItem()); $parent->removeItem($parent->getSelectedItem());
$parent->open(); $parent->open();
// cancel input
} else if (empty($result->fetch())) {
$menu->flash('Cancel')->display();
// if domain not matching
} else {
$menu->flash('Not matching! Domain not deleted!')->display();
} }
})
}, false, $vhost['enabled'])
->addLineBreak('-') ->addLineBreak('-')
->addMenuItem($checkbox) ->addMenuItem($checkbox)
->addLineBreak('-'); ->addLineBreak('-');
@ -108,12 +133,15 @@ class NginxVhostFactory
$submenu->setParent($mainmenu); $submenu->setParent($mainmenu);
$submenu->setStyle($mainmenu->getStyle()); $submenu->setStyle($mainmenu->getStyle());
// create MenuMenuItem
$item = new MenuMenuItem( $item = new MenuMenuItem(
$vhost['domain'], $vhost['domain'],
$submenu, $submenu,
$builder->isMenuDisabled() $builder->isMenuDisabled()
); );
$item->getStyle()->setSelectedMarker("\xF0\x9D\x8C\xA1 ")->setUnselectedMarker(' ');
// show item extra if domain is enabled // show item extra if domain is enabled
$item->getStyle()->setItemExtra('[ enabled ]'); $item->getStyle()->setItemExtra('[ enabled ]');
if ($vhost['enabled']) { if ($vhost['enabled']) {


Loading…
Cancel
Save