gist/src/Gist/Controller/Controller.php

121 lines
2.5 KiB
PHP
Raw Normal View History

2015-05-07 13:49:31 +02:00
<?php
namespace Gist\Controller;
use Silex\Application;
use Gist\Model\Gist;
use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery;
use Symfony\Component\HttpFoundation\Response;
2015-05-07 13:49:31 +02:00
/**
* Class Controller
* @author Simon Vieille <simon@deblan.fr>
*/
class Controller
{
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function getApp()
{
return $this->app;
}
protected function notFoundResponse()
2015-05-07 13:49:31 +02:00
{
$app = $this->getApp();
return new Response(
$app['twig']->render(
'View/notFound.html.twig',
[]
),
404
);
2015-05-07 13:49:31 +02:00
}
protected function getViewOptions(Request $request, $gist, $commit)
2015-05-07 13:49:31 +02:00
{
$app = $this->getApp();
2015-05-07 13:49:31 +02:00
$gist = GistQuery::create()->findOneByFile($gist);
if (null === $gist) {
return null;
}
$history = $app['gist']->getHistory($gist);
if (empty($history)) {
return null;
}
$content = $this->getContentByCommit($gist, $commit, $history);
2015-05-07 13:49:31 +02:00
return array(
'gist' => $gist,
'type' => $gist->getType(),
'history' => $history,
'commit' => $commit,
'raw_content' => $content,
2015-05-09 12:30:33 +02:00
'content' => $app['gist']->highlight($gist->getGeshiType(), $content),
2015-05-07 13:49:31 +02:00
);
}
protected function getContentByCommit(Gist $gist, &$commit, $history)
2015-05-07 13:49:31 +02:00
{
$app = $this->getApp();
2015-05-07 13:49:31 +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);
}
public function getUser()
{
$app = $this->getApp();
$securityContext = $app['security'];
$securityToken = $securityContext->getToken();
if (!$securityToken) {
return null;
}
return $securityToken->getUser();
}
public function render($template, array $params)
{
$app = $this->getApp();
if (!isset($params['user'])) {
$params['user'] = $this->getUser();
}
return $app['twig']->render(
$template,
$params
);
}
2015-05-07 13:49:31 +02:00
}