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 * @param [type] $mainmenu * @return [type] */ private 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; } /** * * @param [type] $vhost * @return [type] */ private 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; } }