From eea3b1e427bd004050e82f048179c31444324b50 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 5 May 2015 20:33:05 +0200 Subject: [PATCH] silex --- app/bootstrap.php | 36 +++++ app/bootstrap.php.d/00-main.php | 7 + app/bootstrap.php.d/10-config.php | 9 ++ app/bootstrap.php.d/20-error.php | 12 ++ app/bootstrap.php.d/20-routing.php | 14 ++ app/bootstrap.php.d/20-twig.php | 13 ++ app/bootstrap.php.d/30-trans.php | 58 ++++++++ app/config/routing.yml | 3 + app/console | 6 + app/deploy | 14 ++ app/locales/en.yml | 8 + app/locales/fr.yml | 0 composer.json | 12 +- src/Gist/Application.php | 22 +++ src/Gist/Controller/HomeController.php | 18 +++ src/Gist/Resources/views/Home/index.html.twig | 1 + src/Gist/Resources/views/base.html.twig | 139 ++++++++++++++++++ src/Gist/Resources/views/error.html.twig | 15 ++ web/index.php | 136 +---------------- 19 files changed, 391 insertions(+), 132 deletions(-) create mode 100644 app/bootstrap.php create mode 100644 app/bootstrap.php.d/00-main.php create mode 100644 app/bootstrap.php.d/10-config.php create mode 100644 app/bootstrap.php.d/20-error.php create mode 100644 app/bootstrap.php.d/20-routing.php create mode 100644 app/bootstrap.php.d/20-twig.php create mode 100644 app/bootstrap.php.d/30-trans.php create mode 100644 app/config/routing.yml create mode 100755 app/console create mode 100755 app/deploy create mode 100644 app/locales/en.yml create mode 100644 app/locales/fr.yml create mode 100644 src/Gist/Application.php create mode 100644 src/Gist/Controller/HomeController.php create mode 100644 src/Gist/Resources/views/Home/index.html.twig create mode 100644 src/Gist/Resources/views/base.html.twig create mode 100644 src/Gist/Resources/views/error.html.twig diff --git a/app/bootstrap.php b/app/bootstrap.php new file mode 100644 index 0000000..5838927 --- /dev/null +++ b/app/bootstrap.php @@ -0,0 +1,36 @@ +isDot() && $file->isFile()) { + $files[] = $file->getPathname(); + } + } + + // Sort init files, order is important + sort($files); + + foreach ($files as $file) { + $closure($file); + } + + return $app; +}); diff --git a/app/bootstrap.php.d/00-main.php b/app/bootstrap.php.d/00-main.php new file mode 100644 index 0000000..86d5573 --- /dev/null +++ b/app/bootstrap.php.d/00-main.php @@ -0,0 +1,7 @@ +error(function (Exception $e, $code) use ($app) { + return $app['twig']->render( + 'error.html.twig', + array( + 'code' => $code, + 'name' => get_class($e), + 'exception' => $e, + ) + ); +}); diff --git a/app/bootstrap.php.d/20-routing.php b/app/bootstrap.php.d/20-routing.php new file mode 100644 index 0000000..dfeb2fe --- /dev/null +++ b/app/bootstrap.php.d/20-routing.php @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..df3ce3f --- /dev/null +++ b/app/bootstrap.php.d/20-twig.php @@ -0,0 +1,13 @@ +register(new TwigServiceProvider(), array( + 'twig.path' => $app['root_path'].'/src/Gist/Resources/views', +)); + +$app->extend('twig', function ($twig, $app) { + $twig->addGlobal('web_path', '/'); + + return $twig; +}); diff --git a/app/bootstrap.php.d/30-trans.php b/app/bootstrap.php.d/30-trans.php new file mode 100644 index 0000000..43859e1 --- /dev/null +++ b/app/bootstrap.php.d/30-trans.php @@ -0,0 +1,58 @@ +register(new TranslationServiceProvider(), array( + 'locale' => 'en', + 'locale_fallback' => 'en', + 'locales' => array('en', 'fr'), // Custom parameter, not Silex +)); + +$app['translator'] = $app->extend('translator', function ($translator, $app) { + $translator->addLoader('yaml', new YamlFileLoader()); + + foreach ($app['locales'] as $locale) { + $file = $app['root_path'].'/app/locales/'.$locale.'.yml'; + + if (is_file($file)) { + $translator->addResource('yaml', $file, $locale); + } + } + + return $translator; +}); + +$app['routes'] = $app->extend('routes', function ($routes, $app) { + $routes->addPrefix('/{_locale}'); + $routes->addDefaults(array('_locale' => $app['locale_fallbacks'][0])); + $routes->addRequirements(array('_locale' => implode('|', $app['locales']))); + + return $routes; +}); + +/** + * Redirect home on right locale page, regarding of request accept locale or + * default fallback. + */ +$app->get('/', function (Request $request) use ($app) { + $accept = AcceptHeader::fromString($request->headers->get('Accept-Language')); + + // Default locale fallback + $foundLocale = $app['translator']->getLocale(); + + foreach ($app['locales'] as $locale) { + if ($accept->has($locale)) { + $foundLocale = $locale; + break; + } + } + + return new RedirectResponse($app['url_generator']->generate( + 'home', + array('_locale' => $foundLocale) + )); +}); diff --git a/app/config/routing.yml b/app/config/routing.yml new file mode 100644 index 0000000..c8f3804 --- /dev/null +++ b/app/config/routing.yml @@ -0,0 +1,3 @@ +home: + path: / + defaults: { _controller: Gist\Controller\HomeController::indexAction, _locale: en } diff --git a/app/console b/app/console new file mode 100755 index 0000000..9f603e5 --- /dev/null +++ b/app/console @@ -0,0 +1,6 @@ +#!/usr/bin/env php +run(); diff --git a/app/deploy b/app/deploy new file mode 100755 index 0000000..1409a3d --- /dev/null +++ b/app/deploy @@ -0,0 +1,14 @@ +#!/bin/sh + +cd "$(dirname "$0")/.." + +deploy_preprod() { + rsync -avzoK --delete -e ssh * weblinuxtest@deblan.fr:/services/web/www/linux-test.deblan.org/project/ +} + + +deploy_preprod_lan() { + rsync -avzoK --delete -e ssh * weblinuxtest@hinata:/services/web/www/linux-test.deblan.org/project/ +} + +([ -n "$1" ] && deploy_$1) || deploy_preprod diff --git a/app/locales/en.yml b/app/locales/en.yml new file mode 100644 index 0000000..90dea7e --- /dev/null +++ b/app/locales/en.yml @@ -0,0 +1,8 @@ +app: + title_prefix: '#!GIST - ' + + menu: + home: + title: 'Home' + about: + title: 'About' diff --git a/app/locales/fr.yml b/app/locales/fr.yml new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json index c259a84..d5df57f 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,15 @@ { "require": { - "geshi/geshi": "dev-master" + "geshi/geshi": "dev-master", + "silex/silex": "dev-master", + "symfony/yaml": "~2.6", + "symfony/twig-bridge": "dev-master", + "symfony/translation": "dev-master", + "symfony/config": "2.8.x-dev" + }, + "autoload": { + "psr-0": { + "": "src/" + } } } diff --git a/src/Gist/Application.php b/src/Gist/Application.php new file mode 100644 index 0000000..0df6d7a --- /dev/null +++ b/src/Gist/Application.php @@ -0,0 +1,22 @@ +render('Home/index.html.twig'); + } +} diff --git a/src/Gist/Resources/views/Home/index.html.twig b/src/Gist/Resources/views/Home/index.html.twig new file mode 100644 index 0000000..1503ec7 --- /dev/null +++ b/src/Gist/Resources/views/Home/index.html.twig @@ -0,0 +1 @@ +{% extends 'base.html.twig' %} diff --git a/src/Gist/Resources/views/base.html.twig b/src/Gist/Resources/views/base.html.twig new file mode 100644 index 0000000..94f7b40 --- /dev/null +++ b/src/Gist/Resources/views/base.html.twig @@ -0,0 +1,139 @@ + + + + {% block css %} + + + + {% endblock %} + + {% block metas %} + + + {% endblock %} + + {{ 'app.title_prefix'|trans }}{% block title %}{% endblock %} + + + +
+
+
+
+
+ +
+
+

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

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

+ +

+

+ +

+
+
+
+
+ {% block js %} + + + + - - - -
- - +run();