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 @@
...
@@ -12,7 +12,6 @@
namespace
Silex
;
namespace
Silex
;
use
Silex\Exception\ControllerFrozenException
;
use
Silex\Exception\ControllerFrozenException
;
use
Symfony\Component\Routing\Route
;
/**
/**
* A wrapper for a controller, mapped to a route.
* A wrapper for a controller, mapped to a route.
...
@@ -21,8 +20,7 @@ use Symfony\Component\Routing\Route;
...
@@ -21,8 +20,7 @@ use Symfony\Component\Routing\Route;
*/
*/
class
Controller
class
Controller
{
{
protected
$route
;
private
$route
;
private
$routeName
;
private
$routeName
;
private
$isFrozen
=
false
;
private
$isFrozen
=
false
;
...
@@ -84,7 +82,7 @@ class Controller
...
@@ -84,7 +82,7 @@ class Controller
*/
*/
public
function
assert
(
$variable
,
$regexp
)
public
function
assert
(
$variable
,
$regexp
)
{
{
$this
->
route
->
setRequiremen
t
(
$variable
,
$regexp
);
$this
->
route
->
asser
t
(
$variable
,
$regexp
);
return
$this
;
return
$this
;
}
}
...
@@ -99,7 +97,7 @@ class Controller
...
@@ -99,7 +97,7 @@ class Controller
*/
*/
public
function
value
(
$variable
,
$default
)
public
function
value
(
$variable
,
$default
)
{
{
$this
->
route
->
setDefault
(
$variable
,
$default
);
$this
->
route
->
value
(
$variable
,
$default
);
return
$this
;
return
$this
;
}
}
...
@@ -114,9 +112,7 @@ class Controller
...
@@ -114,9 +112,7 @@ class Controller
*/
*/
public
function
convert
(
$variable
,
$callback
)
public
function
convert
(
$variable
,
$callback
)
{
{
$converters
=
$this
->
route
->
getOption
(
'_converters'
);
$this
->
route
->
convert
(
$variable
,
$callback
);
$converters
[
$variable
]
=
$callback
;
$this
->
route
->
setOption
(
'_converters'
,
$converters
);
return
$this
;
return
$this
;
}
}
...
@@ -130,7 +126,7 @@ class Controller
...
@@ -130,7 +126,7 @@ class Controller
*/
*/
public
function
method
(
$method
)
public
function
method
(
$method
)
{
{
$this
->
route
->
setRequirement
(
'_method'
,
$method
);
$this
->
route
->
method
(
$method
);
return
$this
;
return
$this
;
}
}
...
@@ -142,7 +138,7 @@ class Controller
...
@@ -142,7 +138,7 @@ class Controller
*/
*/
public
function
requireHttp
()
public
function
requireHttp
()
{
{
$this
->
route
->
setRequirement
(
'_scheme'
,
'http'
);
$this
->
route
->
requireHttp
(
);
return
$this
;
return
$this
;
}
}
...
@@ -154,7 +150,7 @@ class Controller
...
@@ -154,7 +150,7 @@ class Controller
*/
*/
public
function
requireHttps
()
public
function
requireHttps
()
{
{
$this
->
route
->
setRequirement
(
'_scheme'
,
'https'
);
$this
->
route
->
requireHttps
(
);
return
$this
;
return
$this
;
}
}
...
@@ -169,9 +165,7 @@ class Controller
...
@@ -169,9 +165,7 @@ class Controller
*/
*/
public
function
middleware
(
$callback
)
public
function
middleware
(
$callback
)
{
{
$middlewareCallbacks
=
$this
->
route
->
getOption
(
'_middlewares'
);
$this
->
route
->
middleware
(
$callback
);
$middlewareCallbacks
[]
=
$callback
;
$this
->
route
->
setOption
(
'_middlewares'
,
$middlewareCallbacks
);
return
$this
;
return
$this
;
}
}
...
@@ -186,7 +180,7 @@ class Controller
...
@@ -186,7 +180,7 @@ class Controller
$this
->
isFrozen
=
true
;
$this
->
isFrozen
=
true
;
}
}
p
rotected
function
generateRouteName
(
$prefix
)
p
ublic
function
generateRouteName
(
$prefix
)
{
{
$requirements
=
$this
->
route
->
getRequirements
();
$requirements
=
$this
->
route
->
getRequirements
();
$method
=
isset
(
$requirements
[
'_method'
])
?
$requirements
[
'_method'
]
:
''
;
$method
=
isset
(
$requirements
[
'_method'
])
?
$requirements
[
'_method'
]
:
''
;
...
...
src/Silex/ControllerCollection.php
View file @
eaf176d3
...
@@ -12,7 +12,6 @@
...
@@ -12,7 +12,6 @@
namespace
Silex
;
namespace
Silex
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\RouteCollection
;
use
Symfony\Component\Routing\Route
;
use
Silex\Controller
;
use
Silex\Controller
;
/**
/**
...
@@ -25,9 +24,10 @@ use Silex\Controller;
...
@@ -25,9 +24,10 @@ use Silex\Controller;
* @author Igor Wiedler <igor@wiedler.ch>
* @author Igor Wiedler <igor@wiedler.ch>
* @author Fabien Potencier <fabien@symfony.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
*/
class
ControllerCollection
extends
Controller
class
ControllerCollection
{
{
protected
$controllers
=
array
();
protected
$controllers
=
array
();
protected
$defaultRoute
;
/**
/**
* Constructor.
* Constructor.
...
@@ -36,7 +36,7 @@ class ControllerCollection extends Controller
...
@@ -36,7 +36,7 @@ class ControllerCollection extends Controller
*/
*/
public
function
__construct
()
public
function
__construct
()
{
{
parent
::
__construct
(
new
Route
(
''
)
);
$this
->
defaultRoute
=
new
Route
(
''
);
}
}
/**
/**
...
@@ -51,7 +51,7 @@ class ControllerCollection extends Controller
...
@@ -51,7 +51,7 @@ class ControllerCollection extends Controller
*/
*/
public
function
match
(
$pattern
,
$to
)
public
function
match
(
$pattern
,
$to
)
{
{
$route
=
clone
$this
->
r
oute
;
$route
=
clone
$this
->
defaultR
oute
;
$route
->
setPattern
(
$pattern
);
$route
->
setPattern
(
$pattern
);
$route
->
setDefault
(
'_controller'
,
$to
);
$route
->
setDefault
(
'_controller'
,
$to
);
...
@@ -113,11 +113,16 @@ class ControllerCollection extends Controller
...
@@ -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
)
public
function
assert
(
$variable
,
$regexp
)
{
{
parent
::
assert
(
$variable
,
$regexp
);
$this
->
defaultRoute
->
assert
(
$variable
,
$regexp
);
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
assert
(
$variable
,
$regexp
);
$controller
->
assert
(
$variable
,
$regexp
);
...
@@ -127,11 +132,16 @@ class ControllerCollection extends Controller
...
@@ -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
)
public
function
value
(
$variable
,
$default
)
{
{
parent
::
value
(
$variable
,
$default
);
$this
->
defaultRoute
->
value
(
$variable
,
$default
);
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
value
(
$variable
,
$default
);
$controller
->
value
(
$variable
,
$default
);
...
@@ -141,11 +151,16 @@ class ControllerCollection extends Controller
...
@@ -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
)
public
function
convert
(
$variable
,
$callback
)
{
{
parent
::
convert
(
$variable
,
$callback
);
$this
->
defaultRoute
->
convert
(
$variable
,
$callback
);
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
convert
(
$variable
,
$callback
);
$controller
->
convert
(
$variable
,
$callback
);
...
@@ -155,11 +170,15 @@ class ControllerCollection extends Controller
...
@@ -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
)
public
function
method
(
$method
)
{
{
parent
::
method
(
$method
);
$this
->
defaultRoute
->
method
(
$method
);
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
method
(
$method
);
$controller
->
method
(
$method
);
...
@@ -169,11 +188,13 @@ class ControllerCollection extends Controller
...
@@ -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
()
public
function
requireHttp
()
{
{
parent
::
requireHttp
();
$this
->
defaultRoute
->
requireHttp
();
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
requireHttp
();
$controller
->
requireHttp
();
...
@@ -183,11 +204,13 @@ class ControllerCollection extends Controller
...
@@ -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
()
public
function
requireHttps
()
{
{
parent
::
requireHttps
();
$this
->
defaultRoute
->
requireHttps
();
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
requireHttps
();
$controller
->
requireHttps
();
...
@@ -197,11 +220,16 @@ class ControllerCollection extends Controller
...
@@ -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
)
public
function
middleware
(
$callback
)
{
{
parent
::
middleware
(
$callback
);
$this
->
defaultRoute
->
middleware
(
$callback
);
foreach
(
$this
->
controllers
as
$controller
)
{
foreach
(
$this
->
controllers
as
$controller
)
{
$controller
->
middleware
(
$callback
);
$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;
...
@@ -14,8 +14,7 @@ namespace Silex\Tests;
use
Silex\Controller
;
use
Silex\Controller
;
use
Silex\ControllerCollection
;
use
Silex\ControllerCollection
;
use
Silex\Exception\ControllerFrozenException
;
use
Silex\Exception\ControllerFrozenException
;
use
Silex\Route
;
use
Symfony\Component\Routing\Route
;
/**
/**
* ControllerCollection test cases.
* ControllerCollection test cases.
...
@@ -70,10 +69,7 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
...
@@ -70,10 +69,7 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$mountedRootController
=
$controllers
->
match
(
'/'
,
function
()
{});
$mountedRootController
=
$controllers
->
match
(
'/'
,
function
()
{});
$mainRootController
=
new
Controller
(
new
Route
(
'/'
));
$mainRootController
=
new
Controller
(
new
Route
(
'/'
));
$r
=
new
\ReflectionObject
(
$mainRootController
);
$mainRootController
->
bind
(
$mainRootController
->
generateRouteName
(
'main_'
));
$m
=
$r
->
getMethod
(
'generateRouteName'
);
$m
->
setAccessible
(
true
);
$mainRootController
->
bind
(
$m
->
invoke
(
$mainRootController
,
'main_'
));
$controllers
->
flush
();
$controllers
->
flush
();
...
...
tests/Silex/Tests/ControllerTest.php
View file @
eaf176d3
...
@@ -12,8 +12,7 @@
...
@@ -12,8 +12,7 @@
namespace
Silex\Tests
;
namespace
Silex\Tests
;
use
Silex\Controller
;
use
Silex\Controller
;
use
Silex\Route
;
use
Symfony\Component\Routing\Route
;
/**
/**
* Controller test cases.
* Controller test cases.
...
@@ -75,10 +74,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
...
@@ -75,10 +74,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
public
function
testDefaultRouteNameGeneration
(
Route
$route
,
$expectedRouteName
)
public
function
testDefaultRouteNameGeneration
(
Route
$route
,
$expectedRouteName
)
{
{
$controller
=
new
Controller
(
$route
);
$controller
=
new
Controller
(
$route
);
$r
=
new
\ReflectionObject
(
$controller
);
$controller
->
bind
(
$controller
->
generateRouteName
(
''
));
$m
=
$r
->
getMethod
(
'generateRouteName'
);
$m
->
setAccessible
(
true
);
$controller
->
bind
(
$m
->
invoke
(
$controller
,
''
));
$this
->
assertEquals
(
$expectedRouteName
,
$controller
->
getRouteName
());
$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