OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Menus\Nginx;
  3. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  4. use PhpSchool\CliMenu\CliMenu;
  5. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  6. use PhpSchool\CliMenu\Style\SelectableStyle;
  7. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  8. use PhpSchool\CliMenu\MenuItem\StaticItem;
  9. use Respect\Validation\Validator as v;
  10. use Respect\Validation\Exceptions\NestedValidationException;
  11. use App\Menus\ItemValidator;
  12. use App\BladeFile;
  13. use App\Helpers\NginxTemplateHelper;
  14. use App\Facades\TerminalHelper;
  15. /**
  16. *
  17. *
  18. * @author Björn Hase, Tentakelfabrik
  19. * @license http://opensource.org/licenses/MIT The MIT License
  20. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
  21. *
  22. */
  23. class TemplateMenuFactory
  24. {
  25. // path templates
  26. const TEMPLATES_DIR = '/resources/nginx/templates';
  27. private $configuration = [];
  28. /**
  29. * default configuration
  30. *
  31. * @TODO will be removed after
  32. *
  33. * @return array
  34. */
  35. private function getConfiguration()
  36. {
  37. return [
  38. 'domain' => '',
  39. 'root' => '',
  40. 'index' => 'index.php',
  41. 'ssl' => true,
  42. 'redirect_www' => true
  43. ];
  44. }
  45. /**
  46. * add item to select template
  47. *
  48. *
  49. * @return PhpSchool\CliMenu\Builder\CliMenu
  50. *
  51. */
  52. public function addSelectTemplateItem()
  53. {
  54. $menu = function(CliMenuBuilder $builder)
  55. {
  56. // create blade
  57. $bladeFile = new BladeFile(self::TEMPLATES_DIR);
  58. $builder
  59. ->setTitle('Nginx > Add')
  60. ->setGoBackButtonText('Back');
  61. $nginxTemplateHelper = new NginxTemplateHelper();
  62. // get templates
  63. foreach($nginxTemplateHelper->find() as $template) {
  64. $submenuCallable = $this->addCreateItem($template, $bladeFile);
  65. $builder
  66. ->addSubMenu($template['name'], $submenuCallable);
  67. }
  68. $builder->addLineBreak('-');
  69. };
  70. return $menu;
  71. }
  72. /**
  73. * add input item
  74. *
  75. * @param string $key
  76. * @param string $label
  77. * @param array $configuration
  78. */
  79. private function addInputItem($key, $label, $itemValidator = NULL)
  80. {
  81. $callable = function(CliMenu $menu) use ($key, $label, $itemValidator)
  82. {
  83. $input = $menu->askText();
  84. if ($this->configuration[$key]) {
  85. $input->setPlaceholderText($this->configuration[$key]);
  86. }
  87. $result = $input->ask();
  88. $this->configuration[$key] = $result->fetch();
  89. $menu->getSelectedItem()->setText($label.': '.$result->fetch());
  90. if ($itemValidator) {
  91. $itemValidator->validate($menu, $menu->getSelectedItem(), [ $key => $result->fetch() ]);
  92. }
  93. $menu->redraw();
  94. };
  95. return $callable;
  96. }
  97. /**
  98. * add item to publish configuration
  99. *
  100. * @param string $template
  101. * @param object $bladeFile
  102. * @param array $configuration
  103. */
  104. private function addPublishItem($template, $bladeFile)
  105. {
  106. $callable = function(CliMenu $menu) use ($template, $bladeFile)
  107. {
  108. // getting configuration
  109. $data = $this->configuration;
  110. $validator = v::key('domain', v::domain(false))
  111. ->key('root', v::notEmpty())
  112. ->key('index', v::notEmpty())
  113. ->key('phpFpm', v::notEmpty());
  114. try {
  115. $validator->assert($data);
  116. } catch(NestedValidationException $exception) {
  117. $errors = $exception->getMessages();
  118. }
  119. if (isset($errors)) {
  120. TerminalHelper::confirmArray($menu, $errors);
  121. } else {
  122. // create filename
  123. $filename = $this->configuration['domain'].'.conf';
  124. // write configuration to file
  125. $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $this->configuration);
  126. $menu->confirm('Success!')->display('Ok!');
  127. $this->configuration = $this->getConfiguration();
  128. // invoke action
  129. $action = new NginxVhostGoBackAction();
  130. is_callable($action($menu));
  131. }
  132. };
  133. return $callable;
  134. }
  135. /**
  136. *
  137. *
  138. *
  139. */
  140. private function addCancelItem()
  141. {
  142. $callable = function(CliMenu $menu)
  143. {
  144. $this->configuration = $this->getConfiguration();
  145. $action = new NginxVhostCancelAction();
  146. is_callable($action($menu));
  147. };
  148. return $callable;
  149. }
  150. /**
  151. * adding radio buttons to select php-fpm version
  152. *
  153. *
  154. * @param CliMenuBuilder $builder
  155. * @param array $configuration
  156. */
  157. private function addPhpFpmItems($builder)
  158. {
  159. // get php-fpm services
  160. exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files);
  161. foreach($files as $index => $file) {
  162. // remove path
  163. $file = str_replace('/lib/systemd/system/', '', $file);
  164. // remove extension
  165. $file = str_replace('.service', '', $file);
  166. $builder->addRadioItem($file, function(CliMenu $menu) {
  167. $this->configuration['phpFpm'] = $menu->getSelectedItem()->getText();
  168. });
  169. }
  170. return $builder;
  171. }
  172. /**
  173. * add create item
  174. *
  175. *
  176. * @param array $template
  177. * @param Blade $blade
  178. * @return CliMenuBuilder
  179. */
  180. private function addCreateItem($template, $bladeFile)
  181. {
  182. $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
  183. {
  184. $this->configuration = $this->getConfiguration();
  185. // create checkbox for ssl
  186. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) {
  187. $this->configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  188. });
  189. $checkboxSSL->setChecked($this->configuration['ssl']);
  190. // create checkbox for redirect from www
  191. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) {
  192. $this->configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  193. });
  194. $checkboxRedirect->setChecked($this->configuration['redirect_www']);
  195. $validator = v::key('root', v::directory());
  196. $itemValidator = new ItemValidator($validator);
  197. $builder
  198. ->setTitle('Nginx > Add > '.$template['name'])
  199. ->disableDefaultItems()
  200. // input domain
  201. ->addItem('domain: -', $this->addInputItem('domain', 'domain'))
  202. // input root
  203. ->addItem('root: -', $this->addInputItem('root', 'root', $itemValidator))
  204. // input index
  205. ->addItem('index: '.$this->configuration['index'], $this->addInputItem('index', 'index'))
  206. ->addLineBreak('-');
  207. // add php-fpm items
  208. $builder = $this->addPhpFpmItems($builder);
  209. $builder
  210. ->addLineBreak('-')
  211. // options
  212. ->addMenuItem($checkboxSSL)
  213. ->addMenuItem($checkboxRedirect)
  214. ->addLineBreak('-')
  215. // create
  216. ->addItem('Publish', $this->addPublishItem($template, $bladeFile))
  217. ->addLineBreak('-')
  218. ->addItem('Cancel', $this->addCancelItem());
  219. };
  220. return $menu;
  221. }
  222. }