gist/src/Gist/Controller/ViewController.php

176 lines
4.4 KiB
PHP
Raw Normal View History

2015-05-06 20:35:30 +02:00
<?php
namespace Gist\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery;
2015-05-06 22:24:42 +02:00
use Gist\Model\Gist;
2015-05-07 13:38:24 +02:00
use Symfony\Component\HttpFoundation\Response;
2015-05-06 20:35:30 +02:00
/**
2016-11-13 00:44:23 +01:00
* Class ViewController.
*
2015-05-06 20:35:30 +02:00
* @author Simon Vieille <simon@deblan.fr>
*/
2015-05-07 13:49:31 +02:00
class ViewController extends Controller
2015-05-06 20:35:30 +02:00
{
2016-11-13 00:44:23 +01:00
/**
* View action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function viewAction(Request $request, $gist, $commit)
2015-05-07 13:38:24 +02:00
{
$app = $this->getApp();
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-07 13:38:24 +02:00
if (is_array($viewOptions)) {
2019-03-13 11:36:19 +01:00
$viewOptions['no_cache'] = true;
2018-08-21 09:56:51 +02:00
return $this->createResponse('View/view.html.twig', $viewOptions);
2015-05-07 13:38:24 +02:00
} else {
return $this->notFoundResponse();
2015-05-07 13:38:24 +02:00
}
}
2016-11-13 00:44:23 +01:00
/**
* Embed action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function embedAction(Request $request, $gist, $commit)
2015-05-09 17:42:33 +02:00
{
$app = $this->getApp();
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-09 17:42:33 +02:00
if (is_array($viewOptions)) {
2018-08-21 10:13:38 +02:00
return $this->createResponse('View/embed.html.twig', $viewOptions);
2015-05-09 17:42:33 +02:00
} else {
return $this->notFoundResponse();
2015-05-09 17:42:33 +02:00
}
}
2016-11-13 00:44:23 +01:00
/**
* JS embed action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function embedJsAction(Request $request, $gist, $commit)
2015-05-09 17:42:33 +02:00
{
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-09 17:42:33 +02:00
$response = $this->createResponse('View/embedJs.html.twig', $viewOptions);
$response->headers->set('Content-Type', 'text/javascript');
return $response;
2015-05-09 17:42:33 +02:00
}
2016-11-13 00:44:23 +01:00
/**
* Raw action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function rawAction(Request $request, $gist, $commit)
2015-05-07 13:38:24 +02:00
{
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-07 13:38:24 +02:00
if (is_array($viewOptions)) {
return new Response(
$viewOptions['raw_content'],
200,
array(
'Content-Type' => 'text/plain',
)
);
} else {
return $this->notFoundResponse();
2015-05-07 13:38:24 +02:00
}
}
2016-11-13 00:44:23 +01:00
/**
* Download action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function downloadAction(Request $request, $gist, $commit)
2015-05-07 13:38:24 +02:00
{
$app = $this->getApp();
$viewOptions = $this->getViewOptions($request, $gist, $commit);
2015-05-07 13:38:24 +02:00
if (is_array($viewOptions)) {
$gist = $viewOptions['gist'];
$file = $app['gist_path'].'/'.$gist->getFile();
return new Response(
$viewOptions['raw_content'],
200,
array(
'Content-Disposition' => sprintf('filename=%s.%s', $gist->getFile(), $gist->getTypeAsExtension()),
2017-10-16 13:32:01 +02:00
'Content-Length' => mb_strlen($viewOptions['raw_content']),
2015-05-07 13:38:24 +02:00
'Content-Type' => 'application/force-download',
2018-08-20 17:05:36 +02:00
),
false
2015-05-07 13:38:24 +02:00
);
} else {
return $this->notFoundResponse($app);
}
}
2016-11-13 00:44:23 +01:00
/**
* Revisions action.
*
* @param Request $request
* @param string $gist Gist's ID
*
2018-08-20 17:05:36 +02:00
* @return Response
2016-11-13 00:44:23 +01:00
*/
public function revisionsAction(Request $request, $gist)
2015-05-07 00:51:52 +02:00
{
$app = $this->getApp();
2015-05-07 00:51:52 +02:00
$gist = GistQuery::create()->findOneByFile($gist);
if (null === $gist) {
return $this->notFoundResponse();
2015-05-07 00:51:52 +02:00
}
$history = $app['gist']->getHistory($gist);
if (empty($history)) {
return $this->notFoundResponse();
2015-05-07 00:51:52 +02:00
}
2018-08-21 09:56:51 +02:00
return $this->createResponse(
2015-05-07 00:51:52 +02:00
'View/revisions.html.twig',
array(
'gist' => $gist,
'history' => $history,
)
2015-05-07 00:51:52 +02:00
);
}
2015-05-06 20:35:30 +02:00
}