<?php
|
|
|
|
namespace App\Menus\Nginx;
|
|
|
|
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
|
|
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;
|
|
use App\Facades\TerminalHelper;
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @author Björn Hase, Tentakelfabrik
|
|
* @license http://opensource.org/licenses/MIT The MIT License
|
|
* @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
|
|
*
|
|
*/
|
|
class TemplateMenuFactory
|
|
{
|
|
// path templates
|
|
const TEMPLATES_DIR = '/resources/nginx/templates';
|
|
|
|
private $configuration = [];
|
|
|
|
/**
|
|
* default configuration
|
|
*
|
|
* @TODO will be removed after
|
|
*
|
|
* @return array
|
|
*/
|
|
private function getConfiguration()
|
|
{
|
|
return [
|
|
'domain' => '',
|
|
'root' => '',
|
|
'index' => 'index.php',
|
|
'ssl' => true,
|
|
'redirect_www' => true
|
|
];
|
|
}
|
|
|
|
/**
|
|
* add item to select template
|
|
*
|
|
*
|
|
* @return PhpSchool\CliMenu\Builder\CliMenu
|
|
*
|
|
*/
|
|
public function addSelectTemplateItem()
|
|
{
|
|
$menu = function(CliMenuBuilder $builder)
|
|
{
|
|
// create blade
|
|
$bladeFile = new BladeFile(self::TEMPLATES_DIR);
|
|
|
|
$builder
|
|
->setTitle('Nginx > Add')
|
|
->setGoBackButtonText('Back');
|
|
|
|
$nginxTemplateHelper = new NginxTemplateHelper();
|
|
|
|
// get templates
|
|
foreach($nginxTemplateHelper->find() as $template) {
|
|
$submenuCallable = $this->addCreateItem($template, $bladeFile);
|
|
$builder
|
|
->addSubMenu($template['name'], $submenuCallable);
|
|
}
|
|
|
|
$builder->addLineBreak('-');
|
|
};
|
|
|
|
return $menu;
|
|
}
|
|
|
|
/**
|
|
* add input item
|
|
*
|
|
* @param string $key
|
|
* @param string $label
|
|
* @param array $configuration
|
|
*/
|
|
private function addInputItem($key, $label, $itemValidator = NULL)
|
|
{
|
|
$callable = function(CliMenu $menu) use ($key, $label, $itemValidator)
|
|
{
|
|
$input = $menu->askText();
|
|
|
|
if ($this->configuration[$key]) {
|
|
$input->setPlaceholderText($this->configuration[$key]);
|
|
}
|
|
|
|
$result = $input->ask();
|
|
$this->configuration[$key] = $result->fetch();
|
|
|
|
$menu->getSelectedItem()->setText($label.': '.$result->fetch());
|
|
|
|
if ($itemValidator) {
|
|
$itemValidator->validate($menu, $menu->getSelectedItem(), [ $key => $result->fetch() ]);
|
|
}
|
|
|
|
$menu->redraw();
|
|
};
|
|
|
|
return $callable;
|
|
}
|
|
|
|
/**
|
|
* add item to publish configuration
|
|
*
|
|
* @param string $template
|
|
* @param object $bladeFile
|
|
* @param array $configuration
|
|
*/
|
|
private function addPublishItem($template, $bladeFile)
|
|
{
|
|
$callable = function(CliMenu $menu) use ($template, $bladeFile)
|
|
{
|
|
// getting configuration
|
|
$data = $this->configuration;
|
|
|
|
$validator = v::key('domain', v::domain(false))
|
|
->key('root', v::notEmpty())
|
|
->key('index', v::notEmpty())
|
|
->key('phpFpm', v::notEmpty());
|
|
|
|
try {
|
|
$validator->assert($data);
|
|
} catch(NestedValidationException $exception) {
|
|
$errors = $exception->getMessages();
|
|
}
|
|
|
|
if (isset($errors)) {
|
|
TerminalHelper::confirmArray($menu, $errors);
|
|
} else {
|
|
|
|
// create filename
|
|
$filename = $this->configuration['domain'].'.conf';
|
|
|
|
// write configuration to file
|
|
$bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $this->configuration);
|
|
$menu->confirm('Success!')->display('Ok!');
|
|
|
|
$this->configuration = $this->getConfiguration();
|
|
|
|
// invoke action
|
|
$action = new NginxVhostGoBackAction();
|
|
is_callable($action($menu));
|
|
}
|
|
};
|
|
|
|
return $callable;
|
|
}
|
|
|
|
/**
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
private function addCancelItem()
|
|
{
|
|
$callable = function(CliMenu $menu)
|
|
{
|
|
$this->configuration = $this->getConfiguration();
|
|
|
|
$action = new NginxVhostCancelAction();
|
|
is_callable($action($menu));
|
|
};
|
|
|
|
return $callable;
|
|
}
|
|
|
|
/**
|
|
* adding radio buttons to select php-fpm version
|
|
*
|
|
*
|
|
* @param CliMenuBuilder $builder
|
|
* @param array $configuration
|
|
*/
|
|
private function addPhpFpmItems($builder)
|
|
{
|
|
// get php-fpm services
|
|
exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files);
|
|
|
|
foreach($files as $index => $file) {
|
|
|
|
// remove path
|
|
$file = str_replace('/lib/systemd/system/', '', $file);
|
|
|
|
// remove extension
|
|
$file = str_replace('.service', '', $file);
|
|
|
|
$builder->addRadioItem($file, function(CliMenu $menu) {
|
|
$this->configuration['phpFpm'] = $menu->getSelectedItem()->getText();
|
|
});
|
|
}
|
|
|
|
return $builder;
|
|
}
|
|
|
|
/**
|
|
* add create item
|
|
*
|
|
*
|
|
* @param array $template
|
|
* @param Blade $blade
|
|
* @return CliMenuBuilder
|
|
*/
|
|
private function addCreateItem($template, $bladeFile)
|
|
{
|
|
$menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
|
|
{
|
|
$this->configuration = $this->getConfiguration();
|
|
|
|
// create checkbox for ssl
|
|
$checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) {
|
|
$this->configuration['ssl'] = $menu->getSelectedItem()->getChecked();
|
|
});
|
|
|
|
$checkboxSSL->setChecked($this->configuration['ssl']);
|
|
|
|
// create checkbox for redirect from www
|
|
$checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) {
|
|
$this->configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
|
|
});
|
|
|
|
$checkboxRedirect->setChecked($this->configuration['redirect_www']);
|
|
|
|
$validator = v::key('root', v::directory());
|
|
$itemValidator = new ItemValidator($validator);
|
|
|
|
$builder
|
|
->setTitle('Nginx > Add > '.$template['name'])
|
|
->disableDefaultItems()
|
|
|
|
// input domain
|
|
->addItem('domain: -', $this->addInputItem('domain', 'domain'))
|
|
|
|
// input root
|
|
->addItem('root: -', $this->addInputItem('root', 'root', $itemValidator))
|
|
|
|
// input index
|
|
->addItem('index: '.$this->configuration['index'], $this->addInputItem('index', 'index'))
|
|
->addLineBreak('-');
|
|
|
|
// add php-fpm items
|
|
$builder = $this->addPhpFpmItems($builder);
|
|
|
|
$builder
|
|
->addLineBreak('-')
|
|
|
|
// options
|
|
->addMenuItem($checkboxSSL)
|
|
->addMenuItem($checkboxRedirect)
|
|
->addLineBreak('-')
|
|
|
|
// create
|
|
->addItem('Publish', $this->addPublishItem($template, $bladeFile))
|
|
->addLineBreak('-')
|
|
->addItem('Cancel', $this->addCancelItem());
|
|
};
|
|
|
|
return $menu;
|
|
}
|
|
}
|