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.

176 lines
6.4 KiB

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 PhpSchool\CliMenu\Builder\CliMenuBuilder;
  8. use PhpSchool\CliMenu\CliMenu;
  9. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  10. use PhpSchool\CliMenu\Style\SelectableStyle;
  11. use Log;
  12. class NginxVhostsCommand extends Command
  13. {
  14. // path templates
  15. const TEMPLATES_DIR = '/resources/templates/nginx';
  16. /**
  17. * The signature of the command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'nginx:vhosts';
  22. /**
  23. * The description of the command.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Create and Manage Nginx Vhosts';
  28. /**
  29. *
  30. *
  31. *
  32. */
  33. private function add(CliMenuBuilder $builder)
  34. {
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. // create menu
  44. $main = $this->menu(self::MENU_TITLE)
  45. ->addSubMenu('add', function(CliMenuBuilder $builder) {
  46. // getting templates
  47. $templates = scandir(base_path().self::TEMPLATES_DIR);
  48. foreach($templates as $template) {
  49. $builder
  50. ->addSubMenu($template, function(CliMenuBuilder $builder) use ($template) {
  51. $configuration = [
  52. 'domain' => '',
  53. 'ssl' => true,
  54. 'redirect_www' => true
  55. ];
  56. $checkboxSSL = new CheckboxItem('ssl', function(CliMenu $menu) use (&$configuration) {
  57. $configuration['ssl'] = $menu->getSelectedItem()->getChecked();
  58. });
  59. $checkboxSSL->setChecked($configuration['ssl']);
  60. $checkboxRedirect = new CheckboxItem('redirect www', function(CliMenu $menu) use (&$configuration) {
  61. $configuration['redirect www'] = $menu->getSelectedItem()->getChecked();
  62. });
  63. $checkboxRedirect->setChecked($configuration['redirect_www']);
  64. $builder
  65. ->setTitle('Nginx Vhosts > add > '.$template)
  66. ->addItem('domain', function(CliMenu $menu) use (&$configuration) {
  67. $result = $menu->askText()->ask();
  68. $configuration['domain'] = $result->fetch();
  69. $menu->getSelectedItem()->setText('domain -> '.$result->fetch());
  70. $menu->redraw();
  71. })
  72. ->addLineBreak('-')
  73. ->addMenuItem($checkboxSSL)
  74. ->addMenuItem($checkboxRedirect)
  75. ->addLineBreak('-')
  76. ->addItem('save', function() use (&$configuration) {
  77. var_dump($configuration);
  78. })
  79. ->addLineBreak('-');
  80. });
  81. }
  82. //exec('touch /etc/nginx/sites-available/');
  83. })
  84. ->addLineBreak('-');
  85. /***
  86. $itemCallable = function(CliMenu $menu) {
  87. // getting sites-available
  88. $sitesAvailable = scandir('/etc/nginx/sites-available');
  89. // getting sites-endabled
  90. $sitesEnabled = scandir('/etc/nginx/sites-enabled');
  91. foreach($sitesAvailable as $site) {
  92. if ($site !== '.' && $site !== '..') {
  93. // getting title
  94. $title = str_replace('.conf', '', $site);
  95. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($site, $title) {
  96. // check status
  97. if ($menu->getSelectedItem()->getChecked()) {
  98. symlink('/etc/nginx/sites-available/'.$site, '/etc/nginx/sites-enabled/'.$site);
  99. $status = 'enabled';
  100. } else {
  101. unlink('/etc/nginx/sites-enabled/'.$site);
  102. $status = 'disabled';
  103. }
  104. // starting
  105. exec('service nginx restart');
  106. exec('service nginx status', $output);
  107. if (strpos(implode(' ', $output), 'active') !== false) {
  108. $menu->confirm($title.' is '.$status.'!')->display('OK!');
  109. } else {
  110. $menu->confirm('Error! Something not working!')->display('OK!');
  111. }
  112. });
  113. // adding checkbox
  114. if (in_array($site, $sitesEnabled)) {
  115. $checkbox->setChecked(true);
  116. }
  117. $menuMain
  118. ->addSubmenu($title, function(CliMenuBuilder $builder) use ($site, $title, $checkbox) {
  119. $builder->setTitle("Nginx > $title")
  120. ->addItem('edit', function(CliMenu $menu) use ($site, $title) {
  121. system('nano /etc/nginx/sites-available/'.$site.' > `tty`');
  122. })
  123. ->addItem('delete', function(CliMenu $menu) use ($site) {
  124. if (file_exists('/etc/nginx/sites-enabled/'.$site)) {
  125. $menu->confirm('Error! Please disable '.$title.' first!')->display('OK!');
  126. } else {
  127. unlink('/etc/nginx/sites-available/'.$site);
  128. $menu->confirm("$site is deleted!")->display('OK!');
  129. }
  130. })
  131. ->addLineBreak('-')
  132. ->addMenuItem($checkbox)
  133. ->addLineBreak('-');
  134. });
  135. }
  136. }
  137. };*/
  138. $main->addLineBreak('-')->open();
  139. }
  140. }