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
24e01cf2
Commit
24e01cf2
authored
Aug 20, 2011
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made the flush mechanism more flexible
parent
089c84b5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
44 deletions
+19
-44
src/Silex/Application.php
src/Silex/Application.php
+7
-7
src/Silex/ControllerCollection.php
src/Silex/ControllerCollection.php
+7
-17
tests/Silex/Tests/ControllerCollectionTest.php
tests/Silex/Tests/ControllerCollectionTest.php
+5
-20
No files found.
src/Silex/Application.php
View file @
24e01cf2
...
...
@@ -69,7 +69,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
});
$this
[
'controllers'
]
=
$this
->
share
(
function
()
use
(
$app
)
{
return
new
ControllerCollection
(
$app
[
'routes'
]
);
return
new
ControllerCollection
();
});
$this
[
'exception_handler'
]
=
$this
->
share
(
function
()
{
...
...
@@ -267,10 +267,12 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
/**
* Flushes the controller collection.
*
* @param string $prefix The route prefix
*/
public
function
flush
()
public
function
flush
(
$prefix
=
''
)
{
$this
[
'
controllers'
]
->
flush
(
);
$this
[
'
routes'
]
->
addCollection
(
$this
[
'controllers'
]
->
flush
(),
$prefix
);
}
/**
...
...
@@ -311,9 +313,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$app
=
$app
();
}
foreach
(
$app
[
'controllers'
]
->
all
()
as
$controller
)
{
$controller
->
getRoute
()
->
setPattern
(
$prefix
.
$controller
->
getRoute
()
->
getPattern
());
}
$app
->
flush
(
$prefix
);
return
$app
->
handle
(
$request
);
};
...
...
@@ -359,7 +359,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this
[
'request'
]
->
isSecure
()
?
$this
[
'request'
]
->
getPort
()
:
$this
[
'request.https_port'
]
);
$this
[
'controllers'
]
->
flush
();
$this
->
flush
();
$matcher
=
new
RedirectableUrlMatcher
(
$this
[
'routes'
],
$this
[
'request_context'
]);
...
...
src/Silex/ControllerCollection.php
View file @
24e01cf2
...
...
@@ -18,19 +18,13 @@ use Symfony\Component\Routing\RouteCollection;
*
* It acts as a staging area for routes. You are able to set the route name
* until flush() is called, at which point all controllers are frozen and
*
added to the
RouteCollection.
*
converted to a
RouteCollection.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class
ControllerCollection
{
private
$controllers
=
array
();
private
$routes
;
public
function
__construct
(
RouteCollection
$routes
)
{
$this
->
routes
=
$routes
;
}
/**
* Adds a controller to the staging area.
...
...
@@ -44,27 +38,23 @@ class ControllerCollection
/**
* Persists and freezes staged controllers.
*
* @return RouteCollection A RouteCollection instance
*/
public
function
flush
()
{
$routes
=
new
RouteCollection
();
foreach
(
$this
->
controllers
as
$controller
)
{
if
(
!
$controller
->
getRouteName
())
{
$controller
->
bindDefaultRouteName
();
}
$
this
->
routes
->
add
(
$controller
->
getRouteName
(),
$controller
->
getRoute
());
$routes
->
add
(
$controller
->
getRouteName
(),
$controller
->
getRoute
());
$controller
->
freeze
();
}
$this
->
controllers
=
array
();
}
/**
* Gets all controllers.
*
* @return array An array of Controllers
*/
public
function
all
()
{
return
$this
->
controllers
;
return
$routes
;
}
}
tests/Silex/Tests/ControllerCollectionTest.php
View file @
24e01cf2
...
...
@@ -29,39 +29,24 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
{
public
function
testGetRouteCollectionWithNoRoutes
()
{
$routes
=
new
RouteCollection
();
$controllers
=
new
ControllerCollection
(
$routes
);
$this
->
assertEquals
(
0
,
count
(
$routes
->
all
()));
$controllers
->
flush
();
$controllers
=
new
ControllerCollection
();
$routes
=
$controllers
->
flush
();
$this
->
assertEquals
(
0
,
count
(
$routes
->
all
()));
}
public
function
testGetRouteCollectionWithRoutes
()
{
$routes
=
new
RouteCollection
();
$controllers
=
new
ControllerCollection
(
$routes
);
$controllers
=
new
ControllerCollection
();
$controllers
->
add
(
new
Controller
(
new
Route
(
'/foo'
)));
$controllers
->
add
(
new
Controller
(
new
Route
(
'/bar'
)));
$this
->
assertEquals
(
0
,
count
(
$routes
->
all
()));
$controllers
->
flush
();
$routes
=
$controllers
->
flush
();
$this
->
assertEquals
(
2
,
count
(
$routes
->
all
()));
}
public
function
testAll
()
{
$controllers
=
new
ControllerCollection
(
new
RouteCollection
());
$controllers
->
add
(
$c1
=
new
Controller
(
new
Route
(
'/foo'
)));
$controllers
->
add
(
$c2
=
new
Controller
(
new
Route
(
'/bar'
)));
$this
->
assertEquals
(
array
(
$c1
,
$c2
),
$controllers
->
all
());
}
public
function
testControllerFreezing
()
{
$routes
=
new
RouteCollection
();
$controllers
=
new
ControllerCollection
(
$routes
);
$controllers
=
new
ControllerCollection
();
$fooController
=
new
Controller
(
new
Route
(
'/foo'
));
$fooController
->
bind
(
'foo'
);
...
...
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