refactoring
This commit is contained in:
parent
5667ec6066
commit
83244f6b47
67
src/Gist/Controller/Controller.php
Normal file
67
src/Gist/Controller/Controller.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Gist\Controller;
|
||||
|
||||
use Silex\Application;
|
||||
use Gist\Model\Gist;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Gist\Model\GistQuery;
|
||||
|
||||
/**
|
||||
* Class Controller
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Controller
|
||||
{
|
||||
protected function notFoundResponse(Application $app)
|
||||
{
|
||||
return $app['twig']->render('View/notFound.html.twig');
|
||||
}
|
||||
|
||||
protected function getViewOptions(Request $request, Application $app, $gist, $commit)
|
||||
{
|
||||
$gist = GistQuery::create()->findOneByFile($gist);
|
||||
|
||||
if (null === $gist) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$history = $app['gist']->getHistory($gist);
|
||||
|
||||
if (empty($history)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$content = $this->getContentByCommit($app, $gist, $commit, $history);
|
||||
|
||||
return array(
|
||||
'gist' => $gist,
|
||||
'type' => $gist->getType(),
|
||||
'history' => $history,
|
||||
'commit' => $commit,
|
||||
'raw_content' => $content,
|
||||
'content' => $app['gist']->highlight($gist->getType(), $content),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getContentByCommit(Application $app, Gist $gist, &$commit, $history)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ use Gist\Model\Gist;
|
|||
* Class HomeController
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class EditController
|
||||
class EditController extends Controller
|
||||
{
|
||||
public function createAction(Request $request, Application $app)
|
||||
{
|
||||
|
@ -39,4 +39,15 @@ class EditController
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function cloneAction(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,34 +12,8 @@ use Symfony\Component\HttpFoundation\Response;
|
|||
* Class HomeController
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class ViewController
|
||||
class ViewController extends Controller
|
||||
{
|
||||
protected function getViewOptions(Request $request, Application $app, $gist, $commit)
|
||||
{
|
||||
$gist = GistQuery::create()->findOneByFile($gist);
|
||||
|
||||
if (null === $gist) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$history = $app['gist']->getHistory($gist);
|
||||
|
||||
if (empty($history)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$content = $this->getContentByCommit($app, $gist, $commit, $history);
|
||||
|
||||
return array(
|
||||
'gist' => $gist,
|
||||
'type' => $gist->getType(),
|
||||
'history' => $history,
|
||||
'commit' => $commit,
|
||||
'raw_content' => $content,
|
||||
'content' => $app['gist']->highlight($gist->getType(), $content),
|
||||
);
|
||||
}
|
||||
|
||||
public function viewAction(Request $request, Application $app, $gist, $commit)
|
||||
{
|
||||
$viewOptions = $this->getViewOptions($request, $app, $gist, $commit);
|
||||
|
@ -112,30 +86,4 @@ class ViewController
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function notFoundResponse(Application $app)
|
||||
{
|
||||
return $app['twig']->render('View/notFound.html.twig');
|
||||
}
|
||||
|
||||
protected function getContentByCommit(Application $app, Gist $gist, &$commit, $history)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue