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\Container\ContainerInterface;
14
use Psr\Http\Message\ResponseFactoryInterface;
15
use Slim\Interfaces\CallableResolverInterface;
16
use Slim\Interfaces\RouteCollectorInterface;
17
use Slim\Interfaces\RouteCollectorProxyInterface;
18
use Slim\Interfaces\RouteGroupInterface;
19
use Slim\Interfaces\RouteInterface;
20
 
21
class RouteCollectorProxy implements RouteCollectorProxyInterface
22
{
23
    protected ResponseFactoryInterface $responseFactory;
24
 
25
    protected CallableResolverInterface $callableResolver;
26
 
27
    protected ?ContainerInterface $container = null;
28
 
29
    protected RouteCollectorInterface $routeCollector;
30
 
31
    protected string $groupPattern;
32
 
33
    public function __construct(
34
        ResponseFactoryInterface $responseFactory,
35
        CallableResolverInterface $callableResolver,
36
        ?ContainerInterface $container = null,
37
        ?RouteCollectorInterface $routeCollector = null,
38
        string $groupPattern = ''
39
    ) {
40
        $this->responseFactory = $responseFactory;
41
        $this->callableResolver = $callableResolver;
42
        $this->container = $container;
43
        $this->routeCollector = $routeCollector ?? new RouteCollector($responseFactory, $callableResolver, $container);
44
        $this->groupPattern = $groupPattern;
45
    }
46
 
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getResponseFactory(): ResponseFactoryInterface
51
    {
52
        return $this->responseFactory;
53
    }
54
 
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getCallableResolver(): CallableResolverInterface
59
    {
60
        return $this->callableResolver;
61
    }
62
 
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getContainer(): ?ContainerInterface
67
    {
68
        return $this->container;
69
    }
70
 
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getRouteCollector(): RouteCollectorInterface
75
    {
76
        return $this->routeCollector;
77
    }
78
 
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getBasePath(): string
83
    {
84
        return $this->routeCollector->getBasePath();
85
    }
86
 
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function setBasePath(string $basePath): RouteCollectorProxyInterface
91
    {
92
        $this->routeCollector->setBasePath($basePath);
93
 
94
        return $this;
95
    }
96
 
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function get(string $pattern, $callable): RouteInterface
101
    {
102
        return $this->map(['GET'], $pattern, $callable);
103
    }
104
 
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function post(string $pattern, $callable): RouteInterface
109
    {
110
        return $this->map(['POST'], $pattern, $callable);
111
    }
112
 
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function put(string $pattern, $callable): RouteInterface
117
    {
118
        return $this->map(['PUT'], $pattern, $callable);
119
    }
120
 
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function patch(string $pattern, $callable): RouteInterface
125
    {
126
        return $this->map(['PATCH'], $pattern, $callable);
127
    }
128
 
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function delete(string $pattern, $callable): RouteInterface
133
    {
134
        return $this->map(['DELETE'], $pattern, $callable);
135
    }
136
 
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function options(string $pattern, $callable): RouteInterface
141
    {
142
        return $this->map(['OPTIONS'], $pattern, $callable);
143
    }
144
 
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function any(string $pattern, $callable): RouteInterface
149
    {
150
        return $this->map(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $pattern, $callable);
151
    }
152
 
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function map(array $methods, string $pattern, $callable): RouteInterface
157
    {
158
        $pattern = $this->groupPattern . $pattern;
159
 
160
        return $this->routeCollector->map($methods, $pattern, $callable);
161
    }
162
 
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function group(string $pattern, $callable): RouteGroupInterface
167
    {
168
        $pattern = $this->groupPattern . $pattern;
169
 
170
        return $this->routeCollector->group($pattern, $callable);
171
    }
172
 
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function redirect(string $from, $to, int $status = 302): RouteInterface
177
    {
178
        $responseFactory = $this->responseFactory;
179
 
180
        $handler = function () use ($to, $status, $responseFactory) {
181
            $response = $responseFactory->createResponse($status);
182
            return $response->withHeader('Location', (string) $to);
183
        };
184
 
185
        return $this->get($from, $handler);
186
    }
187
}