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.

149 lines
4.3 KiB

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