Skip to content

Commit

Permalink
Merge a0e8e41 into 80a52f7
Browse files Browse the repository at this point in the history
  • Loading branch information
iSize1ce committed Oct 3, 2019
2 parents 80a52f7 + a0e8e41 commit f35d601
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 35 deletions.
2 changes: 0 additions & 2 deletions Slim/Handlers/Strategies/RequestHandler.php
Expand Up @@ -36,15 +36,13 @@ public function __construct(bool $appendRouteArgumentsToRequestAttributes = fals
*
* @param callable $callable
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $routeArguments
*
* @return ResponseInterface
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
if ($this->appendRouteArgumentsToRequestAttributes) {
Expand Down
18 changes: 15 additions & 3 deletions Slim/Handlers/Strategies/RequestResponse.php
Expand Up @@ -9,6 +9,7 @@

namespace Slim\Handlers\Strategies;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;
Expand All @@ -18,27 +19,38 @@
*/
class RequestResponse implements InvocationStrategyInterface
{
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* @param ResponseFactoryInterface $responseFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

/**
* Invoke a route callable with request, response, and all route parameters
* as an array of arguments.
*
* @param callable $callable
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $routeArguments
*
* @return ResponseInterface
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
foreach ($routeArguments as $k => $v) {
$request = $request->withAttribute($k, $v);
}

return $callable($request, $response, $routeArguments);
return $callable($request, $this->responseFactory->createResponse(), $routeArguments);
}
}
18 changes: 15 additions & 3 deletions Slim/Handlers/Strategies/RequestResponseArgs.php
Expand Up @@ -9,6 +9,7 @@

namespace Slim\Handlers\Strategies;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;
Expand All @@ -18,23 +19,34 @@
*/
class RequestResponseArgs implements InvocationStrategyInterface
{
/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* @param ResponseFactoryInterface $responseFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

/**
* Invoke a route callable with request, response and all route parameters
* as individual arguments.
*
* @param callable $callable
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $routeArguments
*
* @return ResponseInterface
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
return $callable($request, $response, ...array_values($routeArguments));
return $callable($request, $this->responseFactory->createResponse(), ...array_values($routeArguments));
}
}
2 changes: 0 additions & 2 deletions Slim/Interfaces/InvocationStrategyInterface.php
Expand Up @@ -22,15 +22,13 @@ interface InvocationStrategyInterface
*
* @param callable $callable The callable to invoke using the strategy.
* @param ServerRequestInterface $request The request object.
* @param ResponseInterface $response The response object.
* @param array $routeArguments The route's placeholder arguments
*
* @return ResponseInterface The response from the callable.
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface;
}
5 changes: 2 additions & 3 deletions Slim/Routing/Route.php
Expand Up @@ -143,7 +143,7 @@ public function __construct(
$this->responseFactory = $responseFactory;
$this->callableResolver = $callableResolver;
$this->container = $container;
$this->invocationStrategy = $invocationStrategy ?? new RequestResponse();
$this->invocationStrategy = $invocationStrategy ?? new RequestResponse($responseFactory);
$this->groups = $groups;
$this->identifier = 'route' . $identifier;
$this->middlewareDispatcher = new MiddlewareDispatcher($this, $callableResolver, $container);
Expand Down Expand Up @@ -368,7 +368,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$strategy = new RequestHandler();
}

$response = $this->responseFactory->createResponse();
return $strategy($callable, $request, $response, $this->arguments);
return $strategy($callable, $request, $this->arguments);
}
}
2 changes: 1 addition & 1 deletion Slim/Routing/RouteCollector.php
Expand Up @@ -105,7 +105,7 @@ public function __construct(
$this->responseFactory = $responseFactory;
$this->callableResolver = $callableResolver;
$this->container = $container;
$this->defaultInvocationStrategy = $defaultInvocationStrategy ?? new RequestResponse();
$this->defaultInvocationStrategy = $defaultInvocationStrategy ?? new RequestResponse($responseFactory);
$this->routeParser = $routeParser ?? new RouteParser($this);

if ($cacheFile) {
Expand Down
8 changes: 6 additions & 2 deletions tests/AppTest.php
Expand Up @@ -1269,7 +1269,9 @@ public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgS
$responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal());

$app = new App($responseFactoryProphecy->reveal());
$app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs());
$app->getRouteCollector()->setDefaultInvocationStrategy(
new RequestResponseArgs($responseFactoryProphecy->reveal())
);
$app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $name) {
$response->getBody()->write("Hello {$name}");
return $response;
Expand Down Expand Up @@ -1583,7 +1585,9 @@ public function testCurrentRequestAttributesAreNotLostWhenAddingRouteArgumentsRe
$responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal());

$app = new App($responseFactoryProphecy->reveal());
$app->getRouteCollector()->setDefaultInvocationStrategy(new RequestResponseArgs());
$app->getRouteCollector()->setDefaultInvocationStrategy(
new RequestResponseArgs($responseFactoryProphecy->reveal())
);
$app->get('/Hello/{name}', function (ServerRequestInterface $request, ResponseInterface $response, $name) {
$response->getBody()->write($request->getAttribute('greeting') . ' ' . $name);
return $response;
Expand Down
18 changes: 15 additions & 3 deletions tests/Mocks/InvocationStrategyTest.php
Expand Up @@ -9,6 +9,7 @@

namespace Slim\Tests\Mocks;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;
Expand All @@ -17,23 +18,34 @@ class InvocationStrategyTest implements InvocationStrategyInterface
{
public static $LastCalledFor = null;

/**
* @var ResponseFactoryInterface
*/
private $responseFactory;

/**
* @param ResponseFactoryInterface $responseFactory
*/
public function __construct(ResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

/**
* Invoke a route callable.
*
* @param callable $callable The callable to invoke using the strategy.
* @param ServerRequestInterface $request The request object.
* @param ResponseInterface $response The response object.
* @param array $routeArguments The route's placeholder arguments
*
* @return ResponseInterface The response from the callable.
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
static::$LastCalledFor = $callable;
return $response;
return $this->responseFactory->createResponse();
}
}
1 change: 0 additions & 1 deletion tests/Mocks/MockCustomRequestHandlerInvocationStrategy.php
Expand Up @@ -20,7 +20,6 @@ class MockCustomRequestHandlerInvocationStrategy implements RequestHandlerInvoca
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
): ResponseInterface {
self::$CalledCount += 1;
Expand Down
23 changes: 8 additions & 15 deletions tests/Routing/RouteTest.php
Expand Up @@ -293,7 +293,7 @@ public function testAddMiddlewareOnGroup()
->shouldBeCalledOnce();

$routeCollectorProxyProphecy = $this->prophesize(RouteCollectorProxyInterface::class);
$strategy = new RequestResponse();
$strategy = new RequestResponse($responseFactoryProphecy->reveal());

$called = 0;
$mw = function (ServerRequestInterface $request, RequestHandlerInterface $handler) use (&$called) {
Expand Down Expand Up @@ -537,7 +537,7 @@ public function testInvokeDeferredCallableWithNoContainer()
->shouldBeCalledOnce();

$callableResolver = new CallableResolver();
$invocationStrategy = new InvocationStrategyTest();
$invocationStrategy = new InvocationStrategyTest($responseFactoryProphecy->reveal());

$deferred = '\Slim\Tests\Mocks\CallableTest:toCall';
$route = new Route(
Expand Down Expand Up @@ -575,7 +575,7 @@ public function testInvokeDeferredCallableWithContainer()
$containerProphecy->get('\Slim\Tests\Mocks\CallableTest')->willReturn(new CallableTest());

$callableResolver = new CallableResolver($containerProphecy->reveal());
$strategy = new InvocationStrategyTest();
$strategy = new InvocationStrategyTest($responseFactoryProphecy->reveal());

$deferred = '\Slim\Tests\Mocks\CallableTest:toCall';
$route = new Route(
Expand All @@ -597,13 +597,10 @@ public function testInvokeDeferredCallableWithContainer()

public function testInvokeUsesRequestHandlerStrategyForRequestHandlers()
{
$responseProphecy = $this->prophesize(ResponseInterface::class);

$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$responseFactoryProphecy
->createResponse()
->willReturn($responseProphecy->reveal())
->shouldBeCalledOnce();
->shouldNotBeCalled();

$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has(RequestHandlerTest::class)->willReturn(true);
Expand Down Expand Up @@ -634,13 +631,10 @@ public function testInvokeUsesRequestHandlerStrategyForRequestHandlers()

public function testInvokeUsesUserSetStrategyForRequestHandlers()
{
$responseProphecy = $this->prophesize(ResponseInterface::class);

$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$responseFactoryProphecy
->createResponse()
->willReturn($responseProphecy->reveal())
->shouldBeCalledOnce();
->shouldNotBeCalled();

$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has(RequestHandlerTest::class)->willReturn(true);
Expand Down Expand Up @@ -676,8 +670,7 @@ public function testRequestHandlerStrategyAppendsRouteArgumentsAsAttributesToReq
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$responseFactoryProphecy
->createResponse()
->willReturn($responseProphecy->reveal())
->shouldBeCalledOnce();
->shouldNotBeCalled();

$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has(RequestHandlerTest::class)->willReturn(true);
Expand Down Expand Up @@ -774,7 +767,7 @@ public function testChangingCallableWithContainer()
$containerProphecy->get('CallableTest2')->willReturn(new CallableTest());

$callableResolver = new CallableResolver($containerProphecy->reveal());
$strategy = new InvocationStrategyTest();
$strategy = new InvocationStrategyTest($responseFactoryProphecy->reveal());

$deferred = 'NonExistent:toCall';
$route = new Route(
Expand Down Expand Up @@ -839,7 +832,7 @@ public function testRouteCallableIsResolvedUsingContainerWhenCallableResolverIsP
});

$callableResolver = new CallableResolver($containerProphecy->reveal());
$strategy = new InvocationStrategyTest();
$strategy = new InvocationStrategyTest($responseFactoryProphecy->reveal());

$deferred = 'CallableTest3';
$route = new Route(
Expand Down

0 comments on commit f35d601

Please sign in to comment.