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.

245 lines
6.9 KiB

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