Add routing service to handle repos in subdirectories

This commit is contained in:
Christian Schorn
2012-10-31 14:06:03 +01:00
parent 6a93bddabb
commit ffe27b2a75
3 changed files with 79 additions and 0 deletions

View File

@@ -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'));

View File

@@ -0,0 +1,26 @@
<?php
namespace GitList\Provider;
use GitList\Util\Routing;
use Silex\Application;
use Silex\ServiceProviderInterface;
class RoutingUtilServiceProvider implements ServiceProviderInterface
{
/**
* Register the Util\Repository class on the Application ServiceProvider
*
* @param Application $app Silex Application
*/
public function register(Application $app)
{
$app['util.routing'] = $app->share(function () use ($app) {
return new Routing($app);
});
}
public function boot(Application $app)
{
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Gitlist\Util;
use Silex\Application;
class Routing
{
protected $app;
public function __construct(Application $app)
{
$this->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)
);
}
}
}