gist/src/Gist/Controller/ViewController.php

122 lines
3.2 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
/**
2015-07-19 16:45:04 +02: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
{
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)) {
return $this->render('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
}
}
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)) {
return $app['twig']->render('View/embed.html.twig', $viewOptions);
} else {
return $this->notFoundResponse();
2015-05-09 17:42:33 +02: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
return new Response(
$this->render('View/embedJs.html.twig', $viewOptions),
2015-05-09 17:42:33 +02:00
200,
array(
'Content-Type' => 'text/javascript',
)
);
}
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
}
}
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()),
'Content-Length' => filesize($file),
'Content-Type' => 'application/force-download',
)
);
} else {
return $this->notFoundResponse($app);
}
}
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
}
return $this->render(
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
}