Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
eaf176d3
Commit
eaf176d3
authored
May 21, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added a Route class
parent
e3ddad54
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
183 additions
and
45 deletions
+183
-45
src/Silex/Controller.php
src/Silex/Controller.php
+9
-15
src/Silex/ControllerCollection.php
src/Silex/ControllerCollection.php
+46
-18
src/Silex/Route.php
src/Silex/Route.php
+124
-0
tests/Silex/Tests/ControllerCollectionTest.php
tests/Silex/Tests/ControllerCollectionTest.php
+2
-6
tests/Silex/Tests/ControllerTest.php
tests/Silex/Tests/ControllerTest.php
+2
-6
No files found.
src/Silex/Controller.php
View file @
eaf176d3
...
...
@@ -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
->
setRequiremen
t
(
$variable
,
$regexp
);
$this
->
route
->
asser
t
(
$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
;
}
p
rotected
function
generateRouteName
(
$prefix
)
p
ublic
function
generateRouteName
(
$prefix
)
{
$requirements
=
$this
->
route
->
getRequirements
();
$method
=
isset
(
$requirements
[
'_method'
])
?
$requirements
[
'_method'
]
:
''
;
...
...
src/Silex/ControllerCollection.php
View file @
eaf176d3
...
...
@@ -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
->
r
oute
;
$route
=
clone
$this
->
defaultR
oute
;
$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
);
...
...
src/Silex/Route.php
0 → 100644
View file @
eaf176d3
<?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
;
}
}
tests/Silex/Tests/ControllerCollectionTest.php
View file @
eaf176d3
...
...
@@ -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
();
...
...
tests/Silex/Tests/ControllerTest.php
View file @
eaf176d3
...
...
@@ -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
());
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment