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.

240 lines
7.1 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
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\Commands;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use LaravelZero\Framework\Commands\Command;
  5. use Illuminate\Support\Facades\File;
  6. use App\Facades\Install;
  7. use App\Facades\NginxVhost;
  8. use App\Facades\NginxVhostFactory;
  9. use App\Facades\TerminalHelper;
  10. use App\Facades\Menus\StylesFactory;
  11. use App\Helpers\NginxTemplateHelper;
  12. use App\BladeFile;
  13. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  14. use PhpSchool\CliMenu\CliMenu;
  15. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  16. use PhpSchool\CliMenu\Style\SelectableStyle;
  17. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  18. use Respect\Validation\Validator as v;
  19. use Respect\Validation\Exceptions\NestedValidationException;
  20. use Log;
  21. use Closure;
  22. /**
  23. * Add and Edit Configurations of Vhosts from Nginx
  24. *
  25. * Using php-school/cli-menu
  26. *
  27. *
  28. *
  29. */
  30. class NginxVhostsCommand extends Command
  31. {
  32. // path templates
  33. const TEMPLATES_DIR = '/resources/nginx/templates';
  34. // index for vhosts
  35. const VHOST_INDEX = 0;
  36. /**
  37. * The signature of the command.
  38. *
  39. * @var string
  40. */
  41. protected $signature = 'nginx:vhosts';
  42. /**
  43. * The description of the command.
  44. *
  45. * @var string
  46. */
  47. protected $description = 'Create and Manage Nginx Vhosts';
  48. /**
  49. *
  50. *
  51. *
  52. */
  53. private function selectTemplate()
  54. {
  55. $menu = function(CliMenuBuilder $builder)
  56. {
  57. // create blade
  58. $bladeFile = new BladeFile(self::TEMPLATES_DIR);
  59. $builder
  60. ->setTitle('Nginx > Add')
  61. ->setGoBackButtonText('Back');
  62. $nginxTemplateHelper = new NginxTemplateHelper();
  63. foreach($nginxTemplateHelper->find() as $template) {
  64. $submenuCallable = $this->createConfiguration($template, $bladeFile);
  65. $builder
  66. ->addSubMenu($template['name'], $submenuCallable);
  67. }
  68. $builder->addLineBreak('-');
  69. };
  70. return $menu;
  71. }
  72. /**
  73. * create Configuration, add for each template a submenu
  74. *
  75. *
  76. * @param array $template
  77. * @param Blade $blade
  78. * @return CliMenuBuilder
  79. */
  80. private function createConfiguration($template, $bladeFile)
  81. {
  82. $menu = function(CliMenuBuilder $builder) use ($template, $bladeFile)
  83. {
  84. $configuration = [
  85. 'domain' => '',
  86. 'root' => '',
  87. 'index' => 'index.php',
  88. 'ssl' => true,
  89. 'redirect_www' => true
  90. ];
  91. // create checkbox for ssl
  92. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  93. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  94. });
  95. $checkboxSSL->setChecked($configuration['ssl']);
  96. // create checkbox for redirect from www
  97. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  98. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  99. });
  100. $checkboxRedirect->setChecked($configuration['redirect_www']);
  101. $builder
  102. ->setTitle('Nginx > Add > '.$template['name'])
  103. ->setGoBackButtonText('Cancel')
  104. // input domain
  105. ->addItem('domain: -', function(CliMenu $menu) use (&$configuration) {
  106. $result = $menu->askText()->ask();
  107. $configuration['domain'] = $result->fetch();
  108. $menu->getSelectedItem()->setText('domain: '.$result->fetch());
  109. $menu->redraw();
  110. })
  111. // input root
  112. ->addItem('root: -', function(CliMenu $menu) use (&$configuration) {
  113. $result = $menu->askText()->ask();
  114. $configuration['root'] = $result->fetch();
  115. $menu->getSelectedItem()->setText('root: '.$result->fetch());
  116. $menu->redraw();
  117. })
  118. // input index
  119. ->addItem('index: '.$configuration['index'], function(CliMenu $menu) use (&$configuration) {
  120. $result = $menu->askText()->ask();
  121. $configuration['index'] = $result->fetch();
  122. $menu->getSelectedItem()->setText('index: '.$result->fetch());
  123. $menu->redraw();
  124. })
  125. ->addLineBreak('-')
  126. // options
  127. ->addMenuItem($checkboxSSL)
  128. ->addMenuItem($checkboxRedirect)
  129. ->addLineBreak('-')
  130. // create
  131. ->addItem('Create', function(CliMenu $menu) use (&$configuration, $template, $bladeFile) {
  132. $data = $configuration;
  133. // add directory for validator to check if file exits
  134. $data['index'] = $data['root'].'/'.$data['index'];
  135. $validator = v::key('domain', v::domain(false))
  136. ->key('root', v::directory())
  137. ->key('index', v::file());
  138. try {
  139. $validator->assert($data);
  140. } catch(NestedValidationException $exception) {
  141. $errors = $exception->getMessages();
  142. }
  143. if (isset($errors)) {
  144. TerminalHelper::confirmArray($menu, $errors);
  145. } else {
  146. // create filename
  147. $filename = $configuration['domain'].'.conf';
  148. // write configuration to file
  149. $bladeFile->put($template['name'], '/etc/nginx/sites-available/'.$filename, $configuration);
  150. $menu->confirm('Success!')->display('Ok!');
  151. // prepare for close current submenu and open mainmenu
  152. $parent = $menu->getParent();
  153. $menu->closeThis();
  154. $mainmenu = $parent->getParent();
  155. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  156. $mainmenu->open();
  157. }
  158. })
  159. ->addLineBreak('-');
  160. };
  161. return $menu;
  162. }
  163. /**
  164. * let it rain
  165. *
  166. *
  167. */
  168. public function handle()
  169. {
  170. $submenuSelectTemplate = $this->selectTemplate();
  171. // create menu
  172. $builder = $this->menu('Nginx')
  173. // add Submenu for select templates
  174. ->addLineBreak('-')
  175. ->addSubMenu('Add', $submenuSelectTemplate);
  176. $builder->addLineBreak('-');
  177. // apperance
  178. $builder = StylesFactory::setMenuStyles($builder);
  179. $mainmenu = $builder->build();
  180. // remove first item
  181. $items = $mainmenu->getItems();
  182. unset($items[0]);
  183. $mainmenu->setItems($items);
  184. // adding current vhosts
  185. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  186. $mainmenu->open();
  187. }
  188. }