From 2559ae33b495ab7ff08611fc706d416a2ea7511a Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 21 Apr 2022 15:52:29 +0200
Subject: [PATCH] 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']
---
 src/Resources/config/routing.yaml  |  5 +++
 src/Resources/config/services.yaml |  7 +++++
 src/Routing/RoutingLoader.php      | 50 ++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 src/Routing/RoutingLoader.php

diff --git a/src/Resources/config/routing.yaml b/src/Resources/config/routing.yaml
index d1923b1..0cfbffb 100644
--- a/src/Resources/config/routing.yaml
+++ b/src/Resources/config/routing.yaml
@@ -2,3 +2,8 @@ api_platform:
   resource: .
   type: api_platform
   prefix: /
+
+dbp_relay:
+  resource: .
+  type: dbp_relay
+  prefix: /
diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml
index 7b77597..3376ac0 100644
--- a/src/Resources/config/services.yaml
+++ b/src/Resources/config/services.yaml
@@ -61,3 +61,10 @@ services:
   Dbp\Relay\CoreBundle\Service\LocalDataAwareEventDispatcher:
     autowire: true
     autoconfigure: true
+
+  Dbp\Relay\CoreBundle\Routing\RoutingLoader:
+    tags: ['routing.loader']
+    autowire: true
+    autoconfigure: true
+    arguments:
+      $env: ~
diff --git a/src/Routing/RoutingLoader.php b/src/Routing/RoutingLoader.php
new file mode 100644
index 0000000..54c64d2
--- /dev/null
+++ b/src/Routing/RoutingLoader.php
@@ -0,0 +1,50 @@
+<?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;
+    }
+}
-- 
GitLab