• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13532154862

25 Feb 2025 10:24PM UTC coverage: 30.374% (+1.7%) from 28.706%
13532154862

push

github

web-flow
Update php.yml

544 of 1791 relevant lines covered (30.37%)

9.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Factory/KernelFactory.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Ody\Core\Factory;
5

6
use Ody\Core\Factory\Psr17\Psr17Factory;
7
use Ody\Core\Factory\Psr17\Psr17FactoryProvider;
8
use Ody\Core\Factory\Psr17\SlimHttpPsr17Factory;
9
use Ody\Core\Interfaces\CallableResolverInterface;
10
use Ody\Core\Interfaces\MiddlewareDispatcherInterface;
11
use Ody\Core\Interfaces\Psr17FactoryProviderInterface;
12
use Ody\Core\Interfaces\RouteCollectorInterface;
13
use Ody\Core\Interfaces\RouteResolverInterface;
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\ContainerInterface;
16
use Psr\Container\NotFoundExceptionInterface;
17
use Psr\Http\Message\ResponseFactoryInterface;
18
use Psr\Http\Message\StreamFactoryInterface;
19
use RuntimeException;
20
use Ody\Core\Kernel;
21

22
/** @api */
23
class KernelFactory
24
{
25
    protected static ?Psr17FactoryProviderInterface $psr17FactoryProvider = null;
26

27
    protected static ?ResponseFactoryInterface $responseFactory = null;
28

29
    protected static ?StreamFactoryInterface $streamFactory = null;
30

31
    protected static ?ContainerInterface $container = null;
32

33
    protected static ?CallableResolverInterface $callableResolver = null;
34

35
    protected static ?RouteCollectorInterface $routeCollector = null;
36

37
    protected static ?RouteResolverInterface $routeResolver = null;
38

39
    protected static ?MiddlewareDispatcherInterface $middlewareDispatcher = null;
40

41
    protected static bool $slimHttpDecoratorsAutomaticDetectionEnabled = true;
42

43
    /**
44
     * @template TContainerInterface of (ContainerInterface|null)
45
     * @param TContainerInterface $container
46
     * @return (TContainerInterface is ContainerInterface ? Kernel<TContainerInterface> : Kernel<ContainerInterface|null>)
47
     */
48
    public static function create(
×
49
        ?ResponseFactoryInterface $responseFactory = null,
50
        ?ContainerInterface $container = null,
51
        ?CallableResolverInterface $callableResolver = null,
52
        ?RouteCollectorInterface $routeCollector = null,
53
        ?RouteResolverInterface $routeResolver = null,
54
        ?MiddlewareDispatcherInterface $middlewareDispatcher = null
55
    ): Kernel {
56
        static::$responseFactory = $responseFactory ?? static::$responseFactory;
×
57
        return new Kernel(
×
58
            self::determineResponseFactory(),
×
59
            $container ?? static::$container,
×
60
            $callableResolver ?? static::$callableResolver,
×
61
            $routeCollector ?? static::$routeCollector,
×
62
            $routeResolver ?? static::$routeResolver,
×
63
            $middlewareDispatcher ?? static::$middlewareDispatcher
×
64
        );
×
65
    }
66

67
    /**
68
     * @template TContainerInterface of (ContainerInterface)
69
     * @param ContainerInterface $container
70
     * @return Kernel
71
     * @throws ContainerExceptionInterface
72
     * @throws NotFoundExceptionInterface
73
     */
74
    public static function createFromContainer(ContainerInterface $container): Kernel
×
75
    {
76
        $responseFactory = $container->has(ResponseFactoryInterface::class)
×
77
        && (
×
78
        $responseFactoryFromContainer = $container->get(ResponseFactoryInterface::class)
×
79
        ) instanceof ResponseFactoryInterface
×
80
            ? $responseFactoryFromContainer
×
81
            : self::determineResponseFactory();
×
82

83
        $callableResolver = $container->has(CallableResolverInterface::class)
×
84
        && (
×
85
        $callableResolverFromContainer = $container->get(CallableResolverInterface::class)
×
86
        ) instanceof CallableResolverInterface
×
87
            ? $callableResolverFromContainer
×
88
            : null;
×
89

90
        $routeCollector = $container->has(RouteCollectorInterface::class)
×
91
        && (
×
92
        $routeCollectorFromContainer = $container->get(RouteCollectorInterface::class)
×
93
        ) instanceof RouteCollectorInterface
×
94
            ? $routeCollectorFromContainer
×
95
            : null;
×
96

97
        $routeResolver = $container->has(RouteResolverInterface::class)
×
98
        && (
×
99
        $routeResolverFromContainer = $container->get(RouteResolverInterface::class)
×
100
        ) instanceof RouteResolverInterface
×
101
            ? $routeResolverFromContainer
×
102
            : null;
×
103

104
        $middlewareDispatcher = $container->has(MiddlewareDispatcherInterface::class)
×
105
        && (
×
106
        $middlewareDispatcherFromContainer = $container->get(MiddlewareDispatcherInterface::class)
×
107
        ) instanceof MiddlewareDispatcherInterface
×
108
            ? $middlewareDispatcherFromContainer
×
109
            : null;
×
110

111
        return new Kernel(
×
112
            $responseFactory,
×
113
            $container,
×
114
            $callableResolver,
×
115
            $routeCollector,
×
116
            $routeResolver,
×
117
            $middlewareDispatcher
×
118
        );
×
119
    }
120

121
    /**
122
     * @throws RuntimeException
123
     */
124
    public static function determineResponseFactory(): ResponseFactoryInterface
×
125
    {
126
        if (static::$responseFactory) {
×
127
            if (static::$streamFactory) {
×
128
                return static::attemptResponseFactoryDecoration(static::$responseFactory, static::$streamFactory);
×
129
            }
130
            return static::$responseFactory;
×
131
        }
132

133
        $psr17FactoryProvider = static::$psr17FactoryProvider ?? new Psr17FactoryProvider();
×
134

135
        /** @var Psr17Factory $psr17factory */
136
        foreach ($psr17FactoryProvider->getFactories() as $psr17factory) {
×
137
            if ($psr17factory::isResponseFactoryAvailable()) {
×
138
                $responseFactory = $psr17factory::getResponseFactory();
×
139

140
                if (static::$streamFactory || $psr17factory::isStreamFactoryAvailable()) {
×
141
                    $streamFactory = static::$streamFactory ?? $psr17factory::getStreamFactory();
×
142
                    return static::attemptResponseFactoryDecoration($responseFactory, $streamFactory);
×
143
                }
144

145
                return $responseFactory;
×
146
            }
147
        }
148

149
        throw new RuntimeException(
×
150
            "Could not detect any PSR-17 ResponseFactory implementations. " .
×
151
            "Please install a supported implementation in order to use `KernelFactory::create()`. " .
×
152
            "See https://github.com/slimphp/Slim/blob/4.x/README.md for a list of supported implementations."
×
153
        );
×
154
    }
155

156
    protected static function attemptResponseFactoryDecoration(
×
157
        ResponseFactoryInterface $responseFactory,
158
        StreamFactoryInterface $streamFactory
159
    ): ResponseFactoryInterface {
160
        if (
161
            static::$slimHttpDecoratorsAutomaticDetectionEnabled
×
162
            && SlimHttpPsr17Factory::isResponseFactoryAvailable()
×
163
        ) {
164
            return SlimHttpPsr17Factory::createDecoratedResponseFactory($responseFactory, $streamFactory);
×
165
        }
166

167
        return $responseFactory;
×
168
    }
169

170
    public static function setPsr17FactoryProvider(Psr17FactoryProviderInterface $psr17FactoryProvider): void
×
171
    {
172
        static::$psr17FactoryProvider = $psr17FactoryProvider;
×
173
    }
174

175
    public static function setResponseFactory(ResponseFactoryInterface $responseFactory): void
×
176
    {
177
        static::$responseFactory = $responseFactory;
×
178
    }
179

180
    public static function setStreamFactory(StreamFactoryInterface $streamFactory): void
×
181
    {
182
        static::$streamFactory = $streamFactory;
×
183
    }
184

185
    public static function setContainer(ContainerInterface $container): void
×
186
    {
187
        static::$container = $container;
×
188
    }
189

190
    public static function setCallableResolver(CallableResolverInterface $callableResolver): void
×
191
    {
192
        static::$callableResolver = $callableResolver;
×
193
    }
194

195
    public static function setRouteCollector(RouteCollectorInterface $routeCollector): void
×
196
    {
197
        static::$routeCollector = $routeCollector;
×
198
    }
199

200
    public static function setRouteResolver(RouteResolverInterface $routeResolver): void
×
201
    {
202
        static::$routeResolver = $routeResolver;
×
203
    }
204

205
    public static function setMiddlewareDispatcher(MiddlewareDispatcherInterface $middlewareDispatcher): void
×
206
    {
207
        static::$middlewareDispatcher = $middlewareDispatcher;
×
208
    }
209

210
    public static function setSlimHttpDecoratorsAutomaticDetection(bool $enabled): void
×
211
    {
212
        static::$slimHttpDecoratorsAutomaticDetectionEnabled = $enabled;
×
213
    }
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc