diff --git a/app/Menus/ItemValidator.php b/app/Menus/ItemValidator.php new file mode 100644 index 0000000..027a679 --- /dev/null +++ b/app/Menus/ItemValidator.php @@ -0,0 +1,119 @@ +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); + } + } +} \ No newline at end of file diff --git a/app/Menus/Nginx/TemplateMenuFactory.php b/app/Menus/Nginx/TemplateMenuFactory.php index 2bd1fed..98d3aeb 100644 --- a/app/Menus/Nginx/TemplateMenuFactory.php +++ b/app/Menus/Nginx/TemplateMenuFactory.php @@ -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))