Skip to content

Commit

Permalink
Merge pull request #1345 from opengeek/foundHandler
Browse files Browse the repository at this point in the history
Add foundHandler that implements a new InvocationStrategyInterface
  • Loading branch information
codeguy committed Jul 9, 2015
2 parents 2b6229d + ae77a9a commit 66afb55
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 6 deletions.
10 changes: 4 additions & 6 deletions Slim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Slim\Http\Body;
use Slim\Http\Request;
use Slim\Interfaces\Http\EnvironmentInterface;
use Slim\Interfaces\InvocationStrategyInterface;
use Slim\Interfaces\RouteGroupInterface;
use Slim\Interfaces\RouteInterface;
use Slim\Interfaces\RouterInterface;
Expand Down Expand Up @@ -370,12 +371,9 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
{
$routeInfo = $this->container->get('router')->dispatch($request);
if ($routeInfo[0] === Dispatcher::FOUND) {
// URL decode the named arguments from the router
$attributes = $routeInfo[2];
foreach ($attributes as $k => $v) {
$request = $request->withAttribute($k, urldecode($v));
}
return $routeInfo[1]($request, $response, []);
/** @var InvocationStrategyInterface $handler */
$handler = $this->container->get('foundHandler');
return $handler->invoke($this->container, $routeInfo, $request, $response);
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
/** @var callable $notAllowedHandler */
$notAllowedHandler = $this->container->get('notAllowedHandler');
Expand Down
15 changes: 15 additions & 0 deletions Slim/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Slim\Handlers\Error;
use Slim\Handlers\NotFound;
use Slim\Handlers\NotAllowed;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\Http\EnvironmentInterface;
use Slim\Interfaces\InvocationStrategyInterface;
use Slim\Interfaces\RouterInterface;

/**
Expand All @@ -36,6 +38,7 @@
* - request: an instance of \Psr\Http\Message\ServerRequestInterface
* - response: an instance of \Psr\Http\Message\ResponseInterface
* - router: an instance of \Slim\Interfaces\RouterInterface
* - foundHandler: an instance of \Slim\Interfaces\InvocationStrategyInterface
* - errorHandler: a callable with the signature: function($request, $response, $exception)
* - notFoundHandler: a callable with the signature: function($request, $response)
* - notAllowedHandler: a callable with the signature: function($request, $response, $allowedHttpMethods)
Expand Down Expand Up @@ -144,6 +147,18 @@ function ($c) {
return new Router();
};

/**
* This service MUST return a SHARED instance
* of \Slim\Interfaces\InvocationStrategyInterface.
*
* @param Container $c
*
* @return InvocationStrategyInterface
*/
$this['foundHandler'] = function ($c) {
return new RequestResponse();
};

/**
* This service MUST return a callable
* that accepts three arguments:
Expand Down
41 changes: 41 additions & 0 deletions Slim/Handlers/Strategies/RequestResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the slim package.
*
* Copyright (c) Jason Coward <jason@opengeek.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Slim\Handlers\Strategies;


use Interop\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;

class RequestResponse implements InvocationStrategyInterface
{

/**
* @param ContainerInterface $container
* @param array $route
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return mixed
*/
public function invoke(
ContainerInterface $container,
array $route,
ServerRequestInterface $request,
ResponseInterface $response
) {
foreach ($route[2] as $k => $v) {
$request = $request->withAttribute($k, urldecode($v));
}
return $route[1]($request, $response, []);
}
}
40 changes: 40 additions & 0 deletions Slim/Handlers/Strategies/RequestResponseArgs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the slim package.
*
* Copyright (c) Jason Coward <jason@opengeek.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Slim\Handlers\Strategies;


use Interop\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Interfaces\InvocationStrategyInterface;

class RequestResponseArgs implements InvocationStrategyInterface
{

/**
* @param ContainerInterface $container
* @param array $route
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return mixed
*/
public function invoke(ContainerInterface $container, array $route, ServerRequestInterface $request, ResponseInterface $response)
{
$arguments = [$request, $response];
foreach ($route[2] as $k => $v) {
$decoded = urldecode($v);
$request = $request->withAttribute($k, $decoded);
array_push($arguments, $decoded);
}
return call_user_func_array($route[1], $arguments);
}
}
27 changes: 27 additions & 0 deletions Slim/Interfaces/InvocationStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Slim Framework (http://slimframework.com)
*
* @link https://github.com/codeguy/Slim
* @copyright Copyright (c) 2011-2015 Josh Lockhart
* @license https://github.com/codeguy/Slim/blob/master/LICENSE (MIT License)
*/
namespace Slim\Interfaces;


use Interop\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

interface InvocationStrategyInterface
{
/**
* @param ContainerInterface $container
* @param array $route
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return mixed
*/
public function invoke(ContainerInterface $container, array $route, ServerRequestInterface $request, ResponseInterface $response);
}

0 comments on commit 66afb55

Please sign in to comment.