<?php
namespace App\Controller;
use App\Entity\Playlist;
use App\Lib\AzuraCast;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="homepage")
* @param UrlGeneratorInterface $urlGenerator
* @param AzuraCast $azuraCast
* @return RedirectResponse|Response
* @Security("is_granted('ROLE_USER')")
* @throws Exception
*/
public function index(UrlGeneratorInterface $urlGenerator, AzuraCast $azuraCast, EntityManagerInterface $em)
{
$playlistRepository = $em->getRepository(Playlist::class);
$playlists = $playlistRepository->findAll();
return $this->render('index.html.twig', [
'stations' => $azuraCast->allStationsNowPlaying(),
'playlists' => $playlists
]);
}
/**
*
* @Route("/command/cache/clear", name="command_cache_clear")
*/
public function command_cache_clear(KernelInterface $kernel)
{
return $this->json([
'git-commit-id'=>trim($this->run_bash($kernel, 'cat /code/.git/ORIG_HEAD')),
'cache-clear'=>$this->do_command($kernel, 'cache:clear')
]);
}
private function run_bash(KernelInterface $kernel, string $command) : string
{
$process = new Process($command);
$process->setWorkingDirectory($kernel->getProjectDir());
$process->run();
if ($process->isSuccessful()){
return $process->getOutput();
} else {
throw new ProcessFailedException($process);
}
}
private function do_command(KernelInterface $kernel, string $command) : string
{
$env = $kernel->getEnvironment();
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => $command,
'--env' => $env
));
$output = new BufferedOutput();
$application->run($input, $output);
$content = $output->fetch();
return $content;
}
/**
* @Route("/auto-deploy-on", name="autodeploy")
* @param Request $request
* @return JsonResponse
* @throws IOException
*/
public function autoDeploy(Request $request){
$gitlab_token = getenv("GITLAB_TOKEN");
if (empty($gitlab_token)) $gitlab_token = "r3m3wQw8eWB885";
$passed_token = $request->query->get('token', $request->headers->get('X-Gitlab-Token'));
if ($passed_token !== $gitlab_token){
return $this->json(['success'=>false, 'message'=>'Invalid Token'], Response::HTTP_FORBIDDEN);
}
$fs = new Filesystem();
$fs->touch('/code/var/update-requested');
return $this->json(['success'=>true]);
}
/**
* @Route("/git-commit-id", name="git-commit-id")
* @return JsonResponse
*/
public function gitCommitId(){
return $this->json(['git-commit'=>trim(file_get_contents('/code/.git/ORIG_HEAD'))]);
}
}