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.

253 lines
7.2 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
  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. /**
  28. * add item to select template
  29. *
  30. *
  31. * @return PhpSchool\CliMenu\Builder\CliMenu
  32. *
  33. */
  34. public function addSelectTemplateItem()
  35. {
  36. $menu = function(CliMenuBuilder $builder)
  37. {
  38. // create blade
  39. $bladeFile = new BladeFile(self::TEMPLATES_DIR);
  40. $builder
  41. ->setTitle('Nginx > Add')
  42. ->setGoBackButtonText('Back');
  43. $nginxTemplateHelper = new NginxTemplateHelper();
  44. // get templates
  45. foreach($nginxTemplateHelper->find() as $template) {
  46. $submenuCallable = $this->addCreateItem($template, $bladeFile);
  47. $builder
  48. ->addSubMenu($template['name'], $submenuCallable);
  49. }
  50. $builder->addLineBreak('-');
  51. };
  52. return $menu;
  53. }
  54. /**
  55. * default configuration
  56. *
  57. * @TODO will be removed after
  58. *
  59. * @return array
  60. */
  61. private function getConfiguration()
  62. {
  63. return [
  64. 'domain' => '',
  65. 'root' => '',
  66. 'index' => 'index.php',
  67. 'ssl' => true,
  68. 'redirect_www' => true
  69. ];
  70. }
  71. /**
  72. * add input item
  73. *
  74. * @param string $key
  75. * @param string $label
  76. * @param array $configuration
  77. */
  78. private function addInputItem($key, $label, &$configuration, $itemValidator = NULL)
  79. {
  80. $callable = function(CliMenu $menu) use ($key, $label, &$configuration, $itemValidator)
  81. {
  82. $input = $menu->askText();
  83. if ($configuration[$key]) {
  84. $input->setPlaceholderText($configuration[$key]);
  85. }
  86. $result = $input->ask();
  87. $configuration[$key] = $result->fetch();
  88. $menu->getSelectedItem()->setText($label.': '.$result->fetch());
  89. if ($itemValidator) {
  90. $itemValidator->validate($menu, $menu->getSelectedItem(), [ $key => $result->fetch() ]);
  91. }
  92. $menu->redraw();
  93. };
  94. return $callable;
  95. }
  96. /**
  97. * add item to publish configuration
  98. *
  99. * @param string $template
  100. * @param object $bladeFile
  101. * @param array $configuration
  102. */
  103. private function addPublishItem($template, $bladeFile, &$configuration)
  104. {
  105. $callable = function(CliMenu $menu) use ($template, $bladeFile, &$configuration)
  106. {
  107. // getting configuration
  108. $data = $configuration;
  109. $validator = v::key('domain', v::domain(false))
  110. ->key('root', v::notEmpty())
  111. ->key('index', v::notEmpty())
  112. ->key('phpFpm', v::notEmpty());
  113. try {
  114. $validator->assert($data);
  115. } catch(NestedValidationException $exception) {
  116. $errors = $exception->getMessages();
  117. }
  118. if (isset($errors)) {
  119. TerminalHelper::confirmArray($menu, $errors);
  120. } else {
  121. // create filename
  122. $filename = $configuration['domain'].'.conf';
  123. // write configuration to file
  124. $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $configuration);
  125. $menu->confirm('Success!')->display('Ok!');
  126. // invoke action
  127. $action = new NginxVhostGoBackAction();
  128. is_callable($action($menu));
  129. }
  130. };
  131. return $callable;
  132. }
  133. /**
  134. * adding radio buttons to select php-fpm version
  135. *
  136. *
  137. * @param CliMenuBuilder $builder
  138. * @param array $configuration
  139. */
  140. private function addPhpFpmItems($builder, &$configuration)
  141. {
  142. // get php-fpm services
  143. exec('find /lib/systemd/system/ -name "php[0-9\.]*-fpm.service"', $files);
  144. foreach($files as $index => $file) {
  145. // remove path
  146. $file = str_replace('/lib/systemd/system/', '', $file);
  147. // remove extension
  148. $file = str_replace('.service', '', $file);
  149. $builder->addRadioItem($file, function(CliMenu $menu) use (&$configuration) {
  150. $configuration['phpFpm'] = $menu->getSelectedItem()->getText();
  151. });
  152. }
  153. return $builder;
  154. }
  155. /**
  156. * add create item
  157. *
  158. *
  159. * @param array $template
  160. * @param Blade $blade
  161. * @return CliMenuBuilder
  162. */
  163. private function addCreateItem($template, $bladeFile)
  164. {
  165. $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
  166. {
  167. $configuration = $this->getConfiguration();
  168. // create checkbox for ssl
  169. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  170. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  171. });
  172. $checkboxSSL->setChecked($configuration['ssl']);
  173. // create checkbox for redirect from www
  174. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  175. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  176. });
  177. $checkboxRedirect->setChecked($configuration['redirect_www']);
  178. $validator = v::key('root', v::directory());
  179. $itemValidator = new ItemValidator($validator);
  180. $builder
  181. ->setTitle('Nginx > Add > '.$template['name'])
  182. ->setGoBackButtonText('Cancel')
  183. // input domain
  184. ->addItem('domain: -', $this->addInputItem('domain', 'domain', $configuration))
  185. // input root
  186. ->addItem('root: -', $this->addInputItem('root', 'root', $configuration, $itemValidator))
  187. // input index
  188. ->addItem('index: '.$configuration['index'], $this->addInputItem('index', 'index', $configuration))
  189. ->addLineBreak('-');
  190. // add php-fpm items
  191. $builder = $this->addPhpFpmItems($builder, $configuration);
  192. $builder
  193. ->addLineBreak('-')
  194. // options
  195. ->addMenuItem($checkboxSSL)
  196. ->addMenuItem($checkboxRedirect)
  197. ->addLineBreak('-')
  198. // create
  199. ->addItem('publish', $this->addPublishItem($template, $bladeFile, $configuration))
  200. ->addLineBreak('-');
  201. };
  202. return $menu;
  203. }
  204. }