summaryrefslogtreecommitdiff
path: root/src/Middleware/RouteDispatcherServiceProvider.php
blob: 3b4fa18318fb73fbea2cce750690c401cb5769ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php

namespace Engelsystem\Middleware;

use Engelsystem\Container\ServiceProvider;
use FastRoute\Dispatcher as FastRouteDispatcher;
use FastRoute\RouteCollector;
use Psr\Http\Server\MiddlewareInterface;

class RouteDispatcherServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->alias(RouteDispatcher::class, 'route.dispatcher');

        $this->app
            ->when(RouteDispatcher::class)
            ->needs(FastRouteDispatcher::class)
            ->give(function () {
                return $this->generateRouting();
            });

        $this->app
            ->when(RouteDispatcher::class)
            ->needs(MiddlewareInterface::class)
            ->give(LegacyMiddleware::class);
    }

    /**
     * Includes the routes.php file
     *
     * @return FastRouteDispatcher
     * @codeCoverageIgnore
     */
    function generateRouting()
    {
        return \FastRoute\simpleDispatcher(function (RouteCollector $route) {
            require config_path('routes.php');
        });
    }
}