@@ -21,7 +21,8 @@ |
||
| 21 | 21 | ?string $clientFilename = null, |
| 22 | 22 | ?string $clientMediaType = null |
| 23 | 23 | ): UploadedFileInterface { |
| 24 | - if ($size === null) { |
|
| 24 | + if ($size === null) |
|
| 25 | + { |
|
| 25 | 26 | $size = $stream->getSize(); |
| 26 | 27 | } |
| 27 | 28 | |
@@ -67,14 +67,19 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | return $this->tracer->trace( |
| 69 | 69 | name: 'Routing', |
| 70 | - callback: function (SpanInterface $span) use ($request) { |
|
| 71 | - try { |
|
| 70 | + callback: function (SpanInterface $span) use ($request) |
|
| 71 | + { |
|
| 72 | + try |
|
| 73 | + { |
|
| 72 | 74 | $route = $this->matchRoute($request, $routeName); |
| 73 | - } catch (RouteException $e) { |
|
| 75 | + } |
|
| 76 | + catch (RouteException $e) |
|
| 77 | + { |
|
| 74 | 78 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 75 | 79 | } |
| 76 | 80 | |
| 77 | - if ($route === null) { |
|
| 81 | + if ($route === null) |
|
| 82 | + { |
|
| 78 | 83 | $this->eventDispatcher?->dispatch(new RouteNotFound($request)); |
| 79 | 84 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 85 | } |
@@ -109,7 +114,8 @@ discard block |
||
| 109 | 114 | |
| 110 | 115 | public function getRoute(string $name): RouteInterface |
| 111 | 116 | { |
| 112 | - if (isset($this->routes[$name])) { |
|
| 117 | + if (isset($this->routes[$name])) |
|
| 118 | + { |
|
| 113 | 119 | return $this->routes[$name]; |
| 114 | 120 | } |
| 115 | 121 | |
@@ -118,7 +124,8 @@ discard block |
||
| 118 | 124 | |
| 119 | 125 | public function getRoutes(): array |
| 120 | 126 | { |
| 121 | - if (!empty($this->default)) { |
|
| 127 | + if (!empty($this->default)) |
|
| 128 | + { |
|
| 122 | 129 | return $this->routes + [null => $this->default]; |
| 123 | 130 | } |
| 124 | 131 | |
@@ -127,9 +134,12 @@ discard block |
||
| 127 | 134 | |
| 128 | 135 | public function uri(string $route, iterable $parameters = []): UriInterface |
| 129 | 136 | { |
| 130 | - try { |
|
| 137 | + try |
|
| 138 | + { |
|
| 131 | 139 | return $this->getRoute($route)->uri($parameters); |
| 132 | - } catch (UndefinedRouteException) { |
|
| 140 | + } |
|
| 141 | + catch (UndefinedRouteException) |
|
| 142 | + { |
|
| 133 | 143 | //In some cases route name can be provided as controller:action pair, we can try to |
| 134 | 144 | //generate such route automatically based on our default/fallback route |
| 135 | 145 | return $this->castRoute($route)->uri($parameters); |
@@ -141,9 +151,11 @@ discard block |
||
| 141 | 151 | /** @var GroupRegistry $groups */ |
| 142 | 152 | $groups = $this->container->get(GroupRegistry::class); |
| 143 | 153 | |
| 144 | - foreach ($routes->getCollection() as $name => $configurator) { |
|
| 154 | + foreach ($routes->getCollection() as $name => $configurator) |
|
| 155 | + { |
|
| 145 | 156 | $target = $configurator->target; |
| 146 | - if ($configurator->core !== null && $target instanceof AbstractTarget) { |
|
| 157 | + if ($configurator->core !== null && $target instanceof AbstractTarget) |
|
| 158 | + { |
|
| 147 | 159 | $target = $target->withCore($configurator->core); |
| 148 | 160 | } |
| 149 | 161 | |
@@ -152,21 +164,25 @@ discard block |
||
| 152 | 164 | : \ltrim($configurator->pattern, '/'); |
| 153 | 165 | $route = new Route($pattern, $target, $configurator->defaults); |
| 154 | 166 | |
| 155 | - if ($configurator->middleware !== null) { |
|
| 167 | + if ($configurator->middleware !== null) |
|
| 168 | + { |
|
| 156 | 169 | $route = $route->withMiddleware(...$configurator->middleware); |
| 157 | 170 | } |
| 158 | 171 | |
| 159 | - if ($configurator->methods !== null) { |
|
| 172 | + if ($configurator->methods !== null) |
|
| 173 | + { |
|
| 160 | 174 | $route = $route->withVerbs(...$configurator->methods); |
| 161 | 175 | } |
| 162 | 176 | |
| 163 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 177 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 178 | + { |
|
| 164 | 179 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 165 | 180 | $group->setPrefix($configurator->prefix); |
| 166 | 181 | $group->addRoute($name, $route); |
| 167 | 182 | } |
| 168 | 183 | |
| 169 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 184 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 185 | + { |
|
| 170 | 186 | $this->setDefault($route); |
| 171 | 187 | } |
| 172 | 188 | } |
@@ -177,17 +193,20 @@ discard block |
||
| 177 | 193 | */ |
| 178 | 194 | protected function matchRoute(ServerRequestInterface $request, ?string &$routeName = null): ?RouteInterface |
| 179 | 195 | { |
| 180 | - foreach ($this->routes as $name => $route) { |
|
| 196 | + foreach ($this->routes as $name => $route) |
|
| 197 | + { |
|
| 181 | 198 | // Matched route will return new route instance with matched parameters |
| 182 | 199 | $matched = $route->match($request); |
| 183 | 200 | |
| 184 | - if ($matched !== null) { |
|
| 201 | + if ($matched !== null) |
|
| 202 | + { |
|
| 185 | 203 | $routeName = $name; |
| 186 | 204 | return $matched; |
| 187 | 205 | } |
| 188 | 206 | } |
| 189 | 207 | |
| 190 | - if ($this->default !== null) { |
|
| 208 | + if ($this->default !== null) |
|
| 209 | + { |
|
| 191 | 210 | return $this->default->match($request); |
| 192 | 211 | } |
| 193 | 212 | |
@@ -200,14 +219,18 @@ discard block |
||
| 200 | 219 | */ |
| 201 | 220 | protected function configure(RouteInterface $route): RouteInterface |
| 202 | 221 | { |
| 203 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 222 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()) |
|
| 223 | + { |
|
| 204 | 224 | // isolating route in a given container |
| 205 | 225 | $route = $route->withContainer($this->container); |
| 206 | 226 | } |
| 207 | 227 | |
| 208 | - try { |
|
| 228 | + try |
|
| 229 | + { |
|
| 209 | 230 | $uriHandler = $route->getUriHandler(); |
| 210 | - } catch (\Throwable) { |
|
| 231 | + } |
|
| 232 | + catch (\Throwable) |
|
| 233 | + { |
|
| 211 | 234 | $uriHandler = $this->uriHandler; |
| 212 | 235 | } |
| 213 | 236 | |
@@ -239,11 +262,16 @@ discard block |
||
| 239 | 262 | /** |
| 240 | 263 | * @var Matches $matches |
| 241 | 264 | */ |
| 242 | - if (!empty($matches['name'])) { |
|
| 265 | + if (!empty($matches['name'])) |
|
| 266 | + { |
|
| 243 | 267 | $routeObject = $this->getRoute($matches['name']); |
| 244 | - } elseif ($this->default !== null) { |
|
| 268 | + } |
|
| 269 | + elseif ($this->default !== null) |
|
| 270 | + { |
|
| 245 | 271 | $routeObject = $this->default; |
| 246 | - } else { |
|
| 272 | + } |
|
| 273 | + else |
|
| 274 | + { |
|
| 247 | 275 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 248 | 276 | } |
| 249 | 277 | |
@@ -16,15 +16,18 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | public function __construct(array $loaders = []) |
| 18 | 18 | { |
| 19 | - foreach ($loaders as $loader) { |
|
| 19 | + foreach ($loaders as $loader) |
|
| 20 | + { |
|
| 20 | 21 | $this->addLoader($loader); |
| 21 | 22 | } |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | 25 | public function resolve(mixed $resource, ?string $type = null): LoaderInterface|false |
| 25 | 26 | { |
| 26 | - foreach ($this->loaders as $loader) { |
|
| 27 | - if ($loader->supports($resource, $type)) { |
|
| 27 | + foreach ($this->loaders as $loader) |
|
| 28 | + { |
|
| 29 | + if ($loader->supports($resource, $type)) |
|
| 30 | + { |
|
| 28 | 31 | return $loader; |
| 29 | 32 | } |
| 30 | 33 | } |
@@ -15,7 +15,8 @@ |
||
| 15 | 15 | |
| 16 | 16 | public function load(mixed $resource, ?string $type = null): mixed |
| 17 | 17 | { |
| 18 | - if (false === $loader = $this->registry->resolve($resource, $type)) { |
|
| 18 | + if (false === $loader = $this->registry->resolve($resource, $type)) |
|
| 19 | + { |
|
| 19 | 20 | throw new LoaderLoadException(\sprintf('Loader for type [%s] not found.', $type ?? '')); |
| 20 | 21 | } |
| 21 | 22 | |
@@ -24,7 +24,8 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | public function load(mixed $resource, ?string $type = null): RouteCollection |
| 26 | 26 | { |
| 27 | - if (!\file_exists($resource)) { |
|
| 27 | + if (!\file_exists($resource)) |
|
| 28 | + { |
|
| 28 | 29 | throw new LoaderLoadException(\sprintf('File [%s] does not exist.', $resource)); |
| 29 | 30 | } |
| 30 | 31 | |
@@ -24,21 +24,30 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | public function createInjection(ReflectionClass $class, ?string $context = null): CacheInterface |
| 26 | 26 | { |
| 27 | - try { |
|
| 28 | - if ($context === null) { |
|
| 27 | + try |
|
| 28 | + { |
|
| 29 | + if ($context === null) |
|
| 30 | + { |
|
| 29 | 31 | $connection = $this->provider->storage(); |
| 30 | - } else { |
|
| 32 | + } |
|
| 33 | + else |
|
| 34 | + { |
|
| 31 | 35 | // Get Cache by context |
| 32 | - try { |
|
| 36 | + try |
|
| 37 | + { |
|
| 33 | 38 | $connection = $this->provider->storage($context); |
| 34 | - } catch (InvalidArgumentException) { |
|
| 39 | + } |
|
| 40 | + catch (InvalidArgumentException) |
|
| 41 | + { |
|
| 35 | 42 | // Case when context doesn't match to configured connections |
| 36 | 43 | return $this->provider->storage(); |
| 37 | 44 | } |
| 38 | 45 | } |
| 39 | 46 | |
| 40 | 47 | $this->matchType($class, $context, $connection); |
| 41 | - } catch (\Throwable $e) { |
|
| 48 | + } |
|
| 49 | + catch (\Throwable $e) |
|
| 50 | + { |
|
| 42 | 51 | throw new ContainerException(sprintf("Can't inject the required cache. %s", $e->getMessage()), 0, $e); |
| 43 | 52 | } |
| 44 | 53 | |
@@ -52,11 +61,13 @@ discard block |
||
| 52 | 61 | */ |
| 53 | 62 | private function matchType(ReflectionClass $class, ?string $context, CacheInterface $connection): void |
| 54 | 63 | { |
| 55 | - if ($connection::class === CacheRepository::class) { |
|
| 64 | + if ($connection::class === CacheRepository::class) |
|
| 65 | + { |
|
| 56 | 66 | $connection = $connection->getStorage(); |
| 57 | 67 | } |
| 58 | 68 | $className = $class->getName(); |
| 59 | - if ($className !== CacheInterface::class && !$connection instanceof $className) { |
|
| 69 | + if ($className !== CacheInterface::class && !$connection instanceof $className) |
|
| 70 | + { |
|
| 60 | 71 | throw new \RuntimeException( |
| 61 | 72 | \sprintf( |
| 62 | 73 | "The cache obtained by the context `%s` doesn't match the type `%s`.", |
@@ -32,13 +32,15 @@ discard block |
||
| 32 | 32 | */ |
| 33 | 33 | protected function say(string $string, array $options = [], ?string $bundle = null): string |
| 34 | 34 | { |
| 35 | - if (Translator::isMessage($string)) { |
|
| 35 | + if (Translator::isMessage($string)) |
|
| 36 | + { |
|
| 36 | 37 | //Cut [[ and ]] |
| 37 | 38 | $string = \substr($string, 2, -2); |
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | $container = ContainerScope::getContainer(); |
| 41 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 42 | + if (empty($container) || !$container->has(TranslatorInterface::class)) |
|
| 43 | + { |
|
| 42 | 44 | return Translator::interpolate($string, $options); |
| 43 | 45 | } |
| 44 | 46 | |
@@ -47,7 +49,8 @@ discard block |
||
| 47 | 49 | */ |
| 48 | 50 | $translator = $container->get(TranslatorInterface::class); |
| 49 | 51 | |
| 50 | - if (\is_null($bundle)) { |
|
| 52 | + if (\is_null($bundle)) |
|
| 53 | + { |
|
| 51 | 54 | $bundle = $translator->getDomain(static::class); |
| 52 | 55 | } |
| 53 | 56 | |
@@ -39,7 +39,8 @@ discard block |
||
| 39 | 39 | |
| 40 | 40 | public function getExtension(): ?string |
| 41 | 41 | { |
| 42 | - if ($this->parser !== null) { |
|
| 42 | + if ($this->parser !== null) |
|
| 43 | + { |
|
| 43 | 44 | return $this->parser->getExtension(); |
| 44 | 45 | } |
| 45 | 46 | |
@@ -52,22 +53,27 @@ discard block |
||
| 52 | 53 | */ |
| 53 | 54 | public function exists(string $path, ?string &$filename = null, ?ViewPath &$parsed = null): bool |
| 54 | 55 | { |
| 55 | - if (empty($this->parser)) { |
|
| 56 | + if (empty($this->parser)) |
|
| 57 | + { |
|
| 56 | 58 | throw new LoaderException('Unable to locate view source, no extension has been associated.'); |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | 61 | $parsed = $this->parser->parse($path); |
| 60 | - if ($parsed === null) { |
|
| 62 | + if ($parsed === null) |
|
| 63 | + { |
|
| 61 | 64 | return false; |
| 62 | 65 | } |
| 63 | 66 | |
| 64 | - if (!isset($this->namespaces[$parsed->getNamespace()])) { |
|
| 67 | + if (!isset($this->namespaces[$parsed->getNamespace()])) |
|
| 68 | + { |
|
| 65 | 69 | return false; |
| 66 | 70 | } |
| 67 | 71 | |
| 68 | - foreach ((array)$this->namespaces[$parsed->getNamespace()] as $directory) { |
|
| 72 | + foreach ((array)$this->namespaces[$parsed->getNamespace()] as $directory) |
|
| 73 | + { |
|
| 69 | 74 | $directory = $this->files->normalizePath($directory, true); |
| 70 | - if ($this->files->exists(\sprintf('%s%s', $directory, $parsed->getBasename()))) { |
|
| 75 | + if ($this->files->exists(\sprintf('%s%s', $directory, $parsed->getBasename()))) |
|
| 76 | + { |
|
| 71 | 77 | $filename = \sprintf('%s%s', $directory, $parsed->getBasename()); |
| 72 | 78 | |
| 73 | 79 | return true; |
@@ -79,7 +85,8 @@ discard block |
||
| 79 | 85 | |
| 80 | 86 | public function load(string $path): ViewSource |
| 81 | 87 | { |
| 82 | - if (!$this->exists($path, $filename, $parsed)) { |
|
| 88 | + if (!$this->exists($path, $filename, $parsed)) |
|
| 89 | + { |
|
| 83 | 90 | $this->dispatcher?->dispatch(new ViewNotFound($path)); |
| 84 | 91 | |
| 85 | 92 | throw new LoaderException(\sprintf('Unable to load view `%s`, file does not exist.', $path)); |
@@ -94,21 +101,27 @@ discard block |
||
| 94 | 101 | |
| 95 | 102 | public function list(?string $namespace = null): array |
| 96 | 103 | { |
| 97 | - if (empty($this->parser)) { |
|
| 104 | + if (empty($this->parser)) |
|
| 105 | + { |
|
| 98 | 106 | throw new LoaderException('Unable to list view sources, no extension has been associated.'); |
| 99 | 107 | } |
| 100 | 108 | |
| 101 | 109 | $result = []; |
| 102 | - foreach ($this->namespaces as $ns => $directories) { |
|
| 103 | - if (!empty($namespace) && $namespace != $ns) { |
|
| 110 | + foreach ($this->namespaces as $ns => $directories) |
|
| 111 | + { |
|
| 112 | + if (!empty($namespace) && $namespace != $ns) |
|
| 113 | + { |
|
| 104 | 114 | continue; |
| 105 | 115 | } |
| 106 | 116 | |
| 107 | - foreach ((array)$directories as $directory) { |
|
| 117 | + foreach ((array)$directories as $directory) |
|
| 118 | + { |
|
| 108 | 119 | $files = $this->files->getFiles($directory); |
| 109 | 120 | |
| 110 | - foreach ($files as $filename) { |
|
| 111 | - if (!$this->parser->match($filename)) { |
|
| 121 | + foreach ($files as $filename) |
|
| 122 | + { |
|
| 123 | + if (!$this->parser->match($filename)) |
|
| 124 | + { |
|
| 112 | 125 | // does not belong to this loader |
| 113 | 126 | continue; |
| 114 | 127 | } |
@@ -12,7 +12,8 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | public function reset(?ContextInterface $context = null): void |
| 14 | 14 | { |
| 15 | - if (empty($context)) { |
|
| 15 | + if (empty($context)) |
|
| 16 | + { |
|
| 16 | 17 | $this->cache = []; |
| 17 | 18 | return; |
| 18 | 19 | } |
@@ -25,7 +26,8 @@ discard block |
||
| 25 | 26 | */ |
| 26 | 27 | public function resetPath(string $path): void |
| 27 | 28 | { |
| 28 | - foreach ($this->cache as &$cache) { |
|
| 29 | + foreach ($this->cache as &$cache) |
|
| 30 | + { |
|
| 29 | 31 | unset($cache[$path], $cache); |
| 30 | 32 | } |
| 31 | 33 | } |
@@ -45,7 +47,8 @@ discard block |
||
| 45 | 47 | */ |
| 46 | 48 | public function get(ContextInterface $context, string $path): ViewInterface |
| 47 | 49 | { |
| 48 | - if (!$this->has($context, $path)) { |
|
| 50 | + if (!$this->has($context, $path)) |
|
| 51 | + { |
|
| 49 | 52 | throw new CacheException(\sprintf('No cache is available for %s.', $path)); |
| 50 | 53 | } |
| 51 | 54 | |