@@ -83,13 +83,15 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 85 | 85 | { |
| 86 | - if (!$this->container instanceof \Psr\Container\ContainerInterface) { |
|
| 86 | + if (!$this->container instanceof \Psr\Container\ContainerInterface) |
|
| 87 | + { |
|
| 87 | 88 | throw new ScopeException('Container is not set'); |
| 88 | 89 | } |
| 89 | 90 | |
| 90 | 91 | $method = method_exists($this, 'perform') ? 'perform' : '__invoke'; |
| 91 | 92 | |
| 92 | - try { |
|
| 93 | + try |
|
| 94 | + { |
|
| 93 | 95 | [$this->input, $this->output] = [$this->prepareInput($input), $this->prepareOutput($input, $output)]; |
| 94 | 96 | |
| 95 | 97 | $this->eventDispatcher?->dispatch(new CommandStarting($this, $this->input, $this->output)); |
@@ -122,7 +124,9 @@ discard block |
||
| 122 | 124 | $this->eventDispatcher?->dispatch(new CommandFinished($this, $code, $this->input, $this->output)); |
| 123 | 125 | |
| 124 | 126 | return $code; |
| 125 | - } finally { |
|
| 127 | + } |
|
| 128 | + finally |
|
| 129 | + { |
|
| 126 | 130 | [$this->input, $this->output] = [null, null]; |
| 127 | 131 | } |
| 128 | 132 | } |
@@ -70,7 +70,8 @@ discard block |
||
| 70 | 70 | |
| 71 | 71 | $this->configureIO($input, $output); |
| 72 | 72 | |
| 73 | - if ($command !== null) { |
|
| 73 | + if ($command !== null) |
|
| 74 | + { |
|
| 74 | 75 | $input = new InputProxy($input, ['firstArgument' => $command]); |
| 75 | 76 | } |
| 76 | 77 | |
@@ -97,18 +98,21 @@ discard block |
||
| 97 | 98 | */ |
| 98 | 99 | public function getApplication(): Application |
| 99 | 100 | { |
| 100 | - if ($this->application instanceof \Symfony\Component\Console\Application) { |
|
| 101 | + if ($this->application instanceof \Symfony\Component\Console\Application) |
|
| 102 | + { |
|
| 101 | 103 | return $this->application; |
| 102 | 104 | } |
| 103 | 105 | |
| 104 | 106 | $this->application = new Application($this->config->getName(), $this->config->getVersion()); |
| 105 | 107 | $this->application->setCatchExceptions(false); |
| 106 | 108 | $this->application->setAutoExit(false); |
| 107 | - if ($this->dispatcher instanceof SymfonyEventDispatcherInterface) { |
|
| 109 | + if ($this->dispatcher instanceof SymfonyEventDispatcherInterface) |
|
| 110 | + { |
|
| 108 | 111 | $this->application->setDispatcher($this->dispatcher); |
| 109 | 112 | } |
| 110 | 113 | |
| 111 | - if ($this->locator instanceof \Spiral\Console\LocatorInterface) { |
|
| 114 | + if ($this->locator instanceof \Spiral\Console\LocatorInterface) |
|
| 115 | + { |
|
| 112 | 116 | $this->addCommands($this->locator->locateCommands()); |
| 113 | 117 | } |
| 114 | 118 | |
@@ -128,13 +132,16 @@ discard block |
||
| 128 | 132 | { |
| 129 | 133 | $interceptors = $this->config->getInterceptors(); |
| 130 | 134 | |
| 131 | - foreach ($commands as $command) { |
|
| 132 | - if ($command instanceof Command) { |
|
| 135 | + foreach ($commands as $command) |
|
| 136 | + { |
|
| 137 | + if ($command instanceof Command) |
|
| 138 | + { |
|
| 133 | 139 | $command->setContainer($this->container); |
| 134 | 140 | $command->setInterceptors($interceptors); |
| 135 | 141 | } |
| 136 | 142 | |
| 137 | - if ($this->dispatcher instanceof \Psr\EventDispatcher\EventDispatcherInterface && $command instanceof EventDispatcherAwareInterface) { |
|
| 143 | + if ($this->dispatcher instanceof \Psr\EventDispatcher\EventDispatcherInterface && $command instanceof EventDispatcherAwareInterface) |
|
| 144 | + { |
|
| 138 | 145 | $command->setEventDispatcher($this->dispatcher); |
| 139 | 146 | } |
| 140 | 147 | |
@@ -149,22 +156,30 @@ discard block |
||
| 149 | 156 | */ |
| 150 | 157 | private function configureIO(InputInterface $input, OutputInterface $output): void |
| 151 | 158 | { |
| 152 | - if ($input->hasParameterOption(['--ansi'], true)) { |
|
| 159 | + if ($input->hasParameterOption(['--ansi'], true)) |
|
| 160 | + { |
|
| 153 | 161 | $output->setDecorated(true); |
| 154 | - } elseif ($input->hasParameterOption(['--no-ansi'], true)) { |
|
| 162 | + } |
|
| 163 | + elseif ($input->hasParameterOption(['--no-ansi'], true)) |
|
| 164 | + { |
|
| 155 | 165 | $output->setDecorated(false); |
| 156 | 166 | } |
| 157 | 167 | |
| 158 | - if ($input->hasParameterOption(['--no-interaction', '-n'], true)) { |
|
| 168 | + if ($input->hasParameterOption(['--no-interaction', '-n'], true)) |
|
| 169 | + { |
|
| 159 | 170 | $input->setInteractive(false); |
| 160 | - } elseif (\function_exists('posix_isatty')) { |
|
| 171 | + } |
|
| 172 | + elseif (\function_exists('posix_isatty')) |
|
| 173 | + { |
|
| 161 | 174 | $inputStream = null; |
| 162 | 175 | |
| 163 | - if ($input instanceof StreamableInputInterface) { |
|
| 176 | + if ($input instanceof StreamableInputInterface) |
|
| 177 | + { |
|
| 164 | 178 | $inputStream = $input->getStream(); |
| 165 | 179 | } |
| 166 | 180 | |
| 167 | - if ($inputStream !== null && !@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) { |
|
| 181 | + if ($inputStream !== null && !@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) |
|
| 182 | + { |
|
| 168 | 183 | $input->setInteractive(false); |
| 169 | 184 | } |
| 170 | 185 | } |
@@ -177,10 +192,13 @@ discard block |
||
| 177 | 192 | default => $shellVerbosity = 0 |
| 178 | 193 | }; |
| 179 | 194 | |
| 180 | - if ($input->hasParameterOption(['--quiet', '-q'], true)) { |
|
| 195 | + if ($input->hasParameterOption(['--quiet', '-q'], true)) |
|
| 196 | + { |
|
| 181 | 197 | $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
| 182 | 198 | $shellVerbosity = -1; |
| 183 | - } else { |
|
| 199 | + } |
|
| 200 | + else |
|
| 201 | + { |
|
| 184 | 202 | if ( |
| 185 | 203 | $input->hasParameterOption('-vvv', true) |
| 186 | 204 | || $input->hasParameterOption('--verbose=3', true) |
@@ -188,14 +206,16 @@ discard block |
||
| 188 | 206 | ) { |
| 189 | 207 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
| 190 | 208 | $shellVerbosity = 3; |
| 191 | - } elseif ( |
|
| 209 | + } |
|
| 210 | + elseif ( |
|
| 192 | 211 | $input->hasParameterOption('-vv', true) |
| 193 | 212 | || $input->hasParameterOption('--verbose=2', true) |
| 194 | 213 | || 2 === $input->getParameterOption('--verbose', false, true) |
| 195 | 214 | ) { |
| 196 | 215 | $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); |
| 197 | 216 | $shellVerbosity = 2; |
| 198 | - } elseif ( |
|
| 217 | + } |
|
| 218 | + elseif ( |
|
| 199 | 219 | $input->hasParameterOption('-v', true) |
| 200 | 220 | || $input->hasParameterOption('--verbose=1', true) |
| 201 | 221 | || $input->hasParameterOption('--verbose', true) |
@@ -206,7 +226,8 @@ discard block |
||
| 206 | 226 | } |
| 207 | 227 | } |
| 208 | 228 | |
| 209 | - if (-1 === $shellVerbosity) { |
|
| 229 | + if (-1 === $shellVerbosity) |
|
| 230 | + { |
|
| 210 | 231 | $input->setInteractive(false); |
| 211 | 232 | } |
| 212 | 233 | |
@@ -37,7 +37,8 @@ discard block |
||
| 37 | 37 | { |
| 38 | 38 | $attribute = $this->reader->firstClassMetadata($reflection, AsCommand::class); |
| 39 | 39 | |
| 40 | - if ($attribute === null) { |
|
| 40 | + if ($attribute === null) |
|
| 41 | + { |
|
| 41 | 42 | $attribute = $reflection->getAttributes(SymfonyAsCommand::class)[0]->newInstance(); |
| 42 | 43 | } |
| 43 | 44 | |
@@ -54,13 +55,16 @@ discard block |
||
| 54 | 55 | { |
| 55 | 56 | $reflection = new \ReflectionClass($command); |
| 56 | 57 | |
| 57 | - foreach ($reflection->getProperties() as $property) { |
|
| 58 | + foreach ($reflection->getProperties() as $property) |
|
| 59 | + { |
|
| 58 | 60 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 59 | - if ($attribute === null) { |
|
| 61 | + if ($attribute === null) |
|
| 62 | + { |
|
| 60 | 63 | continue; |
| 61 | 64 | } |
| 62 | 65 | |
| 63 | - if ($input->hasArgument($attribute->name ?? $property->getName())) { |
|
| 66 | + if ($input->hasArgument($attribute->name ?? $property->getName())) |
|
| 67 | + { |
|
| 64 | 68 | $property->setValue( |
| 65 | 69 | $command, |
| 66 | 70 | $this->typecast($input->getArgument($attribute->name ?? $property->getName()), $property) |
@@ -68,16 +72,20 @@ discard block |
||
| 68 | 72 | } |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | - foreach ($reflection->getProperties() as $property) { |
|
| 75 | + foreach ($reflection->getProperties() as $property) |
|
| 76 | + { |
|
| 72 | 77 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 73 | - if ($attribute === null) { |
|
| 78 | + if ($attribute === null) |
|
| 79 | + { |
|
| 74 | 80 | continue; |
| 75 | 81 | } |
| 76 | 82 | |
| 77 | - if ($input->hasOption($attribute->name ?? $property->getName())) { |
|
| 83 | + if ($input->hasOption($attribute->name ?? $property->getName())) |
|
| 84 | + { |
|
| 78 | 85 | $value = $this->typecast($input->getOption($attribute->name ?? $property->getName()), $property); |
| 79 | 86 | |
| 80 | - if ($value !== null || $this->getPropertyType($property)->allowsNull()) { |
|
| 87 | + if ($value !== null || $this->getPropertyType($property)->allowsNull()) |
|
| 88 | + { |
|
| 81 | 89 | $property->setValue($command, $value); |
| 82 | 90 | } |
| 83 | 91 | } |
@@ -88,9 +96,11 @@ discard block |
||
| 88 | 96 | { |
| 89 | 97 | $result = []; |
| 90 | 98 | $arrayArgument = null; |
| 91 | - foreach ($reflection->getProperties() as $property) { |
|
| 99 | + foreach ($reflection->getProperties() as $property) |
|
| 100 | + { |
|
| 92 | 101 | $attribute = $this->reader->firstPropertyMetadata($property, Argument::class); |
| 93 | - if ($attribute === null) { |
|
| 102 | + if ($attribute === null) |
|
| 103 | + { |
|
| 94 | 104 | continue; |
| 95 | 105 | } |
| 96 | 106 | |
@@ -113,19 +123,22 @@ discard block |
||
| 113 | 123 | suggestedValues: $attribute->suggestedValues |
| 114 | 124 | ); |
| 115 | 125 | |
| 116 | - if ($arrayArgument instanceof \Symfony\Component\Console\Input\InputArgument && $isArray) { |
|
| 126 | + if ($arrayArgument instanceof \Symfony\Component\Console\Input\InputArgument && $isArray) |
|
| 127 | + { |
|
| 117 | 128 | throw new ConfiguratorException('There must be only one array argument!'); |
| 118 | 129 | } |
| 119 | 130 | |
| 120 | 131 | // It must be used at the end of the argument list. |
| 121 | - if ($isArray) { |
|
| 132 | + if ($isArray) |
|
| 133 | + { |
|
| 122 | 134 | $arrayArgument = $argument; |
| 123 | 135 | continue; |
| 124 | 136 | } |
| 125 | 137 | $result[] = $argument; |
| 126 | 138 | } |
| 127 | 139 | |
| 128 | - if ($arrayArgument instanceof \Symfony\Component\Console\Input\InputArgument) { |
|
| 140 | + if ($arrayArgument instanceof \Symfony\Component\Console\Input\InputArgument) |
|
| 141 | + { |
|
| 129 | 142 | $result[] = $arrayArgument; |
| 130 | 143 | } |
| 131 | 144 | |
@@ -135,21 +148,26 @@ discard block |
||
| 135 | 148 | private function parseOptions(\ReflectionClass $reflection): array |
| 136 | 149 | { |
| 137 | 150 | $result = []; |
| 138 | - foreach ($reflection->getProperties() as $property) { |
|
| 151 | + foreach ($reflection->getProperties() as $property) |
|
| 152 | + { |
|
| 139 | 153 | $attribute = $this->reader->firstPropertyMetadata($property, Option::class); |
| 140 | - if ($attribute === null) { |
|
| 154 | + if ($attribute === null) |
|
| 155 | + { |
|
| 141 | 156 | continue; |
| 142 | 157 | } |
| 143 | 158 | |
| 144 | 159 | $type = $this->getPropertyType($property); |
| 145 | 160 | $mode = $attribute->mode; |
| 146 | 161 | |
| 147 | - if ($mode === null) { |
|
| 162 | + if ($mode === null) |
|
| 163 | + { |
|
| 148 | 164 | $mode = $this->guessOptionMode($type, $property); |
| 149 | 165 | } |
| 150 | 166 | |
| 151 | - if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) { |
|
| 152 | - if ($type->getName() !== 'bool') { |
|
| 167 | + if ($mode === InputOption::VALUE_NONE || $mode === InputOption::VALUE_NEGATABLE) |
|
| 168 | + { |
|
| 169 | + if ($type->getName() !== 'bool') |
|
| 170 | + { |
|
| 153 | 171 | throw new ConfiguratorException( |
| 154 | 172 | 'Options properties with mode `VALUE_NONE` or `VALUE_NEGATABLE` must be bool!' |
| 155 | 173 | ); |
@@ -175,17 +193,22 @@ discard block |
||
| 175 | 193 | { |
| 176 | 194 | $type = $property->hasType() ? $property->getType() : null; |
| 177 | 195 | |
| 178 | - if (!$type instanceof \ReflectionNamedType || $value === null) { |
|
| 196 | + if (!$type instanceof \ReflectionNamedType || $value === null) |
|
| 197 | + { |
|
| 179 | 198 | return $value; |
| 180 | 199 | } |
| 181 | 200 | |
| 182 | - if (!$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 183 | - try { |
|
| 201 | + if (!$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 202 | + { |
|
| 203 | + try |
|
| 204 | + { |
|
| 184 | 205 | /** @var class-string<\BackedEnum> $enum */ |
| 185 | 206 | $enum = $type->getName(); |
| 186 | 207 | |
| 187 | 208 | return $enum::from($value); |
| 188 | - } catch (\Throwable) { |
|
| 209 | + } |
|
| 210 | + catch (\Throwable) |
|
| 211 | + { |
|
| 189 | 212 | throw new ConfiguratorException(\sprintf('Wrong option value. Allowed options: `%s`.', \implode( |
| 190 | 213 | '`, `', |
| 191 | 214 | \array_map(static fn (\BackedEnum $item): string => (string) $item->value, $enum::cases()) |
@@ -205,7 +228,8 @@ discard block |
||
| 205 | 228 | |
| 206 | 229 | private function getPropertyType(\ReflectionProperty $property): \ReflectionNamedType |
| 207 | 230 | { |
| 208 | - if (!$property->hasType()) { |
|
| 231 | + if (!$property->hasType()) |
|
| 232 | + { |
|
| 209 | 233 | throw new ConfiguratorException( |
| 210 | 234 | \sprintf('Please, specify the type for the `%s` property!', $property->getName()) |
| 211 | 235 | ); |
@@ -213,23 +237,29 @@ discard block |
||
| 213 | 237 | |
| 214 | 238 | $type = $property->getType(); |
| 215 | 239 | |
| 216 | - if ($type instanceof \ReflectionIntersectionType) { |
|
| 240 | + if ($type instanceof \ReflectionIntersectionType) |
|
| 241 | + { |
|
| 217 | 242 | throw new ConfiguratorException(\sprintf('Invalid type for the `%s` property.', $property->getName())); |
| 218 | 243 | } |
| 219 | 244 | |
| 220 | - if ($type instanceof \ReflectionUnionType) { |
|
| 221 | - foreach ($type->getTypes() as $type) { |
|
| 222 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) { |
|
| 245 | + if ($type instanceof \ReflectionUnionType) |
|
| 246 | + { |
|
| 247 | + foreach ($type->getTypes() as $type) |
|
| 248 | + { |
|
| 249 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin()) |
|
| 250 | + { |
|
| 223 | 251 | return $type; |
| 224 | 252 | } |
| 225 | 253 | } |
| 226 | 254 | } |
| 227 | 255 | |
| 228 | - if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())) { |
|
| 256 | + if ($type instanceof \ReflectionNamedType && !$type->isBuiltin() && \enum_exists($type->getName())) |
|
| 257 | + { |
|
| 229 | 258 | return $type; |
| 230 | 259 | } |
| 231 | 260 | |
| 232 | - if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') { |
|
| 261 | + if ($type instanceof \ReflectionNamedType && $type->isBuiltin() && $type->getName() !== 'object') |
|
| 262 | + { |
|
| 233 | 263 | return $type; |
| 234 | 264 | } |
| 235 | 265 | |
@@ -43,7 +43,8 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function stream_eof(): bool |
| 45 | 45 | { |
| 46 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 46 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 47 | + { |
|
| 47 | 48 | throw new WrapperException('Stream is not opened.'); |
| 48 | 49 | } |
| 49 | 50 | |
@@ -55,7 +56,8 @@ discard block |
||
| 55 | 56 | */ |
| 56 | 57 | public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool |
| 57 | 58 | { |
| 58 | - if (!isset(self::$uris[$path])) { |
|
| 59 | + if (!isset(self::$uris[$path])) |
|
| 60 | + { |
|
| 59 | 61 | return false; |
| 60 | 62 | } |
| 61 | 63 | |
@@ -72,7 +74,8 @@ discard block |
||
| 72 | 74 | */ |
| 73 | 75 | public function stream_read(int $length): string|false |
| 74 | 76 | { |
| 75 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 77 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 78 | + { |
|
| 76 | 79 | throw new WrapperException('Stream is not opened.'); |
| 77 | 80 | } |
| 78 | 81 | |
@@ -84,7 +87,8 @@ discard block |
||
| 84 | 87 | */ |
| 85 | 88 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
| 86 | 89 | { |
| 87 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 90 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 91 | + { |
|
| 88 | 92 | throw new WrapperException('Stream is not opened.'); |
| 89 | 93 | } |
| 90 | 94 | |
@@ -102,7 +106,8 @@ discard block |
||
| 102 | 106 | */ |
| 103 | 107 | public function stream_stat(): array |
| 104 | 108 | { |
| 105 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 109 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 110 | + { |
|
| 106 | 111 | throw new WrapperException('Stream is not opened.'); |
| 107 | 112 | } |
| 108 | 113 | |
@@ -114,7 +119,8 @@ discard block |
||
| 114 | 119 | */ |
| 115 | 120 | public function stream_tell(): int |
| 116 | 121 | { |
| 117 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 122 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 123 | + { |
|
| 118 | 124 | throw new WrapperException('Stream is not opened.'); |
| 119 | 125 | } |
| 120 | 126 | |
@@ -128,7 +134,8 @@ discard block |
||
| 128 | 134 | */ |
| 129 | 135 | public function stream_write(string $data): int |
| 130 | 136 | { |
| 131 | - if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) { |
|
| 137 | + if (!$this->stream instanceof \Psr\Http\Message\StreamInterface) |
|
| 138 | + { |
|
| 132 | 139 | throw new WrapperException('Stream is not opened.'); |
| 133 | 140 | } |
| 134 | 141 | |
@@ -142,12 +149,17 @@ discard block |
||
| 142 | 149 | */ |
| 143 | 150 | public function url_stat(string $path, int $flag): array|false |
| 144 | 151 | { |
| 145 | - try { |
|
| 146 | - if (isset(self::$uris[$path])) { |
|
| 152 | + try |
|
| 153 | + { |
|
| 154 | + if (isset(self::$uris[$path])) |
|
| 155 | + { |
|
| 147 | 156 | return $this->getStreamStats(self::$uris[$path]); |
| 148 | 157 | } |
| 149 | - } catch (\Throwable $e) { |
|
| 150 | - if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET) { |
|
| 158 | + } |
|
| 159 | + catch (\Throwable $e) |
|
| 160 | + { |
|
| 161 | + if (($flag & \STREAM_URL_STAT_QUIET) === \STREAM_URL_STAT_QUIET) |
|
| 162 | + { |
|
| 151 | 163 | return false; |
| 152 | 164 | } |
| 153 | 165 | \trigger_error($e->getMessage(), \E_USER_ERROR); |
@@ -161,7 +173,8 @@ discard block |
||
| 161 | 173 | */ |
| 162 | 174 | public static function register(): void |
| 163 | 175 | { |
| 164 | - if (self::$registered) { |
|
| 176 | + if (self::$registered) |
|
| 177 | + { |
|
| 165 | 178 | return; |
| 166 | 179 | } |
| 167 | 180 | |
@@ -175,7 +188,8 @@ discard block |
||
| 175 | 188 | */ |
| 176 | 189 | public static function has(StreamInterface|string $file): bool |
| 177 | 190 | { |
| 178 | - if ($file instanceof StreamInterface) { |
|
| 191 | + if ($file instanceof StreamInterface) |
|
| 192 | + { |
|
| 179 | 193 | $file = 'spiral://' . \spl_object_hash($file); |
| 180 | 194 | } |
| 181 | 195 | |
@@ -191,15 +205,18 @@ discard block |
||
| 191 | 205 | public static function getResource(StreamInterface $stream) |
| 192 | 206 | { |
| 193 | 207 | $mode = null; |
| 194 | - if ($stream->isReadable()) { |
|
| 208 | + if ($stream->isReadable()) |
|
| 209 | + { |
|
| 195 | 210 | $mode = 'r'; |
| 196 | 211 | } |
| 197 | 212 | |
| 198 | - if ($stream->isWritable()) { |
|
| 213 | + if ($stream->isWritable()) |
|
| 214 | + { |
|
| 199 | 215 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 200 | 216 | } |
| 201 | 217 | |
| 202 | - if (empty($mode)) { |
|
| 218 | + if (empty($mode)) |
|
| 219 | + { |
|
| 203 | 220 | throw new WrapperException('Stream is not available in read or write modes'); |
| 204 | 221 | } |
| 205 | 222 | |
@@ -227,7 +244,8 @@ discard block |
||
| 227 | 244 | */ |
| 228 | 245 | public static function release(StreamInterface|string $file): void |
| 229 | 246 | { |
| 230 | - if ($file instanceof StreamInterface) { |
|
| 247 | + if ($file instanceof StreamInterface) |
|
| 248 | + { |
|
| 231 | 249 | $file = 'spiral://' . \spl_object_hash($file); |
| 232 | 250 | } |
| 233 | 251 | |
@@ -240,12 +258,15 @@ discard block |
||
| 240 | 258 | private function getStreamStats(StreamInterface $stream): array |
| 241 | 259 | { |
| 242 | 260 | $mode = $this->mode; |
| 243 | - if (empty($mode)) { |
|
| 244 | - if ($stream->isReadable()) { |
|
| 261 | + if (empty($mode)) |
|
| 262 | + { |
|
| 263 | + if ($stream->isReadable()) |
|
| 264 | + { |
|
| 245 | 265 | $mode = 'r'; |
| 246 | 266 | } |
| 247 | 267 | |
| 248 | - if ($stream->isWritable()) { |
|
| 268 | + if ($stream->isWritable()) |
|
| 269 | + { |
|
| 249 | 270 | $mode = !empty($mode) ? 'r+' : 'w'; |
| 250 | 271 | } |
| 251 | 272 | } |
@@ -68,13 +68,17 @@ discard block |
||
| 68 | 68 | return $this->tracer->trace( |
| 69 | 69 | name: 'Routing', |
| 70 | 70 | callback: function (SpanInterface $span) use ($request): ResponseInterface { |
| 71 | - try { |
|
| 71 | + try |
|
| 72 | + { |
|
| 72 | 73 | $route = $this->matchRoute($request, $routeName); |
| 73 | - } catch (RouteException $e) { |
|
| 74 | + } |
|
| 75 | + catch (RouteException $e) |
|
| 76 | + { |
|
| 74 | 77 | throw new RouterException('Invalid route definition', $e->getCode(), $e); |
| 75 | 78 | } |
| 76 | 79 | |
| 77 | - if (!$route instanceof \Spiral\Router\RouteInterface) { |
|
| 80 | + if (!$route instanceof \Spiral\Router\RouteInterface) |
|
| 81 | + { |
|
| 78 | 82 | $this->eventDispatcher?->dispatch(new RouteNotFound($request)); |
| 79 | 83 | throw new RouteNotFoundException($request->getUri()); |
| 80 | 84 | } |
@@ -109,7 +113,8 @@ discard block |
||
| 109 | 113 | |
| 110 | 114 | public function getRoute(string $name): RouteInterface |
| 111 | 115 | { |
| 112 | - if (isset($this->routes[$name])) { |
|
| 116 | + if (isset($this->routes[$name])) |
|
| 117 | + { |
|
| 113 | 118 | return $this->routes[$name]; |
| 114 | 119 | } |
| 115 | 120 | |
@@ -118,7 +123,8 @@ discard block |
||
| 118 | 123 | |
| 119 | 124 | public function getRoutes(): array |
| 120 | 125 | { |
| 121 | - if ($this->default instanceof \Spiral\Router\RouteInterface) { |
|
| 126 | + if ($this->default instanceof \Spiral\Router\RouteInterface) |
|
| 127 | + { |
|
| 122 | 128 | return $this->routes + [null => $this->default]; |
| 123 | 129 | } |
| 124 | 130 | |
@@ -127,9 +133,12 @@ discard block |
||
| 127 | 133 | |
| 128 | 134 | public function uri(string $route, iterable $parameters = []): UriInterface |
| 129 | 135 | { |
| 130 | - try { |
|
| 136 | + try |
|
| 137 | + { |
|
| 131 | 138 | return $this->getRoute($route)->uri($parameters); |
| 132 | - } catch (UndefinedRouteException) { |
|
| 139 | + } |
|
| 140 | + catch (UndefinedRouteException) |
|
| 141 | + { |
|
| 133 | 142 | //In some cases route name can be provided as controller:action pair, we can try to |
| 134 | 143 | //generate such route automatically based on our default/fallback route |
| 135 | 144 | return $this->castRoute($route)->uri($parameters); |
@@ -141,9 +150,11 @@ discard block |
||
| 141 | 150 | /** @var GroupRegistry $groups */ |
| 142 | 151 | $groups = $this->container->get(GroupRegistry::class); |
| 143 | 152 | |
| 144 | - foreach ($routes->getCollection() as $name => $configurator) { |
|
| 153 | + foreach ($routes->getCollection() as $name => $configurator) |
|
| 154 | + { |
|
| 145 | 155 | $target = $configurator->target; |
| 146 | - if ($configurator->core !== null && $target instanceof AbstractTarget) { |
|
| 156 | + if ($configurator->core !== null && $target instanceof AbstractTarget) |
|
| 157 | + { |
|
| 147 | 158 | $target = $target->withCore($configurator->core); |
| 148 | 159 | } |
| 149 | 160 | |
@@ -152,21 +163,25 @@ discard block |
||
| 152 | 163 | : \ltrim($configurator->pattern, '/'); |
| 153 | 164 | $route = new Route($pattern, $target, $configurator->defaults); |
| 154 | 165 | |
| 155 | - if ($configurator->middleware !== null) { |
|
| 166 | + if ($configurator->middleware !== null) |
|
| 167 | + { |
|
| 156 | 168 | $route = $route->withMiddleware(...$configurator->middleware); |
| 157 | 169 | } |
| 158 | 170 | |
| 159 | - if ($configurator->methods !== null) { |
|
| 171 | + if ($configurator->methods !== null) |
|
| 172 | + { |
|
| 160 | 173 | $route = $route->withVerbs(...$configurator->methods); |
| 161 | 174 | } |
| 162 | 175 | |
| 163 | - if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 176 | + if (!isset($this->routes[$name]) && $name !== RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 177 | + { |
|
| 164 | 178 | $group = $groups->getGroup($configurator->group ?? $groups->getDefaultGroup()); |
| 165 | 179 | $group->setPrefix($configurator->prefix); |
| 166 | 180 | $group->addRoute($name, $route); |
| 167 | 181 | } |
| 168 | 182 | |
| 169 | - if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) { |
|
| 183 | + if ($name === RoutingConfigurator::DEFAULT_ROUTE_NAME) |
|
| 184 | + { |
|
| 170 | 185 | $this->setDefault($route); |
| 171 | 186 | } |
| 172 | 187 | } |
@@ -177,17 +192,20 @@ discard block |
||
| 177 | 192 | */ |
| 178 | 193 | protected function matchRoute(ServerRequestInterface $request, ?string &$routeName = null): ?RouteInterface |
| 179 | 194 | { |
| 180 | - foreach ($this->routes as $name => $route) { |
|
| 195 | + foreach ($this->routes as $name => $route) |
|
| 196 | + { |
|
| 181 | 197 | // Matched route will return new route instance with matched parameters |
| 182 | 198 | $matched = $route->match($request); |
| 183 | 199 | |
| 184 | - if ($matched !== null) { |
|
| 200 | + if ($matched !== null) |
|
| 201 | + { |
|
| 185 | 202 | $routeName = $name; |
| 186 | 203 | return $matched; |
| 187 | 204 | } |
| 188 | 205 | } |
| 189 | 206 | |
| 190 | - if ($this->default instanceof \Spiral\Router\RouteInterface) { |
|
| 207 | + if ($this->default instanceof \Spiral\Router\RouteInterface) |
|
| 208 | + { |
|
| 191 | 209 | return $this->default->match($request); |
| 192 | 210 | } |
| 193 | 211 | |
@@ -200,14 +218,18 @@ discard block |
||
| 200 | 218 | */ |
| 201 | 219 | protected function configure(RouteInterface $route): RouteInterface |
| 202 | 220 | { |
| 203 | - if ($route instanceof ContainerizedInterface && !$route->hasContainer()) { |
|
| 221 | + if ($route instanceof ContainerizedInterface && !$route->hasContainer()) |
|
| 222 | + { |
|
| 204 | 223 | // isolating route in a given container |
| 205 | 224 | $route = $route->withContainer($this->container); |
| 206 | 225 | } |
| 207 | 226 | |
| 208 | - try { |
|
| 227 | + try |
|
| 228 | + { |
|
| 209 | 229 | $uriHandler = $route->getUriHandler(); |
| 210 | - } catch (\Throwable) { |
|
| 230 | + } |
|
| 231 | + catch (\Throwable) |
|
| 232 | + { |
|
| 211 | 233 | $uriHandler = $this->uriHandler; |
| 212 | 234 | } |
| 213 | 235 | |
@@ -239,11 +261,16 @@ discard block |
||
| 239 | 261 | /** |
| 240 | 262 | * @var Matches $matches |
| 241 | 263 | */ |
| 242 | - if (!empty($matches['name'])) { |
|
| 264 | + if (!empty($matches['name'])) |
|
| 265 | + { |
|
| 243 | 266 | $routeObject = $this->getRoute($matches['name']); |
| 244 | - } elseif ($this->default instanceof \Spiral\Router\RouteInterface) { |
|
| 267 | + } |
|
| 268 | + elseif ($this->default instanceof \Spiral\Router\RouteInterface) |
|
| 269 | + { |
|
| 245 | 270 | $routeObject = $this->default; |
| 246 | - } else { |
|
| 271 | + } |
|
| 272 | + else |
|
| 273 | + { |
|
| 247 | 274 | throw new UndefinedRouteException(\sprintf('Unable to locate route candidate for `%s`', $route)); |
| 248 | 275 | } |
| 249 | 276 | |
@@ -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 (!$this->requestHandler instanceof \Psr\Http\Server\RequestHandlerInterface) { |
|
| 119 | + if (!$this->requestHandler instanceof \Psr\Http\Server\RequestHandlerInterface) |
|
| 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 | } |
@@ -86,13 +86,15 @@ discard block |
||
| 86 | 86 | |
| 87 | 87 | protected function coreHandler(ContainerInterface $container): CoreHandler |
| 88 | 88 | { |
| 89 | - if ($this->handler instanceof \Spiral\Router\CoreHandler) { |
|
| 89 | + if ($this->handler instanceof \Spiral\Router\CoreHandler) |
|
| 90 | + { |
|
| 90 | 91 | return $this->handler; |
| 91 | 92 | } |
| 92 | 93 | |
| 93 | 94 | $scope = Proxy::create(new \ReflectionClass(ScopeInterface::class), null, new \Spiral\Core\Attribute\Proxy()); |
| 94 | 95 | |
| 95 | - try { |
|
| 96 | + try |
|
| 97 | + { |
|
| 96 | 98 | // construct on demand |
| 97 | 99 | $this->handler = new CoreHandler( |
| 98 | 100 | match (false) { |
@@ -106,7 +108,9 @@ discard block |
||
| 106 | 108 | ); |
| 107 | 109 | |
| 108 | 110 | return $this->handler; |
| 109 | - } catch (ContainerExceptionInterface $e) { |
|
| 111 | + } |
|
| 112 | + catch (ContainerExceptionInterface $e) |
|
| 113 | + { |
|
| 110 | 114 | throw new TargetException($e->getMessage(), $e->getCode(), $e); |
| 111 | 115 | } |
| 112 | 116 | } |
@@ -43,11 +43,13 @@ |
||
| 43 | 43 | |
| 44 | 44 | public function getActor(): ?object |
| 45 | 45 | { |
| 46 | - if ($this->closed) { |
|
| 46 | + if ($this->closed) |
|
| 47 | + { |
|
| 47 | 48 | return null; |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | - if ($this->actor === null && $this->token instanceof \Spiral\Auth\TokenInterface) { |
|
| 51 | + if ($this->actor === null && $this->token instanceof \Spiral\Auth\TokenInterface) |
|
| 52 | + { |
|
| 51 | 53 | $this->actor = $this->actorProvider->getActor($this->token); |
| 52 | 54 | } |
| 53 | 55 | |
@@ -78,7 +78,8 @@ |
||
| 78 | 78 | 'test' => $this->defaultCache, |
| 79 | 79 | 'array' => new ArrayStorage(), |
| 80 | 80 | ][$name] ?? null; |
| 81 | - if (!$result instanceof \Psr\SimpleCache\CacheInterface) { |
|
| 81 | + if (!$result instanceof \Psr\SimpleCache\CacheInterface) |
|
| 82 | + { |
|
| 82 | 83 | throw new NotFoundException(); |
| 83 | 84 | } |
| 84 | 85 | return $result; |