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.

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