Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Slim\Routing;
6
 
7
use FastRoute\DataGenerator\GroupCountBased;
8
use FastRoute\RouteCollector as FastRouteCollector;
9
use FastRoute\RouteParser\Std;
10
use Slim\Interfaces\DispatcherInterface;
11
use Slim\Interfaces\RouteCollectorInterface;
12
 
13
class Dispatcher implements DispatcherInterface
14
{
15
    private RouteCollectorInterface $routeCollector;
16
 
17
    private ?FastRouteDispatcher $dispatcher = null;
18
 
19
    public function __construct(RouteCollectorInterface $routeCollector)
20
    {
21
        $this->routeCollector = $routeCollector;
22
    }
23
 
24
    protected function createDispatcher(): FastRouteDispatcher
25
    {
26
        if ($this->dispatcher) {
27
            return $this->dispatcher;
28
        }
29
 
30
        $routeDefinitionCallback = function (FastRouteCollector $r): void {
31
            $basePath = $this->routeCollector->getBasePath();
32
 
33
            foreach ($this->routeCollector->getRoutes() as $route) {
34
                $r->addRoute($route->getMethods(), $basePath . $route->getPattern(), $route->getIdentifier());
35
            }
36
        };
37
 
38
        $cacheFile = $this->routeCollector->getCacheFile();
39
        if ($cacheFile) {
40
            /** @var FastRouteDispatcher $dispatcher */
41
            $dispatcher = \FastRoute\cachedDispatcher($routeDefinitionCallback, [
42
                'dataGenerator' => GroupCountBased::class,
43
                'dispatcher' => FastRouteDispatcher::class,
44
                'routeParser' => new Std(),
45
                'cacheFile' => $cacheFile,
46
            ]);
47
        } else {
48
            /** @var FastRouteDispatcher $dispatcher */
49
            $dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback, [
50
                'dataGenerator' => GroupCountBased::class,
51
                'dispatcher' => FastRouteDispatcher::class,
52
                'routeParser' => new Std(),
53
            ]);
54
        }
55
 
56
        $this->dispatcher = $dispatcher;
57
        return $this->dispatcher;
58
    }
59
 
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function dispatch(string $method, string $uri): RoutingResults
64
    {
65
        $dispatcher = $this->createDispatcher();
66
        $results = $dispatcher->dispatch($method, $uri);
67
        return new RoutingResults($this, $method, $uri, $results[0], $results[1], $results[2]);
68
    }
69
 
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getAllowedMethods(string $uri): array
74
    {
75
        $dispatcher = $this->createDispatcher();
76
        return $dispatcher->getAllowedMethods($uri);
77
    }
78
}