Commit eaf176d3 authored by Fabien Potencier's avatar Fabien Potencier

added a Route class

parent e3ddad54
......@@ -12,7 +12,6 @@
namespace Silex;
use Silex\Exception\ControllerFrozenException;
use Symfony\Component\Routing\Route;
/**
* A wrapper for a controller, mapped to a route.
......@@ -21,8 +20,7 @@ use Symfony\Component\Routing\Route;
*/
class Controller
{
protected $route;
private $route;
private $routeName;
private $isFrozen = false;
......@@ -84,7 +82,7 @@ class Controller
*/
public function assert($variable, $regexp)
{
$this->route->setRequirement($variable, $regexp);
$this->route->assert($variable, $regexp);
return $this;
}
......@@ -99,7 +97,7 @@ class Controller
*/
public function value($variable, $default)
{
$this->route->setDefault($variable, $default);
$this->route->value($variable, $default);
return $this;
}
......@@ -114,9 +112,7 @@ class Controller
*/
public function convert($variable, $callback)
{
$converters = $this->route->getOption('_converters');
$converters[$variable] = $callback;
$this->route->setOption('_converters', $converters);
$this->route->convert($variable, $callback);
return $this;
}
......@@ -130,7 +126,7 @@ class Controller
*/
public function method($method)
{
$this->route->setRequirement('_method', $method);
$this->route->method($method);
return $this;
}
......@@ -142,7 +138,7 @@ class Controller
*/
public function requireHttp()
{
$this->route->setRequirement('_scheme', 'http');
$this->route->requireHttp();
return $this;
}
......@@ -154,7 +150,7 @@ class Controller
*/
public function requireHttps()
{
$this->route->setRequirement('_scheme', 'https');
$this->route->requireHttps();
return $this;
}
......@@ -169,9 +165,7 @@ class Controller
*/
public function middleware($callback)
{
$middlewareCallbacks = $this->route->getOption('_middlewares');
$middlewareCallbacks[] = $callback;
$this->route->setOption('_middlewares', $middlewareCallbacks);
$this->route->middleware($callback);
return $this;
}
......@@ -186,7 +180,7 @@ class Controller
$this->isFrozen = true;
}
protected function generateRouteName($prefix)
public function generateRouteName($prefix)
{
$requirements = $this->route->getRequirements();
$method = isset($requirements['_method']) ? $requirements['_method'] : '';
......
......@@ -12,7 +12,6 @@
namespace Silex;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Silex\Controller;
/**
......@@ -25,9 +24,10 @@ use Silex\Controller;
* @author Igor Wiedler <igor@wiedler.ch>
* @author Fabien Potencier <fabien@symfony.com>
*/
class ControllerCollection extends Controller
class ControllerCollection
{
protected $controllers = array();
protected $defaultRoute;
/**
* Constructor.
......@@ -36,7 +36,7 @@ class ControllerCollection extends Controller
*/
public function __construct()
{
parent::__construct(new Route(''));
$this->defaultRoute = new Route('');
}
/**
......@@ -51,7 +51,7 @@ class ControllerCollection extends Controller
*/
public function match($pattern, $to)
{
$route = clone $this->route;
$route = clone $this->defaultRoute;
$route->setPattern($pattern);
$route->setDefault('_controller', $to);
......@@ -113,11 +113,16 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets the requirement for a route variable.
*
* @param string $variable The variable name
* @param string $regexp The regexp to apply
*
* @return Controller $this The current Controller instance
*/
public function assert($variable, $regexp)
{
parent::assert($variable, $regexp);
$this->defaultRoute->assert($variable, $regexp);
foreach ($this->controllers as $controller) {
$controller->assert($variable, $regexp);
......@@ -127,11 +132,16 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets the default value for a route variable.
*
* @param string $variable The variable name
* @param mixed $default The default value
*
* @return Controller $this The current Controller instance
*/
public function value($variable, $default)
{
parent::value($variable, $default);
$this->defaultRoute->value($variable, $default);
foreach ($this->controllers as $controller) {
$controller->value($variable, $default);
......@@ -141,11 +151,16 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets a converter for a route variable.
*
* @param string $variable The variable name
* @param mixed $callback A PHP callback that converts the original value
*
* @return Controller $this The current Controller instance
*/
public function convert($variable, $callback)
{
parent::convert($variable, $callback);
$this->defaultRoute->convert($variable, $callback);
foreach ($this->controllers as $controller) {
$controller->convert($variable, $callback);
......@@ -155,11 +170,15 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets the requirement for the HTTP method.
*
* @param string $method The HTTP method name. Multiple methods can be supplied, delimited by a pipe character '|', eg. 'GET|POST'
*
* @return Controller $this The current Controller instance
*/
public function method($method)
{
parent::method($method);
$this->defaultRoute->method($method);
foreach ($this->controllers as $controller) {
$controller->method($method);
......@@ -169,11 +188,13 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets the requirement of HTTP (no HTTPS) on this controller.
*
* @return Controller $this The current Controller instance
*/
public function requireHttp()
{
parent::requireHttp();
$this->defaultRoute->requireHttp();
foreach ($this->controllers as $controller) {
$controller->requireHttp();
......@@ -183,11 +204,13 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets the requirement of HTTPS on this controller.
*
* @return Controller $this The current Controller instance
*/
public function requireHttps()
{
parent::requireHttps();
$this->defaultRoute->requireHttps();
foreach ($this->controllers as $controller) {
$controller->requireHttps();
......@@ -197,11 +220,16 @@ class ControllerCollection extends Controller
}
/**
* {@inheritDoc}
* Sets a callback to handle before triggering the route callback.
* (a.k.a. "Route Middleware")
*
* @param mixed $callback A PHP callback to be triggered when the Route is matched, just before the route callback
*
* @return Controller $this The current Controller instance
*/
public function middleware($callback)
{
parent::middleware($callback);
$this->defaultRoute->middleware($callback);
foreach ($this->controllers as $controller) {
$controller->middleware($callback);
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\Routing\Route as BaseRoute;
/**
* A wrapper for a controller, mapped to a route.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Route extends BaseRoute
{
/**
* Sets the requirement for a route variable.
*
* @param string $variable The variable name
* @param string $regexp The regexp to apply
*
* @return Controller $this The current Controller instance
*/
public function assert($variable, $regexp)
{
$this->setRequirement($variable, $regexp);
return $this;
}
/**
* Sets the default value for a route variable.
*
* @param string $variable The variable name
* @param mixed $default The default value
*
* @return Controller $this The current Controller instance
*/
public function value($variable, $default)
{
$this->setDefault($variable, $default);
return $this;
}
/**
* Sets a converter for a route variable.
*
* @param string $variable The variable name
* @param mixed $callback A PHP callback that converts the original value
*
* @return Controller $this The current Controller instance
*/
public function convert($variable, $callback)
{
$converters = $this->getOption('_converters');
$converters[$variable] = $callback;
$this->setOption('_converters', $converters);
return $this;
}
/**
* Sets the requirement for the HTTP method.
*
* @param string $method The HTTP method name. Multiple methods can be supplied, delimited by a pipe character '|', eg. 'GET|POST'
*
* @return Controller $this The current Controller instance
*/
public function method($method)
{
$this->setRequirement('_method', $method);
return $this;
}
/**
* Sets the requirement of HTTP (no HTTPS) on this controller.
*
* @return Controller $this The current Controller instance
*/
public function requireHttp()
{
$this->setRequirement('_scheme', 'http');
return $this;
}
/**
* Sets the requirement of HTTPS on this controller.
*
* @return Controller $this The current Controller instance
*/
public function requireHttps()
{
$this->setRequirement('_scheme', 'https');
return $this;
}
/**
* Sets a callback to handle before triggering the route callback.
* (a.k.a. "Route Middleware")
*
* @param mixed $callback A PHP callback to be triggered when the Route is matched, just before the route callback
*
* @return Controller $this The current Controller instance
*/
public function middleware($callback)
{
$middlewareCallbacks = $this->getOption('_middlewares');
$middlewareCallbacks[] = $callback;
$this->setOption('_middlewares', $middlewareCallbacks);
return $this;
}
}
......@@ -14,8 +14,7 @@ namespace Silex\Tests;
use Silex\Controller;
use Silex\ControllerCollection;
use Silex\Exception\ControllerFrozenException;
use Symfony\Component\Routing\Route;
use Silex\Route;
/**
* ControllerCollection test cases.
......@@ -70,10 +69,7 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$mountedRootController = $controllers->match('/', function () {});
$mainRootController = new Controller(new Route('/'));
$r = new \ReflectionObject($mainRootController);
$m = $r->getMethod('generateRouteName');
$m->setAccessible(true);
$mainRootController->bind($m->invoke($mainRootController, 'main_'));
$mainRootController->bind($mainRootController->generateRouteName('main_'));
$controllers->flush();
......
......@@ -12,8 +12,7 @@
namespace Silex\Tests;
use Silex\Controller;
use Symfony\Component\Routing\Route;
use Silex\Route;
/**
* Controller test cases.
......@@ -75,10 +74,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
public function testDefaultRouteNameGeneration(Route $route, $expectedRouteName)
{
$controller = new Controller($route);
$r = new \ReflectionObject($controller);
$m = $r->getMethod('generateRouteName');
$m->setAccessible(true);
$controller->bind($m->invoke($controller, ''));
$controller->bind($controller->generateRouteName(''));
$this->assertEquals($expectedRouteName, $controller->getRouteName());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment