Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating HMVC code with PHP 8.1 features #628

Merged
merged 4 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
added return type `mixed` to the method `getContext` in `Spiral\Filters\FilterInterface` interface.
Added return type `mixed` to the method `getValue` in `Spiral\Filters\InputInterface`.
- [spiral/http] Config `Spiral\Config\JsonPayloadConfig` moved to the `Spiral\Bootloader\Http\JsonPayloadConfig`.
- [spiral/hmvc] Added return type `mixed` to the method `process` in `Spiral\Core\CoreInterceptorInterface` interface.
- [spiral/hmvc] Added return type `mixed` to the method `callAction` in `Spiral\Core\CoreInterface` interface.
- [spiral/encrypter] Added return type `mixed` to the method `decrypt` in `Spiral\Encrypter\EncrypterInterface` interface.
- [spiral/data-grid] Added return type `mixed` and `mixed` parameter type of `$source` to the method `write` in
`Spiral\DataGrid\WriterInterface` interface.
Expand Down
41 changes: 15 additions & 26 deletions src/Hmvc/src/AbstractCore.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand All @@ -23,38 +16,32 @@
*/
abstract class AbstractCore implements CoreInterface
{
/** @var ContainerInterface @internal */
protected $container;

/** @var ResolverInterface @internal */
protected $resolver;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
/** @internal */
protected ResolverInterface $resolver;

public function __construct(
/** @internal */
protected ContainerInterface $container
) {
// resolver is usually the container itself
$this->resolver = $container->get(ResolverInterface::class);
}

/**
* {@inheritdoc}
*/
public function callAction(string $controller, string $action, array $parameters = [])
public function callAction(string $controller, string $action, array $parameters = []): mixed
{
try {
$method = new \ReflectionMethod($controller, $action);
} catch (\ReflectionException $e) {
throw new ControllerException(
"Invalid action `{$controller}`->`{$action}`",
\sprintf('Invalid action `%s`->`%s`', $controller, $action),
ControllerException::BAD_ACTION,
$e
);
}

if ($method->isStatic() || !$method->isPublic()) {
throw new ControllerException(
"Invalid action `{$controller}`->`{$action}`",
\sprintf('Invalid action `%s`->`%s`', $controller, $action),
ControllerException::BAD_ACTION
);
}
Expand All @@ -64,7 +51,7 @@ public function callAction(string $controller, string $action, array $parameters
$args = $this->resolver->resolveArguments($method, $parameters);
} catch (ArgumentException $e) {
throw new ControllerException(
"Missing/invalid parameter '{$e->getParameter()->name}' of `{$controller}`->`{$action}`",
\sprintf('Missing/invalid parameter %s of `%s`->`%s`', $e->getParameter()->name, $controller, $action),
ControllerException::BAD_ARGUMENT
);
} catch (ContainerExceptionInterface $e) {
Expand All @@ -75,8 +62,10 @@ public function callAction(string $controller, string $action, array $parameters
);
}

return ContainerScope::runScope($this->container, function () use ($controller, $method, $args) {
return $method->invokeArgs($this->container->get($controller), $args);
});
$container = $this->container;
return ContainerScope::runScope(
$container,
static fn () => $method->invokeArgs($container->get($controller), $args)
);
}
}
7 changes: 0 additions & 7 deletions src/Hmvc/src/Core.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand Down
7 changes: 0 additions & 7 deletions src/Hmvc/src/CoreInterceptorInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand Down
10 changes: 1 addition & 9 deletions src/Hmvc/src/CoreInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand All @@ -25,10 +18,9 @@ interface CoreInterface
* @param string $controller Controller class.
* @param string $action Controller method name.
* @param array $parameters Action parameters (if any).
* @return mixed
*
* @throws ControllerException
* @throws \Throwable
*/
public function callAction(string $controller, string $action, array $parameters = []);
public function callAction(string $controller, string $action, array $parameters = []): mixed;
}
7 changes: 0 additions & 7 deletions src/Hmvc/src/Exception/ControllerException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Hmvc/src/Exception/CoreException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core\Exception;
Expand Down
7 changes: 0 additions & 7 deletions src/Hmvc/src/Exception/InterceptorException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core\Exception;
Expand Down
24 changes: 5 additions & 19 deletions src/Hmvc/src/InterceptableCore.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand All @@ -16,27 +9,20 @@
*/
final class InterceptableCore implements CoreInterface
{
/** @var InterceptorPipeline */
private $pipeline;
private InterceptorPipeline $pipeline;

/** @var CoreInterface */
private $core;

public function __construct(CoreInterface $core)
{
public function __construct(
private readonly CoreInterface $core
) {
$this->pipeline = new InterceptorPipeline();
$this->core = $core;
}

public function addInterceptor(CoreInterceptorInterface $interceptor): void
{
$this->pipeline->addInterceptor($interceptor);
}

/**
* @inheritDoc
*/
public function callAction(string $controller, string $action, array $parameters = [])
public function callAction(string $controller, string $action, array $parameters = []): mixed
{
return $this->pipeline->withCore($this->core)->callAction($controller, $action, $parameters);
}
Expand Down
19 changes: 4 additions & 15 deletions src/Hmvc/src/InterceptorPipeline.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Core;
Expand All @@ -18,14 +11,12 @@
*/
final class InterceptorPipeline implements CoreInterface
{
/** @var CoreInterface */
private $core;
private ?CoreInterface $core = null;

/** @var CoreInterceptorInterface[] */
private $interceptors = [];
private array $interceptors = [];

/** @var int */
private $position = 0;
private int $position = 0;

public function addInterceptor(CoreInterceptorInterface $interceptor): void
{
Expand All @@ -41,11 +32,9 @@ public function withCore(CoreInterface $core): self
}

/**
* @param string|null $action
* @return mixed
* @throws \Throwable
*/
public function callAction(string $controller, string $action, array $parameters = [])
public function callAction(string $controller, string $action, array $parameters = []): mixed
{
if ($this->core === null) {
throw new InterceptorException('Unable to invoke pipeline without assigned core');
Expand Down
2 changes: 1 addition & 1 deletion src/Router/tests/TestCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(CoreInterface $core)
$this->core = $core;
}

public function callAction(string $controller, string $action = null, array $parameters = [])
public function callAction(string $controller, string $action = null, array $parameters = []): string
{
return '@wrapped.' . $this->core->callAction($controller, $action, $parameters);
}
Expand Down