diff --git a/src/Gist/Controller/Controller.php b/src/Gist/Controller/Controller.php new file mode 100644 index 0000000..101c1ae --- /dev/null +++ b/src/Gist/Controller/Controller.php @@ -0,0 +1,67 @@ + + */ +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); + } +} diff --git a/src/Gist/Controller/EditController.php b/src/Gist/Controller/EditController.php index 9b84679..7ebba3e 100644 --- a/src/Gist/Controller/EditController.php +++ b/src/Gist/Controller/EditController.php @@ -11,7 +11,7 @@ use Gist\Model\Gist; * Class HomeController * @author Simon Vieille */ -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); + } + } } diff --git a/src/Gist/Controller/ViewController.php b/src/Gist/Controller/ViewController.php index a4aded3..a772eb8 100644 --- a/src/Gist/Controller/ViewController.php +++ b/src/Gist/Controller/ViewController.php @@ -12,34 +12,8 @@ use Symfony\Component\HttpFoundation\Response; * Class HomeController * @author Simon Vieille */ -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); - } }