Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Implemented Alt Restful Route Syntax #24

Merged
merged 2 commits into from
Apr 4, 2014
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
15 changes: 5 additions & 10 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,14 @@ Defaults to `twig`. Will be appended to template name given in `render()` method

// how to integrate the Slim middleware
$app->addRoutes(array(
'/' => array(
'Home:index',
function() {
error_log("MIDDLWARE FOR SINGLE ROUTE");
'/' => array('Home:index', array(function() {
error_log("MIDDLEWARE FOR SINGLE ROUTE");
},
function() {
error_log("ADDITIONAL MIDDLWARE FOR SINGLE ROUTE");
}
error_log("ADDITIONAL MIDDLEWARE FOR SINGLE ROUTE");
})
),
'/hello/:name' => array(
'Home:hello',
'post',
function() {
'/hello/:name' => array('post' => 'Home:hello', function() {
error_log("THIS ROUTE IS ONLY POST");
}
)
Expand Down
56 changes: 30 additions & 26 deletions src/SlimController/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Slim extends \Slim\Slim
/**
* @var array
*/
protected static $ALLOWED_HTTP_METHODS = array('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD');
protected static $ALLOWED_HTTP_METHODS = array('ANY', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD');

/**
* Add multiple controller based routes
Expand Down Expand Up @@ -57,43 +57,47 @@ class Slim extends \Slim\Slim
* ), function() {});
* </code>
*
* @param array $routes The route definitions
* @param callable,... $middlewares Optional callable used for all routes as middleware
*
* @param array $routes The route definitions
* @param array $globalMiddlewares
* @throws \InvalidArgumentException
* @internal param $callable ,... $middlewares Optional callable used for all routes as middleware
*
* @return $this
*/
public function addRoutes(array $routes, $middlewares = null)
public function addRoutes(array $routes, $globalMiddlewares = array())
{
$args = func_get_args();
array_shift($args);
$middlewares = $args;
if (!is_array($globalMiddlewares)) {
$globalMiddlewares = array($globalMiddlewares);
};

foreach ($routes as $path => $routeArgs) {
$httpMethod = 'any';

// simple
if (!is_array($routeArgs)) {
$routeArgs = array($routeArgs);
}

$classRoute = array_shift($routeArgs);
// create array for simple request
$routeArgs = (is_array($routeArgs)) ? $routeArgs : array('any' => $routeArgs);

foreach ($routeArgs as $httpMethod => $classArgs) {
// assign vars if middleware callback exists
if(is_array($classArgs)) {
$classRoute = $classArgs[0];
$localMiddlewares = (is_array($classArgs[1])) ? $classArgs[1] : array($classArgs[1]);
} else {
$classRoute = $classArgs;
$localMiddlewares = array();
}

// specific HTTP method
if (count($routeArgs) > 0 && is_string($routeArgs[0])) {
$httpMethod = strtoupper(array_shift($routeArgs));
// specific HTTP method
$httpMethod = strtoupper($httpMethod);
if (!in_array($httpMethod, static::$ALLOWED_HTTP_METHODS)) {
throw new \InvalidArgumentException("Http method '$httpMethod' is not supported.");
}
}

$routeMiddlewares = array_merge($routeArgs, $middlewares);
$route = $this->addControllerRoute($path, $classRoute, $routeMiddlewares)->name($classRoute);
$routeMiddlewares = array_merge($localMiddlewares, $globalMiddlewares);
$route = $this->addControllerRoute($path, $classRoute, $routeMiddlewares)->name($classRoute);

if ('any' === $httpMethod) {
call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS);
} else {
$route->via($httpMethod);
if ('any' === $httpMethod) {
call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS);
} else {
$route->via($httpMethod);
}
}
}

Expand Down
34 changes: 26 additions & 8 deletions tests/SlimController/Tests/SlimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testAddRoutesInExtendedFormat()
{
$this->setUrl('/bla');
$this->app->addRoutes(array(
'/bla' => array('Controller:index', 'get')
'/bla' => array('get' => 'Controller:index')
));
$this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri())));
}
Expand Down Expand Up @@ -80,9 +80,9 @@ public function testLocalMiddlewareIsAddedToRoute()
{
$this->setUrl('/bla');
$this->app->addRoutes(array(
'/bla' => array('Controller:index', function() {
'/bla' => array('get' => array('Controller:index', function() {
return false;
})
}))
));

/** @var \Slim\Route[] $routes */
Expand All @@ -94,18 +94,36 @@ public function testLocalMiddlewareIsAddedToRoute()
$this->assertSame(1, count($middleware));
}

public function testArrayOfLocalMiddlewareIsAddedToRoute()
{
$this->setUrl('/bla');
$this->app->addRoutes(array(
'/bla' => array('get' => array('Controller:index', [function() {
return false;
}, function() { return false; } ]))
));

/** @var \Slim\Route[] $routes */
$routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri());
$this->assertEquals(1, count($routes));

$middleware = $routes[0]->getMiddleware();
$this->assertInternalType('array', $middleware);
$this->assertSame(2, count($middleware));
}

public function testGlobalAndLocalMiddlewareIsAddedToRoute()
{
$this->setUrl('/bla');
$this->app->addRoutes(array(
'/bla' => array('Controller:index', function() {
'/bla' => array('get' => array('Controller:index', function() {
return false;
})
), function() {
}))
), array(function() {
return false;
}, function() {
return false;
});
}));

/** @var \Slim\Route[] $routes */
$routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri());
Expand All @@ -124,7 +142,7 @@ public function testFailToAddRouteForUnsupportedHttpMethod()
{
$this->setUrl('/bla');
$this->app->addRoutes(array(
'/bla' => array('Controller:index', 'foo')
'/bla' => array('foo' => 'Controller:index')
));
}

Expand Down