From 843cf43b544af236e7be7118e2003f49ac543901 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 5 May 2015 22:04:04 +0200 Subject: [PATCH] git + form --- app/bootstrap.php.d/00-main.php | 1 + app/bootstrap.php.d/10-config.php | 4 +- app/bootstrap.php.d/20-error.php | 1 + app/bootstrap.php.d/20-routing.php | 1 + app/bootstrap.php.d/20-twig.php | 1 + app/bootstrap.php.d/40-form.php | 7 ++ app/bootstrap.php.d/50-git.php | 9 ++ app/deploy | 4 +- app/locales/en.yml | 23 +++++ composer.json | 8 +- src/Gist/Controller/HomeController.php | 13 ++- src/Gist/Form/AbstractForm.php | 26 ++++++ src/Gist/Form/CreateGistForm.php | 93 +++++++++++++++++++ src/Gist/Resources/views/Home/index.html.twig | 90 ++++++++++++++++++ src/Gist/Resources/views/base.html.twig | 87 +---------------- src/Gist/Resources/views/error.html.twig | 24 +++-- web/.htaccess | 49 ++++++++++ 17 files changed, 342 insertions(+), 99 deletions(-) create mode 100644 app/bootstrap.php.d/40-form.php create mode 100644 app/bootstrap.php.d/50-git.php create mode 100644 src/Gist/Form/AbstractForm.php create mode 100644 src/Gist/Form/CreateGistForm.php create mode 100644 web/.htaccess diff --git a/app/bootstrap.php.d/00-main.php b/app/bootstrap.php.d/00-main.php index 86d5573..3a731bf 100644 --- a/app/bootstrap.php.d/00-main.php +++ b/app/bootstrap.php.d/00-main.php @@ -5,3 +5,4 @@ use Gist\Application; $app = Application::getInstance(); $app['root_path'] = __DIR__ . '/../..'; + diff --git a/app/bootstrap.php.d/10-config.php b/app/bootstrap.php.d/10-config.php index f090b64..2fa307e 100644 --- a/app/bootstrap.php.d/10-config.php +++ b/app/bootstrap.php.d/10-config.php @@ -2,8 +2,10 @@ use Symfony\Component\Config\FileLocator; -$app['config.locator.path'] = $app['root_path'].'/app/config'; +$app['config.locator.path'] = $app['root_path'].'/app/config/'; $app['config.locator'] = function ($app) { return new FileLocator($app['config.locator.path']); }; + +$app['env'] = 'dev'; diff --git a/app/bootstrap.php.d/20-error.php b/app/bootstrap.php.d/20-error.php index 02de3a9..e32b2a0 100644 --- a/app/bootstrap.php.d/20-error.php +++ b/app/bootstrap.php.d/20-error.php @@ -10,3 +10,4 @@ $app->error(function (Exception $e, $code) use ($app) { ) ); }); + diff --git a/app/bootstrap.php.d/20-routing.php b/app/bootstrap.php.d/20-routing.php index dfeb2fe..e422277 100644 --- a/app/bootstrap.php.d/20-routing.php +++ b/app/bootstrap.php.d/20-routing.php @@ -12,3 +12,4 @@ $app['routes'] = $app->extend('routes', function ($routes, $app) { $routes->addCollection($app['routing.loader']->load($app['routing.file'])); return $routes; }); + diff --git a/app/bootstrap.php.d/20-twig.php b/app/bootstrap.php.d/20-twig.php index df3ce3f..3c1c7c1 100644 --- a/app/bootstrap.php.d/20-twig.php +++ b/app/bootstrap.php.d/20-twig.php @@ -11,3 +11,4 @@ $app->extend('twig', function ($twig, $app) { return $twig; }); + diff --git a/app/bootstrap.php.d/40-form.php b/app/bootstrap.php.d/40-form.php new file mode 100644 index 0000000..0abdab6 --- /dev/null +++ b/app/bootstrap.php.d/40-form.php @@ -0,0 +1,7 @@ +register(new FormServiceProvider()); +$app->register(new ValidatorServiceProvider()); diff --git a/app/bootstrap.php.d/50-git.php b/app/bootstrap.php.d/50-git.php new file mode 100644 index 0000000..3d7cf11 --- /dev/null +++ b/app/bootstrap.php.d/50-git.php @@ -0,0 +1,9 @@ + */ class HomeController { public function indexAction(Request $request, Application $app) { - return $app['twig']->render('Home/index.html.twig'); + $form = new CreateGistForm($app['form.factory'], $app['translator']); + $form = $form->build(); + + return $app['twig']->render( + 'Home/index.html.twig', + array( + 'form' => $form->createView(), + ) + ); } } diff --git a/src/Gist/Form/AbstractForm.php b/src/Gist/Form/AbstractForm.php new file mode 100644 index 0000000..220d7d4 --- /dev/null +++ b/src/Gist/Form/AbstractForm.php @@ -0,0 +1,26 @@ + + */ +abstract class AbstractForm +{ + protected $builder; + + protected $translator; + + public function __construct(FormFactory $formFactory, Translator $translator) + { + $this->translator = $translator; + + $this->builder = $formFactory->createBuilder('form'); + } + + abstract public function build(array $options = array()); +} diff --git a/src/Gist/Form/CreateGistForm.php b/src/Gist/Form/CreateGistForm.php new file mode 100644 index 0000000..f2cd917 --- /dev/null +++ b/src/Gist/Form/CreateGistForm.php @@ -0,0 +1,93 @@ + + */ +class CreateGistForm extends AbstractForm +{ + public function build(array $options = array()) + { + $this->builder->add( + 'title', + 'text', + array( + 'required' => false, + 'attr' => array( + 'class' => 'form-control', + 'placeholder' => $this->translator->trans('form.title.placeholder'), + ), + ) + ); + + $this->builder->add( + 'content', + 'textarea', + array( + 'required' => true, + 'attr' => array( + 'class' => 'form-control', + 'rows' => 10, + ), + 'constraints' => array( + new NotBlank(), + ), + ) + ); + + $this->builder->add( + 'type', + 'choice', + array( + 'required' => true, + 'choices' => $this->getTypes(), + 'constraints' => array( + new NotBlank(), + ), + ) + ); + + $this->builder->add( + 'cipher', + 'choice', + array( + 'required' => true, + 'choices' => array( + 0 => $this->translator->trans('form.cipher.choice.no'), + 1 => $this->translator->trans('form.cipher.choice.yes'), + ), + ) + ); + + return $this->builder->getForm(); + } + + protected function getTypes() + { + $types = array( + 'xml' => '', + 'css' => '', + 'js' => '', + 'php' => '', + 'sql' => '', + 'yaml'=> '', + 'perl' => '', + 'c' => '', + 'asp' => '', + 'python' => '', + 'bash' => '', + 'as' => '', + 'text' => '', + ); + + foreach ($types as $k => $v) { + $types[$k] = $this->translator->trans('form.type.choice.'.$k); + } + + return $types; + } +} diff --git a/src/Gist/Resources/views/Home/index.html.twig b/src/Gist/Resources/views/Home/index.html.twig index 1503ec7..2cc91ca 100644 --- a/src/Gist/Resources/views/Home/index.html.twig +++ b/src/Gist/Resources/views/Home/index.html.twig @@ -1 +1,91 @@ {% extends 'base.html.twig' %} + +{% block body %} +
+ {{ form(form) }} +
+
+
+ +
+
+

+ + En activant le chiffrement, le code déposé ne pourra pas être forké. +

+
+
+
+ + +
+
+ + +
+
+
+

+ +

+

+ +

+
+
+
+
+{% endblock %} diff --git a/src/Gist/Resources/views/base.html.twig b/src/Gist/Resources/views/base.html.twig index 94f7b40..0c42131 100644 --- a/src/Gist/Resources/views/base.html.twig +++ b/src/Gist/Resources/views/base.html.twig @@ -42,92 +42,7 @@
-
-
-
-
- -
-
-

- - En activant le chiffrement, le code déposé ne pourra pas être forké. -

-
-
-
- - -
-
- - -
-
-
-

- -

-

- -

-
-
-
-
+ {% block body %}{% endblock %} {% block js %} diff --git a/src/Gist/Resources/views/error.html.twig b/src/Gist/Resources/views/error.html.twig index baba8d9..49dc2ae 100644 --- a/src/Gist/Resources/views/error.html.twig +++ b/src/Gist/Resources/views/error.html.twig @@ -5,11 +5,23 @@ {% endblock %} {% block body %} -

{{ name }}

+
+
+
+
+ Error: {{ name }} +
+
+ {% if app.env == 'dev' %} +

In file "{{ exception.file }}" at line {{ exception.line }}

-

In file "{{ exception.file }}" at line {{ exception.line }}

-

{{ exception.message }}

- -

Stacktrace

-
{{ exception.traceAsString }}
+

Stacktrace

+
{{ exception.traceAsString }}
+ {% else %} +

{{ exception.message }}

+ {% endif %} +
+
+
+
{% endblock %} diff --git a/web/.htaccess b/web/.htaccess new file mode 100644 index 0000000..67eb850 --- /dev/null +++ b/web/.htaccess @@ -0,0 +1,49 @@ + + RewriteEngine On + + RewriteCond %{HTTPS} !on + RewriteCond %{SERVER_NAME} =gist.deblan.org + RewriteRule .* https://gist.deblan.org%{REQUEST_URI} [R,L] + + # Determine the RewriteBase automatically and set it as environment variable. + # If you are using Apache aliases to do mass virtual hosting or installed the + # project in a subdirectory, the base path will be prepended to allow proper + # resolution of the app.php file and to redirect to the correct URI. It will + # work in environments without path prefix as well, providing a safe, one-size + # fits all solution. But as you do not need it in this case, you can comment + # the following 2 lines to eliminate the overhead. + RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ + RewriteRule ^(.*) - [E=BASE:%1] + + # Redirect to URI without front controller to prevent duplicate content + # (with and without `/app.php`). Only do this redirect on the initial + # rewrite by Apache and not on subsequent cycles. Otherwise we would get an + # endless redirect loop (request -> rewrite to front controller -> + # redirect -> request -> ...). + # So in case you get a "too many redirects" error or you always get redirected + # to the start page because your Apache does not expose the REDIRECT_STATUS + # environment variable, you have 2 choices: + # - disable this feature by commenting the following 2 lines or + # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the + # following RewriteCond (best solution) + RewriteCond %{ENV:REDIRECT_STATUS} ^$ + RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] + + # If the requested filename exists, simply serve it. + # We only want to let Apache serve files and not directories. + RewriteCond %{REQUEST_FILENAME} -f + RewriteRule .? - [L] + + # Rewrite all other queries to the front controller. + RewriteRule .? %{ENV:BASE}/index.php [L] + + + + + # When mod_rewrite is not available, we instruct a temporary redirect of + # the start page to the front controller explicitly so that the website + # and the generated links can still be used. + RedirectMatch 302 ^/$ /index.php/ + # RedirectTemp cannot be used instead + +