Add MethodMapInterface
This commit is contained in:
parent
cb87660548
commit
45b13691a2
|
|
@ -5,20 +5,28 @@ namespace WellRESTed\Routing;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
class MethodMap implements MiddlewareInterface
|
class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
{
|
{
|
||||||
protected $map;
|
protected $map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $map
|
* @param array $map
|
||||||
*/
|
*/
|
||||||
public function __construct(array $map = null)
|
public function __construct($map = null)
|
||||||
{
|
{
|
||||||
$this->map = [];
|
$this->map = [];
|
||||||
if ($map) {
|
if ($map) {
|
||||||
foreach ($map as $method => $middleware) {
|
$this->addMap($map);
|
||||||
$this->add($method, $middleware);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $map
|
||||||
|
*/
|
||||||
|
public function addMap($map)
|
||||||
|
{
|
||||||
|
foreach ($map as $method => $middleware) {
|
||||||
|
$this->add($method, $middleware);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Routing;
|
||||||
|
|
||||||
|
interface MethodMapInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array $map
|
||||||
|
*/
|
||||||
|
public function addMap($map);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method
|
||||||
|
* @param mixed $middleware
|
||||||
|
*/
|
||||||
|
public function add($method, $middleware);
|
||||||
|
}
|
||||||
|
|
@ -44,7 +44,9 @@ class Router implements MiddlewareInterface
|
||||||
public function add($target, $middleware, $extra = null)
|
public function add($target, $middleware, $extra = null)
|
||||||
{
|
{
|
||||||
if (is_array($middleware)) {
|
if (is_array($middleware)) {
|
||||||
$middleware = $this->getMethodMap($middleware);
|
$map = $this->getMethodMap();
|
||||||
|
$map->addMap($middleware);
|
||||||
|
$middleware = $map;
|
||||||
}
|
}
|
||||||
$this->routeFactory->registerRoute($this->routeTable, $target, $middleware, $extra);
|
$this->routeFactory->registerRoute($this->routeTable, $target, $middleware, $extra);
|
||||||
}
|
}
|
||||||
|
|
@ -70,9 +72,12 @@ class Router implements MiddlewareInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getMethodMap(array $map)
|
/**
|
||||||
|
* @return MethodMapInterface
|
||||||
|
*/
|
||||||
|
protected function getMethodMap()
|
||||||
{
|
{
|
||||||
return new MethodMap($map);
|
return new MethodMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue