Skip to content
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
26 changes: 26 additions & 0 deletions src/Action/ActionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Eukles\Container\ContainerTrait;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -25,6 +26,11 @@ abstract class ActionAbstract implements ActionInterface
*/
protected $response;

/**
* @var RequestInterface
*/
protected $request;

/**
* SlimControllerInterface constructor.
*
Expand Down Expand Up @@ -58,4 +64,24 @@ public function setResponse(ResponseInterface $response): ActionInterface

return $this;
}

/**
* @param RequestInterface $request
*
* @return ActionInterface
*/
public function setRequest(RequestInterface $request): ActionInterface
{
$this->request = $request;

return $this;
}

/**
* @return RequestInterface
*/
public function getRequest(): RequestInterface
{
return $this->request;
}
}
13 changes: 13 additions & 0 deletions src/Action/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Eukles\Service\QueryModifier\QueryModifierInterface;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -46,6 +47,18 @@ public function getResponse(): ResponseInterface;
*/
public function setResponse(ResponseInterface $response): ActionInterface;

/**
* @param RequestInterface $request
*
* @return ActionInterface
*/
public function setRequest(RequestInterface $request): ActionInterface;

/**
* @return RequestInterface
*/
public function getRequest(): RequestInterface;

/**
* Action factory
*
Expand Down
29 changes: 18 additions & 11 deletions src/Slim/Handlers/Strategies/ActionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Eukles\Slim\Handlers\Strategies;

use Closure;
use Eukles\Action;
use Eukles\Container\ContainerInterface;
use Eukles\Service\Pagination\PaginationInterface;
use Eukles\Service\QueryModifier\QueryModifierInterface;
use Eukles\Service\ResponseBuilder\ResponseBuilderException;
use Eukles\Service\ResponseFormatter\ResponseFormatterException;
use Eukles\Slim\Handlers\ApiProblemRendererTrait;
use Exception;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
use Slim\Interfaces\InvocationStrategyInterface;

/**
Expand Down Expand Up @@ -48,7 +54,7 @@ public function __construct(ContainerInterface $container)
* @param array $routeArguments
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function __invoke(
callable $callable,
Expand Down Expand Up @@ -81,7 +87,7 @@ public function callAction(

try {
return call_user_func_array($callable, $this->buildParams($callable, $request, $routeArguments));
} catch (\Exception $e) {
} catch (Exception $e) {
$handler = $this->container->getActionErrorHandler();

return $handler($e, $request, $response);
Expand All @@ -96,7 +102,7 @@ public function callAction(
* @param $routeArguments
*
* @return array
* @throws \ReflectionException
* @throws ReflectionException
*/
protected function buildParams(
callable $callable,
Expand All @@ -108,7 +114,7 @@ protected function buildParams(
return [];
}

$r = new \ReflectionClass($callable[0]);
$r = new ReflectionClass($callable[0]);
$m = $r->getMethod($callable[1]);
$paramsMethod = $m->getParameters();

Expand All @@ -124,7 +130,7 @@ protected function buildParams(

$buildParams = [];

/** @var \ReflectionParameter[] $params */
/** @var ReflectionParameter[] $params */
foreach ($paramsMethod as $param) {
$name = $param->getName();
$class = $param->getClass();
Expand All @@ -141,7 +147,7 @@ protected function buildParams(
/** @var UploadedFileInterface $attachment */
$buildParams[] = isset($files[0]) ? $files[0] : null;
} elseif (!$param->isDefaultValueAvailable()) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
"Missing or null required parameter '{$name}' in " . $r->getName() . "::" . $m->getName()
);
}
Expand All @@ -155,7 +161,7 @@ protected function buildParams(
} elseif ($param->isDefaultValueAvailable()) {
$paramValue = $param->getDefaultValue();
} else {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
"Missing or null required parameter '{$name}' in " . $r->getName() . "::" . $m->getName()
);
}
Expand All @@ -170,10 +176,10 @@ protected function buildParams(
* Build a string response
*
* @param mixed $result
* @param \Psr\Http\Message\ResponseInterface $response
* @param ResponseInterface $response
*
* @return ResponseInterface
* @throws \Exception
* @throws Exception
*/
protected function buildResponse($result, ResponseInterface $response)
{
Expand Down Expand Up @@ -202,7 +208,7 @@ protected function buildResponse($result, ResponseInterface $response)
* @param array $routeArguments
*
* @return mixed
* @throws \Exception
* @throws Exception
*/
protected function callHandler(
callable $callable,
Expand All @@ -211,13 +217,14 @@ protected function callHandler(
array $routeArguments
) {
# Action is a closure
if ($callable instanceof \Closure) {
if ($callable instanceof Closure) {
array_unshift($routeArguments, $request, $response);
$result = call_user_func_array($callable, $routeArguments);
} else {
# Action is a method of an Action class
if (is_array($callable) && $callable[0] instanceof Action\ActionInterface) {
$callable[0]->setResponse($response);
$callable[0]->setRequest($request);
}

# Call Action method
Expand Down