From 88fb39fbabd46dc7d8e338dda6f8eecac4df4f02 Mon Sep 17 00:00:00 2001
From: Simon Vieille
Date: Thu, 7 May 2015 14:10:23 +0200
Subject: [PATCH] cloning
---
app/config/routing.yml | 4 ++
app/locales/en.yml | 3 +-
app/locales/fr.yml | 1 +
src/Gist/Controller/EditController.php | 42 +++++++++--
src/Gist/Form/CloneGistForm.php | 71 +++++++++++++++++++
src/Gist/Resources/views/Edit/clone.html.twig | 66 +++++++++++++++++
.../views/{Home => Edit}/index.html.twig | 2 +-
src/Gist/Resources/views/View/view.html.twig | 4 +-
src/Gist/Resources/views/error.html.twig | 40 +++++------
src/Gist/Service/GistService.php | 11 +++
10 files changed, 215 insertions(+), 29 deletions(-)
create mode 100644 src/Gist/Form/CloneGistForm.php
create mode 100644 src/Gist/Resources/views/Edit/clone.html.twig
rename src/Gist/Resources/views/{Home => Edit}/index.html.twig (99%)
diff --git a/app/config/routing.yml b/app/config/routing.yml
index 2da2256..b5cefa4 100644
--- a/app/config/routing.yml
+++ b/app/config/routing.yml
@@ -2,6 +2,10 @@ home:
path: /
defaults: {_controller: Gist\Controller\EditController::createAction, _locale: en}
+clone:
+ path: /clone/{gist}/{commit}
+ defaults: {_controller: Gist\Controller\EditController::cloneAction, _locale: en, commit: 0}
+
view:
path: /view/{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 0608499..da26832 100644
--- a/app/locales/en.yml
+++ b/app/locales/en.yml
@@ -18,7 +18,7 @@ gist:
history: 'Commit(s)'
raw: 'RAW'
download: 'Download'
- fork: 'Fork'
+ clone: 'Clone'
date:
format: 'Y-m-d h:i:s'
@@ -34,6 +34,7 @@ form:
choice:
yes: 'Yes'
no: 'No'
+ submit: 'Send'
type:
label: 'Language: %value%'
choice:
diff --git a/app/locales/fr.yml b/app/locales/fr.yml
index e8d67de..d51c1e7 100644
--- a/app/locales/fr.yml
+++ b/app/locales/fr.yml
@@ -34,6 +34,7 @@ form:
choice:
yes: 'Oui'
no: 'Non'
+ submit: 'Send'
type:
label: 'Langage : %value%'
choice:
diff --git a/src/Gist/Controller/EditController.php b/src/Gist/Controller/EditController.php
index 3406f38..e794292 100644
--- a/src/Gist/Controller/EditController.php
+++ b/src/Gist/Controller/EditController.php
@@ -5,7 +5,10 @@ namespace Gist\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Gist\Form\CreateGistForm;
+use Gist\Form\CloneGistForm;
use Gist\Model\Gist;
+use GitWrapper\GitException;
+use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class HomeController
@@ -32,7 +35,7 @@ class EditController extends Controller
}
return $app['twig']->render(
- 'Home/index.html.twig',
+ 'Edit/index.html.twig',
array(
'gist' => isset($gist) ? $gist : null,
'form' => $form->createView(),
@@ -44,10 +47,39 @@ class EditController extends Controller
{
$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);
+ $data = array(
+ 'type' => $viewOptions['gist']->getType(),
+ 'content' => $viewOptions['raw_content'],
+ 'cipher' => 'no',
+ );
+
+ $form = new CloneGistForm($app['form.factory'], $app['translator'], $data);
+ $form = $form->build();
+
+ if ($request->isMethod('post')) {
+ $form->submit($request);
+
+ if ($form->isValid()) {
+ try {
+ $gist = $app['gist']->commit($viewOptions['gist'], $form->getData());
+ } catch (GitException $e) {
+
+ }
+
+ $history = $app['gist']->getHistory($gist);
+
+ return new RedirectResponse($app['url_generator']->generate(
+ 'view',
+ array(
+ 'gist' => $gist->getFile(),
+ 'commit' => array_pop($history)['commit'],
+ )
+ ));
+ }
}
+
+ $viewOptions['form'] = $form->createView();
+
+ return $app['twig']->render('Edit/clone.html.twig', $viewOptions);
}
}
diff --git a/src/Gist/Form/CloneGistForm.php b/src/Gist/Form/CloneGistForm.php
new file mode 100644
index 0000000..dcaddce
--- /dev/null
+++ b/src/Gist/Form/CloneGistForm.php
@@ -0,0 +1,71 @@
+
+ */
+class CloneGistForm extends AbstractForm
+{
+ public function build(array $options = array())
+ {
+ $this->builder->add(
+ 'content',
+ 'textarea',
+ array(
+ 'required' => true,
+ 'attr' => array(
+ 'class' => 'form-control',
+ 'rows' => 10,
+ ),
+ 'constraints' => array(
+ new NotBlank(array(
+ 'message' => $this->translator->trans('form.error.not_blank'),
+ )),
+ ),
+ )
+ );
+
+ $this->builder->add(
+ 'type',
+ 'choice',
+ array(
+ 'required' => true,
+ 'choices' => $this->getTypes(),
+ 'constraints' => array(
+ new NotBlank(),
+ ),
+ )
+ );
+
+ return $this->builder->getForm();
+ }
+
+ protected function getTypes()
+ {
+ $types = array(
+ 'xml' => '',
+ 'css' => '',
+ 'javascript' => '',
+ 'php' => '',
+ 'sql' => '',
+ 'yaml'=> '',
+ 'perl' => '',
+ 'c' => '',
+ 'asp' => '',
+ 'python' => '',
+ 'bash' => '',
+ 'actionscript3' => '',
+ 'text' => '',
+ );
+
+ foreach ($types as $k => $v) {
+ $types[$k] = $this->translator->trans('form.type.choice.'.$k);
+ }
+
+ return $types;
+ }
+}
diff --git a/src/Gist/Resources/views/Edit/clone.html.twig b/src/Gist/Resources/views/Edit/clone.html.twig
new file mode 100644
index 0000000..85147a4
--- /dev/null
+++ b/src/Gist/Resources/views/Edit/clone.html.twig
@@ -0,0 +1,66 @@
+{% extends 'base.html.twig' %}
+
+{% block body %}
+
+{% endblock %}
diff --git a/src/Gist/Resources/views/Home/index.html.twig b/src/Gist/Resources/views/Edit/index.html.twig
similarity index 99%
rename from src/Gist/Resources/views/Home/index.html.twig
rename to src/Gist/Resources/views/Edit/index.html.twig
index c13204a..ecaa6c6 100644
--- a/src/Gist/Resources/views/Home/index.html.twig
+++ b/src/Gist/Resources/views/Edit/index.html.twig
@@ -74,7 +74,7 @@
{{ form_widget(form.content) }}
-
+
diff --git a/src/Gist/Resources/views/View/view.html.twig b/src/Gist/Resources/views/View/view.html.twig
index 4da8a7e..8c8e3b5 100644
--- a/src/Gist/Resources/views/View/view.html.twig
+++ b/src/Gist/Resources/views/View/view.html.twig
@@ -36,9 +36,9 @@
{{ 'gist.action.download'|trans }}
-
+
- {{ 'gist.action.fork'|trans }}
+ {{ 'gist.action.clone'|trans }}
{% endif %}
diff --git a/src/Gist/Resources/views/error.html.twig b/src/Gist/Resources/views/error.html.twig
index 907be1d..a83af92 100644
--- a/src/Gist/Resources/views/error.html.twig
+++ b/src/Gist/Resources/views/error.html.twig
@@ -1,7 +1,7 @@
{% extends 'base.html.twig' %}
{% block title %}
- Error {{ code }} - {{ name }}
+ Error {{ code }} - {{ name }}
{% endblock %}
{% block langs %}
@@ -9,24 +9,24 @@
{% endblock %}
{% block body %}
-
-
-
-
- Error: {{ name }}
-
-
- {% if app.env == 'dev' %}
-
In file "{{ exception.file }}" at line {{ exception.line }}
+
+
+
+
+ Error: {{ name }}
+
+
+ {% if app.env == 'dev' %}
+
In file "{{ exception.file }}" at line {{ exception.line }}
-
Stacktrace
-
{{ exception.message }}
-
{{ exception.traceAsString }}
- {% else %}
-
{{ exception.message }}
- {% endif %}
-
-
-
-
+
Stacktrace
+
{{ exception.message }}
+
{{ exception.traceAsString }}
+ {% else %}
+
{{ exception.message }}
+ {% endif %}
+
+
+
+
{% endblock %}
diff --git a/src/Gist/Service/GistService.php b/src/Gist/Service/GistService.php
index 220b18b..1027cfc 100644
--- a/src/Gist/Service/GistService.php
+++ b/src/Gist/Service/GistService.php
@@ -89,6 +89,17 @@ class GistService
return $gist;
}
+ public function commit(Gist $gist, array $data)
+ {
+ file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
+
+ $this->gitWorkingCopy
+ ->add($gist->getFile())
+ ->commit('Update');
+
+ return $gist;
+ }
+
public function highlight($type, $content)
{
$this->geshi->set_source($content);