Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
/**
4
 * Slim Framework (https://slimframework.com)
5
 *
6
 * @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
7
 */
8
 
9
declare(strict_types=1);
10
 
11
namespace Slim\Routing;
12
 
13
use Psr\Http\Server\MiddlewareInterface;
14
use Slim\Interfaces\AdvancedCallableResolverInterface;
15
use Slim\Interfaces\CallableResolverInterface;
16
use Slim\Interfaces\RouteCollectorProxyInterface;
17
use Slim\Interfaces\RouteGroupInterface;
18
use Slim\MiddlewareDispatcher;
19
 
20
class RouteGroup implements RouteGroupInterface
21
{
22
    /**
23
     * @var callable|string
24
     */
25
    protected $callable;
26
 
27
    protected CallableResolverInterface $callableResolver;
28
 
29
    protected RouteCollectorProxyInterface $routeCollectorProxy;
30
 
31
    /**
32
     * @var MiddlewareInterface[]|string[]|callable[]
33
     */
34
    protected array $middleware = [];
35
 
36
    protected string $pattern;
37
 
38
    /**
39
     * @param callable|string              $callable
40
     */
41
    public function __construct(
42
        string $pattern,
43
        $callable,
44
        CallableResolverInterface $callableResolver,
45
        RouteCollectorProxyInterface $routeCollectorProxy
46
    ) {
47
        $this->pattern = $pattern;
48
        $this->callable = $callable;
49
        $this->callableResolver = $callableResolver;
50
        $this->routeCollectorProxy = $routeCollectorProxy;
51
    }
52
 
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function collectRoutes(): RouteGroupInterface
57
    {
58
        if ($this->callableResolver instanceof AdvancedCallableResolverInterface) {
59
            $callable = $this->callableResolver->resolveRoute($this->callable);
60
        } else {
61
            $callable = $this->callableResolver->resolve($this->callable);
62
        }
63
        $callable($this->routeCollectorProxy);
64
        return $this;
65
    }
66
 
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function add($middleware): RouteGroupInterface
71
    {
72
        $this->middleware[] = $middleware;
73
        return $this;
74
    }
75
 
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function addMiddleware(MiddlewareInterface $middleware): RouteGroupInterface
80
    {
81
        $this->middleware[] = $middleware;
82
        return $this;
83
    }
84
 
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function appendMiddlewareToDispatcher(MiddlewareDispatcher $dispatcher): RouteGroupInterface
89
    {
90
        foreach ($this->middleware as $middleware) {
91
            $dispatcher->add($middleware);
92
        }
93
 
94
        return $this;
95
    }
96
 
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getPattern(): string
101
    {
102
        return $this->pattern;
103
    }
104
}