Skip to content
Snippets Groups Projects
Commit 2559ae33 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Add a custom routing loader for bundles

This allows bundles to add something to the "dbp_api.route_resources"
parameter which will then be loaded as routes.

For example, using a routes.yml in the bundle, add the following to the array
in the bundle setup:

[__DIR__.'/../Resources/config/routes.yaml', 'yaml']
parent da80e649
No related branches found
No related tags found
No related merge requests found
Pipeline #103326 passed
...@@ -2,3 +2,8 @@ api_platform: ...@@ -2,3 +2,8 @@ api_platform:
resource: . resource: .
type: api_platform type: api_platform
prefix: / prefix: /
dbp_relay:
resource: .
type: dbp_relay
prefix: /
...@@ -61,3 +61,10 @@ services: ...@@ -61,3 +61,10 @@ services:
Dbp\Relay\CoreBundle\Service\LocalDataAwareEventDispatcher: Dbp\Relay\CoreBundle\Service\LocalDataAwareEventDispatcher:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
Dbp\Relay\CoreBundle\Routing\RoutingLoader:
tags: ['routing.loader']
autowire: true
autoconfigure: true
arguments:
$env: ~
<?php
declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Routing\RouteCollection;
class RoutingLoader extends Loader
{
/**
* @var ParameterBagInterface
*/
private $params;
public function __construct(string $env = null, ParameterBagInterface $params)
{
parent::__construct($env);
$this->params = $params;
}
/**
* @return mixed
*/
public function load($resource, string $type = null)
{
$routes = new RouteCollection();
$routeResources = [];
if ($this->params->has('dbp_api.route_resources')) {
$routeResources = $this->params->get('dbp_api.route_resources');
assert(is_array($routeResources));
}
foreach ($routeResources as [$resource, $type]) {
$importedRoutes = $this->import($resource, $type);
$routes->addCollection($importedRoutes);
}
return $routes;
}
public function supports($resource, string $type = null): bool
{
return 'dbp_relay' === $type;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment