Add plugin capability to add routes.

This commit is contained in:
Benoît Burnichon
2015-07-29 22:15:44 +02:00
parent fce0dd1d85
commit 2e2948f477
3 changed files with 46 additions and 0 deletions

View File

@@ -114,6 +114,7 @@ use Neutron\Silex\Provider\FilesystemServiceProvider;
use Neutron\ReCaptcha\ReCaptchaServiceProvider;
use PHPExiftool\PHPExiftoolServiceProvider;
use Silex\Application as SilexApplication;
use Silex\ControllerProviderInterface;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\MonologServiceProvider;
use Silex\Provider\SessionServiceProvider;
@@ -833,6 +834,8 @@ class Application extends SilexApplication
$this->mount('/thesaurus', new Thesaurus());
$this->mount('/xmlhttp', new ThesaurusXMLHttp());
$this->bindPluginRoutes('plugin.controller_providers.root');
}
/**
@@ -854,4 +857,36 @@ class Application extends SilexApplication
{
return static::$flashTypes;
}
/**
* @param $routeParameter
*/
public function bindPluginRoutes($routeParameter)
{
foreach ($this[$routeParameter] as $provider) {
$prefix = '';
if (is_array($provider)) {
$providerDefinition = $provider;
list($prefix, $provider) = $providerDefinition;
}
if (!is_string($prefix) || !is_string($provider)) {
continue;
}
$prefix = '/' . ltrim($prefix, '/');
if (!isset($this[$provider])) {
continue;
}
$provider = $this[$provider];
if (!$provider instanceof ControllerProviderInterface) {
continue;
}
$this->mount($prefix, $provider);
}
}
}

View File

@@ -129,6 +129,7 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
$app->mount('/api/oauthv2', new Oauth2());
$app->mount('/datafiles/', new Datafiles());
$app->mount('/api/v1', new V1());
$app->bindPluginRoutes('plugin.controller_providers.api');
$app['dispatcher']->addSubscriber(new ApiOauth2ErrorsSubscriber($app['phraseanet.exception_handler']));
$app['dispatcher']->addSubscriber(new ApiCorsSubscriber($app));

View File

@@ -23,6 +23,16 @@ class PluginServiceProvider implements ServiceProviderInterface
});
$app['plugin.locale.textdomains'] = new \ArrayObject();
// Routes will be bound after all others
// Add a new controller provider can be added as follows
// $app['plugin.controller_providers'][] = array('/prefix', 'controller_provider_service_key');
$app['plugin.controller_providers.root'] = new \ArrayObject();
// Routes will be bound after all others
// Add a new controller provider can be added as follows
// $app['plugin.controller_providers'][] = array('/prefix', 'controller_provider_service_key');
$app['plugin.controller_providers.api'] = new \ArrayObject();
}
public function boot(Application $app)