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\Factory;
12
 
13
use Psr\Container\ContainerInterface;
14
use Psr\Http\Message\ResponseFactoryInterface;
15
use Psr\Http\Message\StreamFactoryInterface;
16
use RuntimeException;
17
use Slim\App;
18
use Slim\Factory\Psr17\Psr17Factory;
19
use Slim\Factory\Psr17\Psr17FactoryProvider;
20
use Slim\Factory\Psr17\SlimHttpPsr17Factory;
21
use Slim\Interfaces\CallableResolverInterface;
22
use Slim\Interfaces\MiddlewareDispatcherInterface;
23
use Slim\Interfaces\Psr17FactoryProviderInterface;
24
use Slim\Interfaces\RouteCollectorInterface;
25
use Slim\Interfaces\RouteResolverInterface;
26
 
27
class AppFactory
28
{
29
    protected static ?Psr17FactoryProviderInterface $psr17FactoryProvider = null;
30
 
31
    protected static ?ResponseFactoryInterface $responseFactory = null;
32
 
33
    protected static ?StreamFactoryInterface $streamFactory = null;
34
 
35
    protected static ?ContainerInterface $container = null;
36
 
37
    protected static ?CallableResolverInterface $callableResolver = null;
38
 
39
    protected static ?RouteCollectorInterface $routeCollector = null;
40
 
41
    protected static ?RouteResolverInterface $routeResolver = null;
42
 
43
    protected static ?MiddlewareDispatcherInterface $middlewareDispatcher = null;
44
 
45
    protected static bool $slimHttpDecoratorsAutomaticDetectionEnabled = true;
46
 
47
    public static function create(
48
        ?ResponseFactoryInterface $responseFactory = null,
49
        ?ContainerInterface $container = null,
50
        ?CallableResolverInterface $callableResolver = null,
51
        ?RouteCollectorInterface $routeCollector = null,
52
        ?RouteResolverInterface $routeResolver = null,
53
        ?MiddlewareDispatcherInterface $middlewareDispatcher = null
54
    ): App {
55
        static::$responseFactory = $responseFactory ?? static::$responseFactory;
56
        return new App(
57
            self::determineResponseFactory(),
58
            $container ?? static::$container,
59
            $callableResolver ?? static::$callableResolver,
60
            $routeCollector ?? static::$routeCollector,
61
            $routeResolver ?? static::$routeResolver,
62
            $middlewareDispatcher ?? static::$middlewareDispatcher
63
        );
64
    }
65
 
66
    public static function createFromContainer(ContainerInterface $container): App
67
    {
68
        $responseFactory = $container->has(ResponseFactoryInterface::class)
69
        && (
70
            $responseFactoryFromContainer = $container->get(ResponseFactoryInterface::class)
71
        ) instanceof ResponseFactoryInterface
72
            ? $responseFactoryFromContainer
73
            : self::determineResponseFactory();
74
 
75
        $callableResolver = $container->has(CallableResolverInterface::class)
76
        && (
77
            $callableResolverFromContainer = $container->get(CallableResolverInterface::class)
78
        ) instanceof CallableResolverInterface
79
            ? $callableResolverFromContainer
80
            : null;
81
 
82
        $routeCollector = $container->has(RouteCollectorInterface::class)
83
        && (
84
            $routeCollectorFromContainer = $container->get(RouteCollectorInterface::class)
85
        ) instanceof RouteCollectorInterface
86
            ? $routeCollectorFromContainer
87
            : null;
88
 
89
        $routeResolver = $container->has(RouteResolverInterface::class)
90
        && (
91
            $routeResolverFromContainer = $container->get(RouteResolverInterface::class)
92
        ) instanceof RouteResolverInterface
93
            ? $routeResolverFromContainer
94
            : null;
95
 
96
        $middlewareDispatcher = $container->has(MiddlewareDispatcherInterface::class)
97
        && (
98
            $middlewareDispatcherFromContainer = $container->get(MiddlewareDispatcherInterface::class)
99
        ) instanceof MiddlewareDispatcherInterface
100
            ? $middlewareDispatcherFromContainer
101
            : null;
102
 
103
        return new App(
104
            $responseFactory,
105
            $container,
106
            $callableResolver,
107
            $routeCollector,
108
            $routeResolver,
109
            $middlewareDispatcher
110
        );
111
    }
112
 
113
    /**
114
     * @throws RuntimeException
115
     */
116
    public static function determineResponseFactory(): ResponseFactoryInterface
117
    {
118
        if (static::$responseFactory) {
119
            if (static::$streamFactory) {
120
                return static::attemptResponseFactoryDecoration(static::$responseFactory, static::$streamFactory);
121
            }
122
            return static::$responseFactory;
123
        }
124
 
125
        $psr17FactoryProvider = static::$psr17FactoryProvider ?? new Psr17FactoryProvider();
126
 
127
        /** @var Psr17Factory $psr17factory */
128
        foreach ($psr17FactoryProvider->getFactories() as $psr17factory) {
129
            if ($psr17factory::isResponseFactoryAvailable()) {
130
                $responseFactory = $psr17factory::getResponseFactory();
131
 
132
                if (static::$streamFactory || $psr17factory::isStreamFactoryAvailable()) {
133
                    $streamFactory = static::$streamFactory ?? $psr17factory::getStreamFactory();
134
                    return static::attemptResponseFactoryDecoration($responseFactory, $streamFactory);
135
                }
136
 
137
                return $responseFactory;
138
            }
139
        }
140
 
141
        throw new RuntimeException(
142
            "Could not detect any PSR-17 ResponseFactory implementations. " .
143
            "Please install a supported implementation in order to use `AppFactory::create()`. " .
144
            "See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations."
145
        );
146
    }
147
 
148
    protected static function attemptResponseFactoryDecoration(
149
        ResponseFactoryInterface $responseFactory,
150
        StreamFactoryInterface $streamFactory
151
    ): ResponseFactoryInterface {
152
        if (
153
            static::$slimHttpDecoratorsAutomaticDetectionEnabled
154
            && SlimHttpPsr17Factory::isResponseFactoryAvailable()
155
        ) {
156
            return SlimHttpPsr17Factory::createDecoratedResponseFactory($responseFactory, $streamFactory);
157
        }
158
 
159
        return $responseFactory;
160
    }
161
 
162
    public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void
163
    {
164
        static::$psr17FactoryProvider = $psr17FactoryProvider;
165
    }
166
 
167
    public static function setResponseFactory(ResponseFactoryInterface $responseFactory): void
168
    {
169
        static::$responseFactory = $responseFactory;
170
    }
171
 
172
    public static function setStreamFactory(StreamFactoryInterface $streamFactory): void
173
    {
174
        static::$streamFactory = $streamFactory;
175
    }
176
 
177
    public static function setContainer(ContainerInterface $container): void
178
    {
179
        static::$container = $container;
180
    }
181
 
182
    public static function setCallableResolver(CallableResolverInterface $callableResolver): void
183
    {
184
        static::$callableResolver = $callableResolver;
185
    }
186
 
187
    public static function setRouteCollector(RouteCollectorInterface $routeCollector): void
188
    {
189
        static::$routeCollector = $routeCollector;
190
    }
191
 
192
    public static function setRouteResolver(RouteResolverInterface $routeResolver): void
193
    {
194
        static::$routeResolver = $routeResolver;
195
    }
196
 
197
    public static function setMiddlewareDispatcher(MiddlewareDispatcherInterface $middlewareDispatcher): void
198
    {
199
        static::$middlewareDispatcher = $middlewareDispatcher;
200
    }
201
 
202
    public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void
203
    {
204
        static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled;
205
    }
206
}