gist/src/Gist/Controller/ViewController.php

142 lines
3.8 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
/**
* Class HomeController
* @author Simon Vieille <simon@deblan.fr>
*/
class ViewController
{
2015-05-07 13:38:24 +02:00
protected function getViewOptions(Request $request, Application $app, $gist, $commit)
2015-05-06 20:35:30 +02:00
{
$gist = GistQuery::create()->findOneByFile($gist);
if (null === $gist) {
2015-05-07 13:38:24 +02:00
return null;
2015-05-06 20:35:30 +02:00
}
2015-05-06 22:24:42 +02:00
$history = $app['gist']->getHistory($gist);
2015-05-06 22:26:04 +02:00
if (empty($history)) {
2015-05-07 13:38:24 +02:00
return null;
2015-05-06 22:24:42 +02:00
}
$content = $this->getContentByCommit($app, $gist, $commit, $history);
2015-05-07 13:38:24 +02:00
return array(
'gist' => $gist,
'type' => $gist->getType(),
'history' => $history,
'commit' => $commit,
'raw_content' => $content,
'content' => $app['gist']->highlight($gist->getType(), $content),
2015-05-06 20:35:30 +02:00
);
}
2015-05-07 13:38:24 +02:00
public function viewAction(Request $request, Application $app, $gist, $commit)
{
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
if (is_array($viewOptions)) {
return $app['twig']->render('View/view.html.twig', $viewOptions);
} else {
return $this->notFoundResponse($app);
}
}
public function rawAction(Request $request, Application $app, $gist, $commit)
{
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
if (is_array($viewOptions)) {
return new Response(
$viewOptions['raw_content'],
200,
array(
'Content-Type' => 'text/plain',
)
);
} else {
return $this->notFoundResponse($app);
}
}
public function downloadAction(Request $request, Application $app, $gist, $commit)
{
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
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()),
'Content-Length' => filesize($file),
'Content-Type' => 'application/force-download',
)
);
} else {
return $this->notFoundResponse($app);
}
}
2015-05-07 00:51:52 +02:00
public function revisionsAction(Request $request, Application $app, $gist)
{
$gist = GistQuery::create()->findOneByFile($gist);
if (null === $gist) {
return $this->notFoundResponse($app);
}
$history = $app['gist']->getHistory($gist);
if (empty($history)) {
return $this->notFoundResponse($app);
}
return $app['twig']->render(
'View/revisions.html.twig',
array(
'gist' => $gist,
'history' => $history,
)
);
}
2015-05-06 20:35:30 +02:00
protected function notFoundResponse(Application $app)
{
2015-05-06 20:39:51 +02:00
return $app['twig']->render('View/notFound.html.twig');
2015-05-06 20:35:30 +02:00
}
2015-05-06 22:24:42 +02:00
2015-05-07 00:51:52 +02:00
protected function getContentByCommit(Application $app, Gist $gist, &$commit, $history)
2015-05-06 22:24:42 +02:00
{
if ($commit === 0) {
$commit = $history[0]['commit'];
} else {
$commitExists = false;
foreach ($history as $ci) {
if ($commit === $ci['commit']) {
$commitExists = true;
}
}
if (!$commitExists) {
return null;
}
}
return $app['gist']->getContent($gist, $commit);
}
2015-05-06 20:35:30 +02:00
}