From ffe27b2a757597807a2870cc47e93c791159ba06 Mon Sep 17 00:00:00 2001 From: Christian Schorn Date: Wed, 31 Oct 2012 14:06:03 +0100 Subject: [PATCH] Add routing service to handle repos in subdirectories --- src/GitList/Application.php | 2 + .../Provider/RoutingUtilServiceProvider.php | 26 ++++++++++ src/GitList/Util/Routing.php | 51 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/GitList/Provider/RoutingUtilServiceProvider.php create mode 100644 src/GitList/Util/Routing.php diff --git a/src/GitList/Application.php b/src/GitList/Application.php index 739f0a2..7f7e2cd 100644 --- a/src/GitList/Application.php +++ b/src/GitList/Application.php @@ -8,6 +8,7 @@ use Silex\Provider\UrlGeneratorServiceProvider; use GitList\Provider\GitServiceProvider; use GitList\Provider\RepositoryUtilServiceProvider; use GitList\Provider\ViewUtilServiceProvider; +use GitList\Provider\RoutingUtilServiceProvider; /** * GitList application. @@ -44,6 +45,7 @@ class Application extends SilexApplication $this->register(new ViewUtilServiceProvider()); $this->register(new RepositoryUtilServiceProvider()); $this->register(new UrlGeneratorServiceProvider()); + $this->register(new RoutingUtilServiceProvider()); $this['twig'] = $this->share($this->extend('twig', function($twig, $app) { $twig->addFilter('md5', new \Twig_Filter_Function('md5')); diff --git a/src/GitList/Provider/RoutingUtilServiceProvider.php b/src/GitList/Provider/RoutingUtilServiceProvider.php new file mode 100644 index 0000000..95b43d6 --- /dev/null +++ b/src/GitList/Provider/RoutingUtilServiceProvider.php @@ -0,0 +1,26 @@ +share(function () use ($app) { + return new Routing($app); + }); + } + + public function boot(Application $app) + { + } +} diff --git a/src/GitList/Util/Routing.php b/src/GitList/Util/Routing.php new file mode 100644 index 0000000..c82438a --- /dev/null +++ b/src/GitList/Util/Routing.php @@ -0,0 +1,51 @@ +app = $app; + } + + public function getRepositoryRegex() + { + static $regex = null; + + if ($regex === null) { + $app = $this->app; + $quoted_paths = array_map( + function ($repo) use ($app) { + return preg_quote($app['util.routing']->getRelativePath($repo['path']), '/'); + }, + $this->app['git']->getRepositories($this->app['git.repos']) + ); + $regex = '/' . implode('|', $quoted_paths) . '/'; + } + + return $regex; + } + + /** + * Strips the base path from a full repository path + * + * @param string $repo_path Full path to the repository + * @return string Relative path to the repository from git.repositories + */ + public function getRelativePath($repo_path) + { + if (strpos($repo_path, $this->app['git.repos']) === 0) { + $relative_path = substr($repo_path, strlen($this->app['git.repos'])); + return ltrim($relative_path, '/'); + } else { + throw new \InvalidArgumentException( + sprintf("Path '%s' does not match configured repository directory", $repo_path) + ); + } + } +}