From 809aa359894ad075646d41eb93008fef8efca877 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 6 May 2015 22:24:42 +0200 Subject: [PATCH] viewer --- app/bootstrap.php.d/20-twig.php | 8 +++ app/bootstrap.php.d/50-git.php | 11 ++-- app/config/routing.yml | 6 +- app/locales/en.yml | 7 ++- app/locales/fr.yml | 7 ++- src/Gist/Controller/ViewController.php | 35 +++++++++++- src/Gist/Form/CreateGistForm.php | 4 +- src/Gist/Resources/views/View/view.html.twig | 56 +++++++++--------- src/Gist/Service/GistService.php | 60 +++++++++++++++++--- 9 files changed, 146 insertions(+), 48 deletions(-) diff --git a/app/bootstrap.php.d/20-twig.php b/app/bootstrap.php.d/20-twig.php index 4f5a342..a0431a2 100644 --- a/app/bootstrap.php.d/20-twig.php +++ b/app/bootstrap.php.d/20-twig.php @@ -11,3 +11,11 @@ $app->extend('twig', function ($twig, $app) { return $twig; }); + +$app['geshi'] = function ($app) { + $geshi = new GeSHi(); + $geshi->enable_classes(); + $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); + + return $geshi; +}; diff --git a/app/bootstrap.php.d/50-git.php b/app/bootstrap.php.d/50-git.php index d9a6414..03d5c67 100644 --- a/app/bootstrap.php.d/50-git.php +++ b/app/bootstrap.php.d/50-git.php @@ -5,11 +5,14 @@ use Gist\Service\GistService; $app['gist_path'] = $app['root_path'].'/data/git'; -$app['git'] = function ($app) { - $wrapper = new GitWrapper('/usr/bin/git'); - return $wrapper->init($app['gist_path']); +$app['git_wrapper'] = function ($app) { + return new GitWrapper('/usr/bin/git'); +}; + +$app['git_working_copy'] = function ($app) { + return $app['git_wrapper']->init($app['gist_path']); }; $app['gist'] = function ($app) { - return new GistService($app['gist_path'], $app['git']); + return new GistService($app['gist_path'], $app['git_wrapper'], $app['git_working_copy'], $app['geshi']); }; diff --git a/app/config/routing.yml b/app/config/routing.yml index 86a4ae0..f74e139 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -1,7 +1,7 @@ home: path: / - defaults: { _controller: Gist\Controller\EditController::createAction, _locale: en } + defaults: {_controller: Gist\Controller\EditController::createAction, _locale: en} view: - path: /{gist} - defaults: { _controller: Gist\Controller\ViewController::viewAction, _locale: en } + path: /{gist}/{commit} + defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0} diff --git a/app/locales/en.yml b/app/locales/en.yml index d7c5abc..aa687a9 100644 --- a/app/locales/en.yml +++ b/app/locales/en.yml @@ -14,6 +14,9 @@ gist: title: 'Oops!' message: 'The gist was not found...' +date: + format: 'Y-m-d h:i:s' + form: error: not_blank: 'This value should not be blank bro!' @@ -30,7 +33,7 @@ form: choice: xml: 'HTML/XML' css: 'CSS' - js: 'JAVASCRIPT' + javascript: 'JAVASCRIPT' php: 'PHP' sql: 'SQL' yaml: 'YAML' @@ -39,5 +42,5 @@ form: asp: 'ASP' python: 'PYTHON' bash: 'BASH' - as: 'ACTION SCRIPT' + actionscript3: 'ACTION SCRIPT' text: 'TEXT' diff --git a/app/locales/fr.yml b/app/locales/fr.yml index 466014c..7fce02a 100644 --- a/app/locales/fr.yml +++ b/app/locales/fr.yml @@ -14,6 +14,9 @@ gist: title: 'Arf...' message: "Ce gist n'existe pas." +date: + format: 'd/m/Y H\hi s\s' + form: error: not_blank: 'Vous devez saisir cette donnée.' @@ -30,7 +33,7 @@ form: choice: xml: 'HTML/XML' css: 'CSS' - js: 'JAVASCRIPT' + javascript: 'JAVASCRIPT' php: 'PHP' sql: 'SQL' yaml: 'YAML' @@ -39,5 +42,5 @@ form: asp: 'ASP' python: 'PYTHON' bash: 'BASH' - as: 'ACTION SCRIPT' + actionscript3: 'ACTION SCRIPT' text: 'TEXTE' diff --git a/src/Gist/Controller/ViewController.php b/src/Gist/Controller/ViewController.php index be594ab..5732e9e 100644 --- a/src/Gist/Controller/ViewController.php +++ b/src/Gist/Controller/ViewController.php @@ -5,6 +5,7 @@ namespace Gist\Controller; use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Gist\Model\GistQuery; +use Gist\Model\Gist; /** * Class HomeController @@ -12,7 +13,7 @@ use Gist\Model\GistQuery; */ class ViewController { - public function viewAction(Request $request, Application $app, $gist) + public function viewAction(Request $request, Application $app, $gist, $commit) { $gist = GistQuery::create()->findOneByFile($gist); @@ -20,10 +21,21 @@ class ViewController return $this->notFoundResponse($app); } + $history = $app['gist']->getHistory($gist); + + if (empty($history)) { + return $this->notFoundResponse($app); + } + + $content = $this->getContentByCommit($app, $gist, $commit, $history); + return $app['twig']->render( 'View/view.html.twig', array( 'gist' => $gist, + 'type' => $gist->getType(), + 'history' => $history, + 'content' => $app['gist']->highlight($gist->getType(), $content), ) ); } @@ -32,4 +44,25 @@ class ViewController { 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); + } } diff --git a/src/Gist/Form/CreateGistForm.php b/src/Gist/Form/CreateGistForm.php index a115028..6d5b640 100644 --- a/src/Gist/Form/CreateGistForm.php +++ b/src/Gist/Form/CreateGistForm.php @@ -73,7 +73,7 @@ class CreateGistForm extends AbstractForm $types = array( 'xml' => '', 'css' => '', - 'js' => '', + 'javascript' => '', 'php' => '', 'sql' => '', 'yaml'=> '', @@ -82,7 +82,7 @@ class CreateGistForm extends AbstractForm 'asp' => '', 'python' => '', 'bash' => '', - 'as' => '', + 'actionscript3' => '', 'text' => '', ); diff --git a/src/Gist/Resources/views/View/view.html.twig b/src/Gist/Resources/views/View/view.html.twig index bfc9d5e..f345e84 100644 --- a/src/Gist/Resources/views/View/view.html.twig +++ b/src/Gist/Resources/views/View/view.html.twig @@ -1,5 +1,11 @@ {% extends 'base.html.twig' %} +{% block css %} + {{ parent() }} + + +{% endblock %} + {% block body %}
@@ -11,7 +17,7 @@
  • RĂ©visions - 4 + {{ history|length }}
  • @@ -53,31 +59,35 @@
    {% if gist.cipher %} -
    -                                        {{ 'foo' }}
    -                                    
    +
    {{ content }}
    + {% else %} + {{ content|raw }} {% endif %}
    {% if not gist.cipher %}
    -
    -

    - Commit 82110bbb53fbc5bf010ea4a50d999ab7cb22ae4f -
    - Date: Mon May 4 23:09:44 2015 +0200 -

    + {% for commit in history %} +
    +

    + Commit {{ commit.commit }} +
    + Date: {{ commit.date|date('date.format'|trans) }} +

    +

    + + Afficher + + + DIFF + +

    +
    -

    - - Afficher - - - DIFF - -

    -
    -
    + {% if not loop.last %} +
    + {% endif %} + {% endfor %}
    {% endif %}
    @@ -89,10 +99,4 @@ {% block js %} {{ parent() }} - - {% if gist is not null %} - - {% endif %} {% endblock %} diff --git a/src/Gist/Service/GistService.php b/src/Gist/Service/GistService.php index 6a8b327..5885eef 100644 --- a/src/Gist/Service/GistService.php +++ b/src/Gist/Service/GistService.php @@ -4,6 +4,9 @@ namespace Gist\Service; use Gist\Model\Gist; use GitWrapper\GitWorkingCopy; +use GitWrapper\GitWrapper; +use GitWrapper\GitCommand; +use GeSHi; /** * Class GistService @@ -13,31 +16,72 @@ class GistService { protected $gistPath; - protected $git; + protected $gitWrapper; - public function __construct($gistPath, GitWorkingCopy $git) + protected $gitWorkingCopy; + + protected $geshi; + + public function __construct($gistPath, GitWrapper $gitWrapper, GitWorkingCopy $gitWorkingCopy, GeSHi $geshi) { $this->gistPath = $gistPath; - $this->git = $git; + $this->gitWrapper = $gitWrapper; + $this->gitWorkingCopy = $gitWorkingCopy; + $this->geshi = $geshi; + } + + public function getHistory(Gist $gist) + { + $command = GitCommand::getInstance('log', '--format=medium', $gist->getFile()); + $command->setDirectory($this->gistPath); + $command->bypass(false); + $output = $this->gitWrapper->run($command); + + preg_match_all('/commit ([^\n]+)\n/isU', $output, $commits, PREG_SET_ORDER); + preg_match_all('/Date:\s+([^\n]+)\n/isU', $output, $dates, PREG_SET_ORDER); + + $history = []; + + for ($i = count($commits) - 1; $i >= 0; $i--) { + $history[] = array( + 'commit' => trim($commits[$i][1]), + 'date' => new \DateTime(trim($dates[$i][1])), + ); + } + + return $history; + } + + public function getContent(Gist $gist, $commit) + { + $command = GitCommand::getInstance('cat-file', '--textconv', $commit.':'.$gist->getFile()); + $command->setDirectory($this->gistPath); + $command->bypass(false); + + return $this->gitWrapper->run($command); } public function create(Gist $gist, array $data) { $gist->hydrateWith($data); $gist->generateFilename(); - $dir = getcwd(); - chdir($this->gistPath); file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']); - $this->git + $this->gitWorkingCopy ->add($gist->getFile()) ->commit('Init'); - chdir($dir); - $gist->save(); return $gist; } + + public function highlight($type, $content) + { + $this->geshi->set_source($content); + $this->geshi->set_language($type); + + return $this->geshi->parse_code(); + } }