Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent 63bee5a commit 18dcf0b
Show file tree
Hide file tree
Showing 77 changed files with 968 additions and 742 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
"require": {
"php": ">=8.1",
"ext-json": "*",
"spiral/core": "^3.14.10",
"spiral/hmvc": "^3.14.10",
"spiral/http": "^3.14.10",
"spiral/interceptors": "^3.14.10",
"spiral/core": "^3.15",
"spiral/hmvc": "^3.15",
"spiral/http": "^3.15",
"spiral/interceptors": "^3.15",
"cocur/slugify": "^3.2 || ^4.0",
"spiral/telemetry": "^3.14.10",
"spiral/telemetry": "^3.15",
"doctrine/inflector": "^1.4|^2.0",
"psr/event-dispatcher": "^1.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractRoute implements RouteInterface

public function __construct(
protected string $pattern,
array $defaults = []
array $defaults = [],
) {
$this->defaults = $defaults;
}
Expand Down Expand Up @@ -80,7 +80,7 @@ public function uri(iterable $parameters = []): UriInterface
{
return $this->uriHandler->uri(
$parameters,
\array_merge($this->defaults, $this->matches ?? [])
\array_merge($this->defaults, $this->matches ?? []),
);
}
}
5 changes: 2 additions & 3 deletions src/Autofill.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
final class Autofill implements \Stringable
{
public function __construct(
private readonly string $value
) {
}
private readonly string $value,
) {}

public function __toString(): string
{
Expand Down
21 changes: 14 additions & 7 deletions src/CoreHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Core\CoreInterface;
Expand Down Expand Up @@ -35,10 +36,13 @@ final class CoreHandler implements RequestHandlerInterface

/** @readonly */
private ?string $controller = null;

/** @readonly */
private ?string $action = null;

/** @readonly */
private ?bool $verbActions = null;

/** @readonly */
private ?array $parameters = null;

Expand All @@ -48,7 +52,7 @@ public function __construct(
private readonly HandlerInterface|CoreInterface $core,
private readonly ScopeInterface $scope,
private readonly ResponseFactoryInterface $responseFactory,
?TracerInterface $tracer = null
?TracerInterface $tracer = null,
) {
$this->tracer = $tracer ?? new NullTracer($scope);
$this->isLegacyPipeline = !$core instanceof HandlerInterface;
Expand Down Expand Up @@ -112,26 +116,29 @@ public function handle(Request $request): Response
name: 'http-request',
bindings: [Request::class => $request, Response::class => $response, $controller => $controller],
),
fn (): mixed => $this->tracer->trace(
fn(): mixed => $this->tracer->trace(
name: 'Controller [' . $controller . ':' . $action . ']',
callback: $this->isLegacyPipeline
? fn (): mixed => $this->core->callAction(
? fn(): mixed => $this->core->callAction(
controller: $controller,
action: $action,
parameters: $parameters,
)
: fn (): mixed => $this->core->handle(
: fn(): mixed => $this->core->handle(
new CallContext(
Target::fromPair($controller, $action),
$parameters,
[
ServerRequestInterface::class => $request,
],
),
),
attributes: [
'route.controller' => $this->controller,
'route.action' => $action,
'route.parameters' => \array_keys($parameters),
]
)
],
),
);
} catch (TargetCallException $e) {
\ob_get_clean();
Expand Down Expand Up @@ -176,7 +183,7 @@ private function wrapResponse(Response $response, mixed $result = null, string $
if (\is_array($result) || $result instanceof \JsonSerializable) {
$response = $this->writeJson($response, $result);
} else {
$response->getBody()->write((string)$result);
$response->getBody()->write((string) $result);
}

//Always glue buffered output
Expand Down
3 changes: 1 addition & 2 deletions src/Event/RouteMatched.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ final class RouteMatched
public function __construct(
public readonly ServerRequestInterface $request,
public readonly RouteInterface $route,
) {
}
) {}
}
5 changes: 2 additions & 3 deletions src/Event/RouteNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
final class RouteNotFound
{
public function __construct(
public readonly ServerRequestInterface $request
) {
}
public readonly ServerRequestInterface $request,
) {}
}
5 changes: 2 additions & 3 deletions src/Event/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
final class Routing
{
public function __construct(
public readonly ServerRequestInterface $request
) {
}
public readonly ServerRequestInterface $request,
) {}
}
4 changes: 1 addition & 3 deletions src/Exception/ConstrainException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class ConstrainException extends RouteException
{
}
class ConstrainException extends RouteException {}
4 changes: 1 addition & 3 deletions src/Exception/HandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class HandlerException extends \RuntimeException
{
}
class HandlerException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Exception/LoaderLoadException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class LoaderLoadException extends \RuntimeException
{
}
class LoaderLoadException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Exception/RouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class RouteException extends RouterException
{
}
class RouteException extends RouterException {}
2 changes: 1 addition & 1 deletion src/Exception/RouteNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RouteNotFoundException extends UndefinedRouteException
public function __construct(
private readonly UriInterface $uri,
int $code = 0,
?\Throwable $previous = null
?\Throwable $previous = null,
) {
parent::__construct(\sprintf('Unable to route `%s`.', (string) $uri), $code, $previous);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/RouterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class RouterException extends \RuntimeException
{
}
class RouterException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Exception/TargetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class TargetException extends \RuntimeException
{
}
class TargetException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Exception/UndefinedRouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class UndefinedRouteException extends RouterException
{
}
class UndefinedRouteException extends RouterException {}
4 changes: 1 addition & 3 deletions src/Exception/UriHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Router\Exception;

class UriHandlerException extends \RuntimeException
{
}
class UriHandlerException extends \RuntimeException {}
5 changes: 2 additions & 3 deletions src/GroupRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ final class GroupRegistry implements \IteratorAggregate
private array $groups = [];

public function __construct(
private readonly FactoryInterface $factory
) {
}
private readonly FactoryInterface $factory,
) {}

/**
* @param non-empty-string $name
Expand Down
35 changes: 17 additions & 18 deletions src/Loader/Configurator/ImportConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,8 @@ final class ImportConfigurator
{
public function __construct(
private readonly RouteCollection $parent,
private readonly RouteCollection $routes
) {
}

public function __destruct()
{
$this->parent->addCollection($this->routes);
}

public function __sleep(): array
{
throw new \BadMethodCallException('Cannot unserialize ' . self::class);
}

public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize ' . self::class);
}
private readonly RouteCollection $routes,
) {}

public function defaults(array $defaults): self
{
Expand Down Expand Up @@ -95,4 +79,19 @@ public function middleware(MiddlewareInterface|string|array $middleware): self

return $this;
}

public function __sleep(): array
{
throw new \BadMethodCallException('Cannot unserialize ' . self::class);
}

public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize ' . self::class);
}

public function __destruct()
{
$this->parent->addCollection($this->routes);
}
}
69 changes: 34 additions & 35 deletions src/Loader/Configurator/RouteConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,8 @@ final class RouteConfigurator
public function __construct(
private readonly string $name,
private readonly string $pattern,
private readonly RouteCollection $collection
) {
}

public function __destruct()
{
if ($this->target === null) {
throw new TargetException(
\sprintf('The [%s] route has no defined target. Call one of: `controller`, `action`,
`namespaced`, `groupControllers`, `callable`, `handler` methods.', $this->name)
);
}

$this->collection->add($this->name, $this);
}

/**
* @internal
*
* Don't use this method. For internal use only.
*/
public function __get(string $name): mixed
{
return match ($name) {
'core' => $this->core,
'target' => $this->target,
'defaults' => $this->defaults,
'group' => $this->group,
'middleware' => $this->middleware,
'methods' => $this->methods,
'pattern' => $this->pattern,
'prefix' => \trim($this->prefix, '/'),
default => throw new \BadMethodCallException(\sprintf('Unable to access %s.', $name))
};
}
private readonly RouteCollection $collection,
) {}

public function controller(string $controller, int $options = 0, string $defaultAction = 'index'): self
{
Expand Down Expand Up @@ -158,4 +125,36 @@ public function methods(string|array $methods): self

return $this;
}

/**
* @internal
*
* Don't use this method. For internal use only.
*/
public function __get(string $name): mixed
{
return match ($name) {
'core' => $this->core,
'target' => $this->target,
'defaults' => $this->defaults,
'group' => $this->group,
'middleware' => $this->middleware,
'methods' => $this->methods,
'pattern' => $this->pattern,
'prefix' => \trim($this->prefix, '/'),
default => throw new \BadMethodCallException(\sprintf('Unable to access %s.', $name)),
};
}

public function __destruct()
{
if ($this->target === null) {
throw new TargetException(
\sprintf('The [%s] route has no defined target. Call one of: `controller`, `action`,
`namespaced`, `groupControllers`, `callable`, `handler` methods.', $this->name),
);
}

$this->collection->add($this->name, $this);
}
}
5 changes: 2 additions & 3 deletions src/Loader/Configurator/RoutingConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ final class RoutingConfigurator

public function __construct(
private readonly RouteCollection $collection,
private readonly LoaderInterface $loader
) {
}
private readonly LoaderInterface $loader,
) {}

public function import(string|array $resource, ?string $type = null): ImportConfigurator
{
Expand Down
5 changes: 2 additions & 3 deletions src/Loader/DelegatingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
final class DelegatingLoader implements LoaderInterface
{
public function __construct(
private readonly LoaderRegistryInterface $registry
) {
}
private readonly LoaderRegistryInterface $registry,
) {}

public function load(mixed $resource, ?string $type = null): mixed
{
Expand Down
Loading

0 comments on commit 18dcf0b

Please sign in to comment.