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.

148 lines
4.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
  1. <?php
  2. namespace App\Factories;
  3. use PhpSchool\CliMenu\Builder\CliMenuBuilder;
  4. use PhpSchool\CliMenu\CliMenu;
  5. use PhpSchool\CliMenu\MenuItem\CheckboxItem;
  6. use PhpSchool\CliMenu\MenuItem\MenuMenuItem;
  7. class NginxVhostFactory
  8. {
  9. /**
  10. *
  11. * @param [type] $mainmenu
  12. * @param [type] $vhosts
  13. * @param [type] $vhostIndex
  14. */
  15. public function addVhosts($mainmenu, $vhosts, $vhostIndex)
  16. {
  17. // get items
  18. $items = $mainmenu->getItems();
  19. // new items
  20. $newItems = [];
  21. foreach($items as $index => $item) {
  22. if ($index <= $vhostIndex) {
  23. $newItems[] = $item;
  24. } else {
  25. break;
  26. }
  27. }
  28. // check for linebreak
  29. foreach($items as $index => $item) {
  30. if ($index > $vhostIndex && get_class($item) === 'PhpSchool\CliMenu\MenuItem\LineBreakItem') {
  31. $startIndex = $index;
  32. break;
  33. }
  34. }
  35. // add submenu for each vhost
  36. foreach($vhosts as $vhost) {
  37. $newItems[] = self::createVhostSubmenu($vhost, $mainmenu);
  38. }
  39. // fillup last items from mainmenu
  40. foreach($items as $index => $item) {
  41. if ($index >= $startIndex) {
  42. $newItems[] = $item;
  43. }
  44. }
  45. $mainmenu->setItems($newItems);
  46. return $mainmenu;
  47. }
  48. /**
  49. *
  50. * @param [type] $vhost
  51. * @param [type] $mainmenu
  52. * @return [type]
  53. */
  54. private function createVhostSubmenu($vhost, $mainmenu)
  55. {
  56. $builder = CliMenuBuilder::newSubMenu($mainmenu->getTerminal());
  57. // create checkbox for disable / enabled
  58. $checkbox = self::createVhostCheckbox($vhost);
  59. // adding checkbox
  60. if ($vhost['enabled']) {
  61. $checkbox->setChecked(true);
  62. }
  63. $builder
  64. ->setTitle('Nginx > '.$vhost['domain'])
  65. ->setGoBackButtonText('Back')
  66. ->addItem('edit', function(CliMenu $menu) use ($vhost) {
  67. system('nano /etc/nginx/sites-available/'.$vhost['file'].' > `tty`');
  68. })
  69. ->addItem('delete', function(CliMenu $menu) use ($vhost) {
  70. if (file_exists('/etc/nginx/sites-enabled/'.$vhost['file'])) {
  71. $menu->confirm('Error! Please disable '.$vhost['domain'].' first!')->display('OK!');
  72. } else {
  73. unlink('/etc/nginx/sites-available/'.$vhost['file']);
  74. $menu->confirm($vhost['domain'].' is deleted!')->display('OK!');
  75. // get
  76. $parent = $menu->getParent();
  77. $menu->closeThis();
  78. // remove current vhost from mainmenu
  79. $parent->removeItem($parent->getSelectedItem());
  80. $parent->open();
  81. }
  82. })
  83. ->addLineBreak('-')
  84. ->addMenuItem($checkbox)
  85. ->addLineBreak('-');
  86. $submenu = $builder->build();
  87. $submenu->setParent($mainmenu);
  88. $submenu->setStyle($mainmenu->getStyle());
  89. $item = new MenuMenuItem(
  90. $vhost['domain'],
  91. $submenu,
  92. $builder->isMenuDisabled()
  93. );
  94. return $item;
  95. }
  96. /**
  97. *
  98. * @param [type] $vhost
  99. * @return [type]
  100. */
  101. private function createVhostCheckbox($vhost)
  102. {
  103. // create checkbox for enabled / disabled
  104. $checkbox = new CheckboxItem('enabled', function(CliMenu $menu) use ($vhost) {
  105. // check status
  106. if ($menu->getSelectedItem()->getChecked()) {
  107. symlink('/etc/nginx/sites-available/'.$vhost['file'], '/etc/nginx/sites-enabled/'.$vhost['file']);
  108. $status = 'enabled';
  109. } else {
  110. unlink('/etc/nginx/sites-enabled/'.$vhost['file']);
  111. $status = 'disabled';
  112. }
  113. // restart
  114. exec('service nginx restart');
  115. exec('service nginx status', $output);
  116. if (strpos(implode(' ', $output), 'active') !== false) {
  117. $menu->confirm($vhost['domain'].' is '.$status.'!')->display('OK!');
  118. } else {
  119. $menu->confirm('Error! Something not working!')->display('OK!');
  120. }
  121. });
  122. return $checkbox;
  123. }
  124. }