mirror of
https://github.com/klaussilveira/gitlist.git
synced 2026-08-30 11:55:48 +02:00
Add routing service to handle repos in subdirectories
This commit is contained in:
@@ -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'));
|
||||
|
||||
26
src/GitList/Provider/RoutingUtilServiceProvider.php
Normal file
26
src/GitList/Provider/RoutingUtilServiceProvider.php
Normal 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)
|
||||
{
|
||||
}
|
||||
}
|
||||
51
src/GitList/Util/Routing.php
Normal file
51
src/GitList/Util/Routing.php
Normal 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user