Skip to content

Commit

Permalink
Added syntactic sugar for route collector
Browse files Browse the repository at this point in the history
Add RouteCollector->get($route, $handler) etc for the common HTTP
request methods.
  • Loading branch information
ragboyjr authored and nikic committed Sep 3, 2016
1 parent 8ea9281 commit 7b15535
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,78 @@ public function addRoute($httpMethod, $route, $handler) {
}
}
}

/**
* Adds a GET route to the collection
*
* This is simply an alias of $this->addRoute('GET', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function get($route, $handler) {
$this->addRoute('GET', $route, $handler);
}

/**
* Adds a POST route to the collection
*
* This is simply an alias of $this->addRoute('POST', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function post($route, $handler) {
$this->addRoute('POST', $route, $handler);
}

/**
* Adds a PUT route to the collection
*
* This is simply an alias of $this->addRoute('PUT', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function put($route, $handler) {
$this->addRoute('PUT', $route, $handler);
}

/**
* Adds a DELETE route to the collection
*
* This is simply an alias of $this->addRoute('DELETE', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function delete($route, $handler) {
$this->addRoute('DELETE', $route, $handler);
}

/**
* Adds a PATCH route to the collection
*
* This is simply an alias of $this->addRoute('PATCH', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function patch($route, $handler) {
$this->addRoute('PATCH', $route, $handler);
}

/**
* Adds a HEAD route to the collection
*
* This is simply an alias of $this->addRoute('HEAD', $route, $handler)
*
* @param string $route
* @param mixed $handler
*/
public function head($route, $handler) {
$this->addRoute('HEAD', $route, $handler);
}

/**
* Returns the collected route data, as provided by the data generator.
Expand Down
35 changes: 35 additions & 0 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FastRoute;

class RouteCollectorTest extends \PHPUnit_Framework_TestCase {
public function testShortcuts() {
$r = new DummyRouteCollector();

$r->delete('/delete', 'delete');
$r->get('/get', 'get');
$r->head('/head', 'head');
$r->patch('/patch', 'patch');
$r->post('/post', 'post');
$r->put('/put', 'put');

$expected = [
['DELETE', '/delete', 'delete'],
['GET', '/get', 'get'],
['HEAD', '/head', 'head'],
['PATCH', '/patch', 'patch'],
['POST', '/post', 'post'],
['PUT', '/put', 'put'],
];

$this->assertSame($expected, $r->routes);
}
}

class DummyRouteCollector extends RouteCollector {
public $routes = [];
public function __construct() {}
public function addRoute($method, $route, $handler) {
$this->routes[] = [$method, $route, $handler];
}
}

0 comments on commit 7b15535

Please sign in to comment.