@@ -19,7 +19,8 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | public function init(HttpBootloader $http): void |
| 21 | 21 | { |
| 22 | - foreach ($this->globalMiddleware() as $middleware) { |
|
| 22 | + foreach ($this->globalMiddleware() as $middleware) |
|
| 23 | + { |
|
| 23 | 24 | $http->addMiddleware($middleware); |
| 24 | 25 | } |
| 25 | 26 | } |
@@ -61,7 +62,8 @@ discard block |
||
| 61 | 62 | |
| 62 | 63 | private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void |
| 63 | 64 | { |
| 64 | - foreach ($groups as $group => $middleware) { |
|
| 65 | + foreach ($groups as $group => $middleware) |
|
| 66 | + { |
|
| 65 | 67 | $binder |
| 66 | 68 | ->getBinder('http') |
| 67 | 69 | ->bind( |
@@ -75,7 +77,8 @@ discard block |
||
| 75 | 77 | |
| 76 | 78 | private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void |
| 77 | 79 | { |
| 78 | - foreach ($groups as $group) { |
|
| 80 | + foreach ($groups as $group) |
|
| 81 | + { |
|
| 79 | 82 | $registry->getGroup($group)->addMiddleware('middleware:' . $group); |
| 80 | 83 | } |
| 81 | 84 | } |
@@ -92,9 +92,11 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | $previousRequest = $currentRequest->get(); |
| 94 | 94 | $currentRequest->set($request); |
| 95 | - try { |
|
| 95 | + try |
|
| 96 | + { |
|
| 96 | 97 | // There is no middleware to process, let's pass the request to the handler |
| 97 | - if (!\array_key_exists($this->position, $this->middleware)) { |
|
| 98 | + if (!\array_key_exists($this->position, $this->middleware)) |
|
| 99 | + { |
|
| 98 | 100 | return $this->handler->handle($request); |
| 99 | 101 | } |
| 100 | 102 | |
@@ -108,7 +110,8 @@ discard block |
||
| 108 | 110 | ? \sprintf('%s=%s', $this->middleware[$this->position], $middleware::class) |
| 109 | 111 | : $middleware::class; |
| 110 | 112 | // Init a tracing span when the pipeline starts |
| 111 | - if ($span === null) { |
|
| 113 | + if ($span === null) |
|
| 114 | + { |
|
| 112 | 115 | /** @var TracerInterface $tracer */ |
| 113 | 116 | $tracer = $this->container->get(TracerInterface::class); |
| 114 | 117 | return $tracer->trace( |
@@ -126,7 +129,9 @@ discard block |
||
| 126 | 129 | $span->setAttribute('http.middleware', $middlewares); |
| 127 | 130 | |
| 128 | 131 | return $middleware->process($request, $this->next($span)); |
| 129 | - } finally { |
|
| 132 | + } |
|
| 133 | + finally |
|
| 134 | + { |
|
| 130 | 135 | $currentRequest->set($previousRequest); |
| 131 | 136 | } |
| 132 | 137 | } |
@@ -35,11 +35,15 @@ discard block |
||
| 35 | 35 | ?TracerFactoryInterface $tracerFactory = null, |
| 36 | 36 | private readonly ?EventDispatcherInterface $dispatcher = null, |
| 37 | 37 | ) { |
| 38 | - if ($pipeline instanceof Pipeline) { |
|
| 39 | - foreach ($this->config->getMiddleware() as $middleware) { |
|
| 38 | + if ($pipeline instanceof Pipeline) |
|
| 39 | + { |
|
| 40 | + foreach ($this->config->getMiddleware() as $middleware) |
|
| 41 | + { |
|
| 40 | 42 | $pipeline->pushMiddleware($this->container->get($middleware)); |
| 41 | 43 | } |
| 42 | - } else { |
|
| 44 | + } |
|
| 45 | + else |
|
| 46 | + { |
|
| 43 | 47 | $pipeline = $pipeline->withAddedMiddleware( |
| 44 | 48 | ...$this->config->getMiddleware() |
| 45 | 49 | ); |
@@ -77,7 +81,8 @@ discard block |
||
| 77 | 81 | |
| 78 | 82 | $this->dispatcher?->dispatch(new RequestReceived($request)); |
| 79 | 83 | |
| 80 | - if ($this->handler === null) { |
|
| 84 | + if ($this->handler === null) |
|
| 85 | + { |
|
| 81 | 86 | throw new HttpException('Unable to run HttpCore, no handler is set.'); |
| 82 | 87 | } |
| 83 | 88 | |
@@ -117,7 +122,8 @@ discard block |
||
| 117 | 122 | traceKind: TraceKind::SERVER, |
| 118 | 123 | ); |
| 119 | 124 | |
| 120 | - foreach ($tracer->getContext() as $key => $value) { |
|
| 125 | + foreach ($tracer->getContext() as $key => $value) |
|
| 126 | + { |
|
| 121 | 127 | $response = $response->withHeader($key, $value); |
| 122 | 128 | } |
| 123 | 129 | |
@@ -44,16 +44,19 @@ discard block |
||
| 44 | 44 | $route = clone $this; |
| 45 | 45 | |
| 46 | 46 | // array fallback |
| 47 | - if (\count($middleware) === 1 && \is_array($middleware[0])) { |
|
| 47 | + if (\count($middleware) === 1 && \is_array($middleware[0])) |
|
| 48 | + { |
|
| 48 | 49 | $middleware = $middleware[0]; |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | 52 | /** @var MiddlewareType[] $middleware */ |
| 52 | - foreach ($middleware as $item) { |
|
| 53 | + foreach ($middleware as $item) |
|
| 54 | + { |
|
| 53 | 55 | $route->middleware[] = $item; |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | - if ($route->pipeline !== null) { |
|
| 58 | + if ($route->pipeline !== null) |
|
| 59 | + { |
|
| 57 | 60 | $route->pipeline = $route->makeLazyPipeline(); |
| 58 | 61 | } |
| 59 | 62 | |
@@ -80,11 +83,14 @@ discard block |
||
| 80 | 83 | protected function makePipeline(): Pipeline |
| 81 | 84 | { |
| 82 | 85 | \assert($this->container !== null); |
| 83 | - try { |
|
| 86 | + try |
|
| 87 | + { |
|
| 84 | 88 | return $this->container |
| 85 | 89 | ->get(PipelineFactory::class) |
| 86 | 90 | ->createWithMiddleware($this->middleware); |
| 87 | - } catch (ContainerExceptionInterface $e) { |
|
| 91 | + } |
|
| 92 | + catch (ContainerExceptionInterface $e) |
|
| 93 | + { |
|
| 88 | 94 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 89 | 95 | } |
| 90 | 96 | } |
@@ -97,11 +103,14 @@ discard block |
||
| 97 | 103 | protected function makeLazyPipeline(): LazyPipeline |
| 98 | 104 | { |
| 99 | 105 | \assert($this->container !== null); |
| 100 | - try { |
|
| 106 | + try |
|
| 107 | + { |
|
| 101 | 108 | /** @var LazyPipeline $pipeline */ |
| 102 | 109 | $pipeline = $this->container->get(LazyPipeline::class); |
| 103 | 110 | return $pipeline->withMiddleware(...$this->middleware); |
| 104 | - } catch (ContainerExceptionInterface $e) { |
|
| 111 | + } |
|
| 112 | + catch (ContainerExceptionInterface $e) |
|
| 113 | + { |
|
| 105 | 114 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 106 | 115 | } |
| 107 | 116 | } |
@@ -69,7 +69,8 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function withUriHandler(UriHandler $uriHandler): static |
| 71 | 71 | { |
| 72 | - if ($this->target instanceof TargetInterface) { |
|
| 72 | + if ($this->target instanceof TargetInterface) |
|
| 73 | + { |
|
| 73 | 74 | $uriHandler = $uriHandler->withConstrains( |
| 74 | 75 | $this->target->getConstrains(), |
| 75 | 76 | $this->defaults, |
@@ -87,7 +88,8 @@ discard block |
||
| 87 | 88 | $route = clone $this; |
| 88 | 89 | $route->container = $container; |
| 89 | 90 | |
| 90 | - if ($route->target instanceof TargetInterface) { |
|
| 91 | + if ($route->target instanceof TargetInterface) |
|
| 92 | + { |
|
| 91 | 93 | $route->target = clone $route->target; |
| 92 | 94 | } |
| 93 | 95 | |
@@ -114,7 +116,8 @@ discard block |
||
| 114 | 116 | */ |
| 115 | 117 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 116 | 118 | { |
| 117 | - if (empty($this->requestHandler)) { |
|
| 119 | + if (empty($this->requestHandler)) |
|
| 120 | + { |
|
| 118 | 121 | $this->requestHandler = $this->requestHandler(); |
| 119 | 122 | } |
| 120 | 123 | |
@@ -134,25 +137,32 @@ discard block |
||
| 134 | 137 | 'Unable to configure route pipeline without associated container.', |
| 135 | 138 | ); |
| 136 | 139 | |
| 137 | - if ($this->target instanceof TargetInterface) { |
|
| 138 | - try { |
|
| 140 | + if ($this->target instanceof TargetInterface) |
|
| 141 | + { |
|
| 142 | + try |
|
| 143 | + { |
|
| 139 | 144 | \assert($this->matches !== null); |
| 140 | 145 | return $this->target->getHandler($this->container, $this->matches); |
| 141 | - } catch (TargetException $e) { |
|
| 146 | + } |
|
| 147 | + catch (TargetException $e) |
|
| 148 | + { |
|
| 142 | 149 | throw new RouteException('Invalid target resolution', $e->getCode(), $e); |
| 143 | 150 | } |
| 144 | 151 | } |
| 145 | 152 | |
| 146 | - if ($this->target instanceof RequestHandlerInterface) { |
|
| 153 | + if ($this->target instanceof RequestHandlerInterface) |
|
| 154 | + { |
|
| 147 | 155 | return $this->target; |
| 148 | 156 | } |
| 149 | 157 | |
| 150 | - try { |
|
| 158 | + try |
|
| 159 | + { |
|
| 151 | 160 | $target = \is_string($this->target) |
| 152 | 161 | ? $this->container->get($this->target) |
| 153 | 162 | : $this->target; |
| 154 | 163 | |
| 155 | - if ($target instanceof RequestHandlerInterface) { |
|
| 164 | + if ($target instanceof RequestHandlerInterface) |
|
| 165 | + { |
|
| 156 | 166 | return $target; |
| 157 | 167 | } |
| 158 | 168 | |
@@ -160,7 +170,9 @@ discard block |
||
| 160 | 170 | $target, |
| 161 | 171 | $this->container->get(ResponseFactoryInterface::class) |
| 162 | 172 | ); |
| 163 | - } catch (ContainerExceptionInterface $e) { |
|
| 173 | + } |
|
| 174 | + catch (ContainerExceptionInterface $e) |
|
| 175 | + { |
|
| 164 | 176 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
| 165 | 177 | } |
| 166 | 178 | } |
@@ -28,33 +28,43 @@ |
||
| 28 | 28 | final protected function runScope(Span $span, callable $callback): mixed |
| 29 | 29 | { |
| 30 | 30 | $container = ContainerScope::getContainer(); |
| 31 | - if ($container === null) { |
|
| 31 | + if ($container === null) |
|
| 32 | + { |
|
| 32 | 33 | return $this->scope->runScope([ |
| 33 | 34 | SpanInterface::class => $span, |
| 34 | 35 | TracerInterface::class => $this, |
| 35 | 36 | ], static fn (InvokerInterface $invoker): mixed => $invoker->invoke($callback)); |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | - if ($container instanceof Container) { |
|
| 39 | + if ($container instanceof Container) |
|
| 40 | + { |
|
| 39 | 41 | $invoker = $container; |
| 40 | 42 | $binder = $container; |
| 41 | - } else { |
|
| 43 | + } |
|
| 44 | + else |
|
| 45 | + { |
|
| 42 | 46 | /** @var InvokerInterface $invoker */ |
| 43 | 47 | $invoker = $container->get(InvokerInterface::class); |
| 44 | 48 | /** @var BinderInterface $binder */ |
| 45 | 49 | $binder = $container->get(BinderInterface::class); |
| 46 | 50 | } |
| 47 | 51 | |
| 48 | - try { |
|
| 52 | + try |
|
| 53 | + { |
|
| 49 | 54 | $prevSpan = $container->get(SpanInterface::class); |
| 50 | - } catch (\Throwable) { |
|
| 55 | + } |
|
| 56 | + catch (\Throwable) |
|
| 57 | + { |
|
| 51 | 58 | $prevSpan = null; |
| 52 | 59 | } |
| 53 | 60 | |
| 54 | 61 | $binder->bindSingleton(SpanInterface::class, $span); |
| 55 | - try { |
|
| 62 | + try |
|
| 63 | + { |
|
| 56 | 64 | return $invoker->invoke($callback); |
| 57 | - } finally { |
|
| 65 | + } |
|
| 66 | + finally |
|
| 67 | + { |
|
| 58 | 68 | $prevSpan === null |
| 59 | 69 | ? $binder->removeBinding(SpanInterface::class) |
| 60 | 70 | : $binder->bindSingleton(SpanInterface::class, $prevSpan); |
@@ -77,7 +77,8 @@ |
||
| 77 | 77 | ->with(SpanInterface::class); |
| 78 | 78 | $scope->shouldNotReceive('runScope'); |
| 79 | 79 | |
| 80 | - ContainerScope::runScope($container, function () use ($tracer, $callable) { |
|
| 80 | + ContainerScope::runScope($container, function () use ($tracer, $callable) |
|
| 81 | + { |
|
| 81 | 82 | $this->assertSame( |
| 82 | 83 | 'hello', |
| 83 | 84 | $tracer->trace('foo', $callable, ['foo' => 'bar']) |