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.

237 lines
6.7 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
  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 Jenssegers\Blade\Blade;
  7. use App\Facades\Install;
  8. use App\Facades\NginxVhost;
  9. use App\Facades\NginxVhostFactory;
  10. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  11. use PhpSchool\CliMenu\CliMenu;
  12. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  13. use PhpSchool\CliMenu\Style\SelectableStyle;
  14. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  15. use Log;
  16. use Closure;
  17. /**
  18. *
  19. *
  20. *
  21. *
  22. */
  23. class NginxVhostsCommand extends Command
  24. {
  25. // path templates
  26. const TEMPLATES_DIR = '/resources/nginx/templates';
  27. // index for vhosts
  28. const VHOST_INDEX = 2;
  29. /**
  30. * The signature of the command.
  31. *
  32. * @var string
  33. */
  34. protected $signature = 'nginx:vhosts';
  35. /**
  36. * The description of the command.
  37. *
  38. * @var string
  39. */
  40. protected $description = 'Create and Manage Nginx Vhosts';
  41. /**
  42. *
  43. *
  44. *
  45. */
  46. private function getTemplates($prefix = '')
  47. {
  48. // templates
  49. $templates = [];
  50. // getting templates
  51. $files = scandir(base_path().self::TEMPLATES_DIR);
  52. foreach($files as $file) {
  53. // create filepath
  54. $filepath = base_path().self::TEMPLATES_DIR.'/'.$file;
  55. // getting info
  56. $pathinfo = pathinfo($filepath);
  57. // if extension isset and php
  58. if (isset($pathinfo['extension']) && $pathinfo['extension'] === 'php') {
  59. $name = str_replace('.blade.php', '', $file);
  60. $templates[] = [
  61. 'name' => $name,
  62. 'file' => $file,
  63. 'filepath' => $filepath
  64. ];
  65. }
  66. }
  67. return $templates;
  68. }
  69. /**
  70. *
  71. *
  72. *
  73. */
  74. private function selectTemplate()
  75. {
  76. // getting templates
  77. $templates = $this->getTemplates();
  78. $menu = function(CliMenuBuilder $builder) use ($templates)
  79. {
  80. // writing configuration
  81. $blade = new Blade(base_path().self::TEMPLATES_DIR, base_path().'/storage/cache');
  82. $builder
  83. ->setTitle('Nginx > add')
  84. ->setGoBackButtonText('Back');
  85. foreach($templates as $template) {
  86. $submenuCallable = $this->createConfiguration($template, $blade);
  87. $builder
  88. ->addSubMenu($template['name'], $submenuCallable);
  89. }
  90. $builder->addLineBreak('-');
  91. };
  92. return $menu;
  93. }
  94. /**
  95. * create Configuration, add for each template a submenu
  96. *
  97. *
  98. * @param array $template
  99. * @param Blade $blade
  100. * @return CliMenuBuilder
  101. */
  102. private function createConfiguration($template, $blade)
  103. {
  104. $self = $this;
  105. $menu = function(CliMenuBuilder $builder) use ($template, $blade, $self)
  106. {
  107. $configuration = [
  108. 'domain' => '',
  109. 'root' => '',
  110. 'index' => 'index.php',
  111. 'ssl' => true,
  112. 'redirect_www' => true
  113. ];
  114. // create checkbox for ssl
  115. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  116. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  117. });
  118. $checkboxSSL->setChecked($configuration['ssl']);
  119. // create checkbox for redirect from www
  120. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  121. $configuration['redirect_www'] = $menu->getSelectedItem()->getChecked();
  122. });
  123. $checkboxRedirect->setChecked($configuration['redirect_www']);
  124. $builder
  125. ->setTitle('Nginx Vhosts > add > '.$template['name'])
  126. ->addItem('domain', function(CliMenu $menu) use (&$configuration) {
  127. $result = $menu->askText()->ask();
  128. $configuration['domain'] = $result->fetch();
  129. $menu->getSelectedItem()->setText('domain -> '.$result->fetch());
  130. $menu->redraw();
  131. })
  132. ->addItem('root', function(CliMenu $menu) use (&$configuration) {
  133. $result = $menu->askText()->ask();
  134. $configuration['root'] = $result->fetch();
  135. $menu->getSelectedItem()->setText('root -> '.$result->fetch());
  136. $menu->redraw();
  137. })
  138. ->addItem($configuration['index'], function(CliMenu $menu) use (&$configuration) {
  139. $result = $menu->askText()->ask();
  140. $configuration['index'] = $result->fetch();
  141. $menu->getSelectedItem()->setText($result->fetch());
  142. $menu->redraw();
  143. })
  144. ->addLineBreak('-')
  145. ->addMenuItem($checkboxSSL)
  146. ->addMenuItem($checkboxRedirect)
  147. ->addLineBreak('-')
  148. ->addItem('save', function(CliMenu $menu) use (&$configuration, $template, $blade, $self) {
  149. // render configuration
  150. $content = $blade->render($template['name'], $configuration);
  151. // create filename
  152. $filename = $configuration['domain'].'.conf';
  153. // write configuration to file
  154. file_put_contents('/etc/nginx/sites-available/'.$filename, $content);
  155. // prepare for close current submenu and open mainmenu
  156. $parent = $menu->getParent();
  157. $menu->closeThis();
  158. $mainmenu = $parent->getParent();
  159. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  160. $mainmenu->open();
  161. })
  162. ->addLineBreak('-');
  163. };
  164. return $menu;
  165. }
  166. /**
  167. * let it rain
  168. *
  169. *
  170. */
  171. public function handle()
  172. {
  173. $submenuSelectTemplate = $this->selectTemplate();
  174. // create menu
  175. $mainmenu = $this->menu('Nginx')
  176. // add Submenu for select templates
  177. ->addSubMenu('add', $submenuSelectTemplate)
  178. ->addLineBreak('-');
  179. $mainmenu->addLineBreak('-');
  180. // apperance
  181. $mainmenu->setWidth($mainmenu->getTerminal()->getWidth());
  182. $mainmenu->setMargin(2);
  183. $mainmenu = $mainmenu->build();
  184. // adding current vhosts
  185. $mainmenu = NginxVhostFactory::addVhosts($mainmenu, NginxVhost::find(), self::VHOST_INDEX);
  186. $mainmenu->open();
  187. }
  188. }