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.

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