Browse Source

adding #40

release/0.2
Björn 4 years ago
parent
commit
2f85fa4520
2 changed files with 135 additions and 8 deletions
  1. +119
    -0
      app/Menus/ItemValidator.php
  2. +16
    -8
      app/Menus/Nginx/TemplateMenuFactory.php

+ 119
- 0
app/Menus/ItemValidator.php View File

@ -0,0 +1,119 @@
<?php
namespace App\Menus;
use PhpSchool\CliMenu\MenuItem\StaticItem;
use Respect\Validation\Exceptions\NestedValidationException;
/**
*
*
*
*
*/
class ItemValidator
{
// items from menu
private $items;
// current message
private $message;
// validator
private $validator;
/**
*
*
*/
public function __construct($validator)
{
$this->validator = $validator;
$this->message = new StaticItem(''); // must create to compare in items
}
/**
* remove all items from menu and save them
*
* @param [type] $menu
*
*/
private function clear(&$menu)
{
$this->items = $menu->getItems();
foreach($menu->getItems() as $item) {
$menu->removeItem($item);
}
}
/**
* add staticItem after item
*
* @param [type] $menu
* @param [type] $after
* @param [type] $target
*
*/
private function addAfter(&$menu, $after, $target)
{
$this->clear($menu);
foreach($this->items as $item) {
$menu->addItem($item);
if ($after === $item) {
$menu->addItem($target, true);
}
}
}
/**
* remove staticItem
*
*
* @param [type] $menu [description]
* @param [type] $target [description]
*
*/
private function remove(&$menu, $target)
{
$this->clear($menu);
foreach($this->items as $item) {
if ($item === $target) {
continue;
}
$menu->addItem($item);
}
}
/**
* validate,
* if failed show staticItem with errors,
* if not remove staticItem if found
*
* @param [type] $menu
* @param [type] $item
* @param [type] $data
*
*/
public function validate(&$menu, $item, $data)
{
try {
$this->validator->assert($data);
} catch(NestedValidationException $exception) {
$errors = $exception->getMessages();
}
if (isset($errors)) {
// @TODO use ColorUtil
$this->message->setText("\033[33m"."\xE2\x9A\xA0 ".join(' ', $errors));
$this->addAfter($menu, $item, $this->message);
} else {
$this->remove($menu, $this->message);
}
}
}

+ 16
- 8
app/Menus/Nginx/TemplateMenuFactory.php View File

@ -7,10 +7,13 @@ use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\MenuItem\CheckboxItem;
use PhpSchool\CliMenu\Style\SelectableStyle;
use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
use PhpSchool\CliMenu\MenuItem\StaticItem;
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationException;
use App\Menus\ItemValidator;
use App\BladeFile;
use App\Helpers\NginxTemplateHelper;
@ -87,9 +90,9 @@ class TemplateMenuFactory
* @param string $label
* @param array $configuration
*/
private function addInputItem($key, $label, &$configuration)
private function addInputItem($key, $label, &$configuration, $itemValidator = NULL)
{
$callable = function(CliMenu $menu) use ($key, $label, &$configuration)
$callable = function(CliMenu $menu) use ($key, $label, &$configuration, $itemValidator)
{
$input = $menu->askText();
@ -101,6 +104,11 @@ class TemplateMenuFactory
$configuration[$key] = $result->fetch();
$menu->getSelectedItem()->setText($label.': '.$result->fetch());
if ($itemValidator) {
$itemValidator->validate($menu, $menu->getSelectedItem(), [ $key => $result->fetch() ]);
}
$menu->redraw();
};
@ -121,12 +129,9 @@ class TemplateMenuFactory
// getting configuration
$data = $configuration;
// add directory for validator to check if file exits
$data['index'] = $data['root'].'/'.$data['index'];
$validator = v::key('domain', v::domain(false))
->key('root', v::directory())
->key('index', v::file())
->key('root', v::notEmpty())
->key('index', v::notEmpty())
->key('phpFpm', v::notEmpty());
try {
@ -211,6 +216,9 @@ class TemplateMenuFactory
$checkboxRedirect->setChecked($configuration['redirect_www']);
$validator = v::key('root', v::directory());
$itemValidator = new ItemValidator($validator);
$builder
->setTitle('Nginx > Add > '.$template['name'])
->setGoBackButtonText('Cancel')
@ -219,7 +227,7 @@ class TemplateMenuFactory
->addItem('domain: -', $this->addInputItem('domain', 'domain', $configuration))
// input root
->addItem('root: -', $this->addInputItem('root', 'root', $configuration))
->addItem('root: -', $this->addInputItem('root', 'root', $configuration, $itemValidator))
// input index
->addItem('index: '.$configuration['index'], $this->addInputItem('index', 'index', $configuration))


Loading…
Cancel
Save