src/Controller/DefaultController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Playlist;
  4. use App\Lib\AzuraCast;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Exception;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
  10. use Symfony\Component\Filesystem\Exception\IOException;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Bundle\FrameworkBundle\Console\Application;
  19. use Symfony\Component\Console\Input\ArrayInput;
  20. use Symfony\Component\Console\Output\BufferedOutput;
  21. use Symfony\Component\HttpKernel\KernelInterface;
  22. use Symfony\Component\Process\Exception\ProcessFailedException;
  23. use Symfony\Component\Process\Process;
  24. class DefaultController extends AbstractController
  25. {
  26. /**
  27. * @Route("/", name="homepage")
  28. * @param UrlGeneratorInterface $urlGenerator
  29. * @param AzuraCast $azuraCast
  30. * @return RedirectResponse|Response
  31. * @Security("is_granted('ROLE_USER')")
  32. * @throws Exception
  33. */
  34. public function index(UrlGeneratorInterface $urlGenerator, AzuraCast $azuraCast, EntityManagerInterface $em)
  35. {
  36. $playlistRepository = $em->getRepository(Playlist::class);
  37. $playlists = $playlistRepository->findAll();
  38. return $this->render('index.html.twig', [
  39. 'stations' => $azuraCast->allStationsNowPlaying(),
  40. 'playlists' => $playlists
  41. ]);
  42. }
  43. /**
  44. *
  45. * @Route("/command/cache/clear", name="command_cache_clear")
  46. */
  47. public function command_cache_clear(KernelInterface $kernel)
  48. {
  49. return $this->json([
  50. 'git-commit-id'=>trim($this->run_bash($kernel, 'cat /code/.git/ORIG_HEAD')),
  51. 'cache-clear'=>$this->do_command($kernel, 'cache:clear')
  52. ]);
  53. }
  54. private function run_bash(KernelInterface $kernel, string $command) : string
  55. {
  56. $process = new Process($command);
  57. $process->setWorkingDirectory($kernel->getProjectDir());
  58. $process->run();
  59. if ($process->isSuccessful()){
  60. return $process->getOutput();
  61. } else {
  62. throw new ProcessFailedException($process);
  63. }
  64. }
  65. private function do_command(KernelInterface $kernel, string $command) : string
  66. {
  67. $env = $kernel->getEnvironment();
  68. $application = new Application($kernel);
  69. $application->setAutoExit(false);
  70. $input = new ArrayInput(array(
  71. 'command' => $command,
  72. '--env' => $env
  73. ));
  74. $output = new BufferedOutput();
  75. $application->run($input, $output);
  76. $content = $output->fetch();
  77. return $content;
  78. }
  79. /**
  80. * @Route("/auto-deploy-on", name="autodeploy")
  81. * @param Request $request
  82. * @return JsonResponse
  83. * @throws IOException
  84. */
  85. public function autoDeploy(Request $request){
  86. $gitlab_token = getenv("GITLAB_TOKEN");
  87. if (empty($gitlab_token)) $gitlab_token = "r3m3wQw8eWB885";
  88. $passed_token = $request->query->get('token', $request->headers->get('X-Gitlab-Token'));
  89. if ($passed_token !== $gitlab_token){
  90. return $this->json(['success'=>false, 'message'=>'Invalid Token'], Response::HTTP_FORBIDDEN);
  91. }
  92. $fs = new Filesystem();
  93. $fs->touch('/code/var/update-requested');
  94. return $this->json(['success'=>true]);
  95. }
  96. /**
  97. * @Route("/git-commit-id", name="git-commit-id")
  98. * @return JsonResponse
  99. */
  100. public function gitCommitId(){
  101. return $this->json(['git-commit'=>trim(file_get_contents('/code/.git/ORIG_HEAD'))]);
  102. }
  103. }