From 5624c4bc0f222565aa9dea36cf21cfc8c4edb1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Sun, 15 Jul 2012 11:11:42 +0200 Subject: [PATCH] Move services init to a specific GitList\Application class --- index.php | 31 +------------------ lib/GitList/Application.php | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 lib/GitList/Application.php diff --git a/index.php b/index.php index a9d6bcd..1ae2e04 100644 --- a/index.php +++ b/index.php @@ -12,29 +12,7 @@ $config = new GitList\Config('config.ini'); $config->set('git', 'repositories', rtrim($config->get('git', 'repositories'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); // Startup and configure Silex application -$app = new Silex\Application(); -$app['debug'] = $config->get('app', 'debug'); -$app['filetypes'] = $config->getSection('filetypes'); -$app['cache.archives'] = __DIR__ . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'archives'; - -// Register services -$app->register(new Silex\Provider\TwigServiceProvider(), array( - 'twig.path' => __DIR__ . '/views', - 'twig.options' => array('cache' => __DIR__ . '/cache'), -)); -$app->register(new GitList\Provider\GitServiceProvider(), array( - 'git.client' => $config->get('git', 'client'), - 'git.repos' => $config->get('git', 'repositories'), - 'git.hidden' => $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(), -)); -$app->register(new GitList\Provider\ViewUtilServiceProvider()); -$app->register(new GitList\Provider\RepositoryUtilServiceProvider()); -$app->register(new Silex\Provider\UrlGeneratorServiceProvider()); - -$app['twig'] = $app->share($app->extend('twig', function($twig, $app) { - $twig->addFilter('md5', new Twig_Filter_Function('md5')); - return $twig; -})); +$app = new GitList\Application($config, __DIR__); // Mount the controllers $app->mount('', new GitList\Controller\MainController()); @@ -42,11 +20,4 @@ $app->mount('', new GitList\Controller\BlobController()); $app->mount('', new GitList\Controller\CommitController()); $app->mount('', new GitList\Controller\TreeController()); -// Handle errors -$app->error(function (\Exception $e, $code) use ($app) { - return $app['twig']->render('error.twig', array( - 'message' => $e->getMessage(), - )); -}); - $app->run(); diff --git a/lib/GitList/Application.php b/lib/GitList/Application.php new file mode 100644 index 0000000..92983e9 --- /dev/null +++ b/lib/GitList/Application.php @@ -0,0 +1,61 @@ +get('app', 'debug'); + $this['filetypes'] = $config->getSection('filetypes'); + $this['cache.archives'] = $root . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'archives'; + + // Register services + $this->register(new TwigServiceProvider(), array( + 'twig.path' => $root . DIRECTORY_SEPARATOR . 'views', + 'twig.options' => array('cache' => $root . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'views'), + )); + $this->register(new GitServiceProvider(), array( + 'git.client' => $config->get('git', 'client'), + 'git.repos' => $config->get('git', 'repositories'), + 'git.hidden' => $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(), + )); + $this->register(new ViewUtilServiceProvider()); + $this->register(new RepositoryUtilServiceProvider()); + $this->register(new UrlGeneratorServiceProvider()); + + $this['twig'] = $this->share($this->extend('twig', function($twig, $app) { + $twig->addFilter('md5', new \Twig_Filter_Function('md5')); + + return $twig; + })); + + // Handle errors + $this->error(function (\Exception $e, $code) use ($app) { + return $app['twig']->render('error.twig', array( + 'message' => $e->getMessage(), + )); + }); + } +}