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
18f253f2
Commit
18f253f2
authored
Jun 12, 2015
by
lschricke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added factory for RouteCollection, makes subclassing of RouteCollection possible.
parent
5b7f26c2
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
4 deletions
+58
-4
src/Silex/Application.php
src/Silex/Application.php
+5
-2
src/Silex/ControllerCollection.php
src/Silex/ControllerCollection.php
+8
-2
tests/Silex/Tests/ApplicationTest.php
tests/Silex/Tests/ApplicationTest.php
+20
-0
tests/Silex/Tests/ControllerCollectionTest.php
tests/Silex/Tests/ControllerCollectionTest.php
+25
-0
No files found.
src/Silex/Application.php
View file @
18f253f2
...
...
@@ -64,8 +64,11 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
$app
=
$this
;
$this
[
'routes
'
]
=
function
()
{
$this
[
'routes
_factory'
]
=
$this
->
factory
(
function
()
{
return
new
RouteCollection
();
});
$this
[
'routes'
]
=
function
()
use
(
$app
)
{
return
$app
[
'routes_factory'
];
};
$this
[
'controllers'
]
=
function
()
use
(
$app
)
{
...
...
@@ -73,7 +76,7 @@ class Application extends Container implements HttpKernelInterface, TerminableIn
};
$this
[
'controllers_factory'
]
=
$this
->
factory
(
function
()
use
(
$app
)
{
return
new
ControllerCollection
(
$app
[
'route_factory'
]);
return
new
ControllerCollection
(
$app
[
'route_factory'
]
,
$app
[
'routes_factory'
]
);
});
$this
[
'route_class'
]
=
'Silex\\Route'
;
...
...
src/Silex/ControllerCollection.php
View file @
18f253f2
...
...
@@ -42,10 +42,12 @@ class ControllerCollection
protected
$defaultRoute
;
protected
$defaultController
;
protected
$prefix
;
protected
$routesFactory
;
public
function
__construct
(
Route
$defaultRoute
)
public
function
__construct
(
Route
$defaultRoute
,
$routesFactory
=
null
)
{
$this
->
defaultRoute
=
$defaultRoute
;
$this
->
routesFactory
=
$routesFactory
;
$this
->
defaultController
=
function
(
Request
$request
)
{
throw
new
\LogicException
(
sprintf
(
'The "%s" route must have code to run when it matches.'
,
$request
->
attributes
->
get
(
'_route'
)));
};
...
...
@@ -186,7 +188,11 @@ class ControllerCollection
*/
public
function
flush
(
$prefix
=
''
)
{
if
(
null
===
$this
->
routesFactory
)
{
$routes
=
new
RouteCollection
();
}
else
{
$routes
=
$this
->
routesFactory
;
}
foreach
(
$this
->
controllers
as
$controller
)
{
if
(
$controller
instanceof
Controller
)
{
...
...
tests/Silex/Tests/ApplicationTest.php
View file @
18f253f2
...
...
@@ -23,6 +23,7 @@ use Symfony\Component\HttpFoundation\Response;
use
Symfony\Component\HttpFoundation\StreamedResponse
;
use
Symfony\Component\HttpKernel\HttpKernelInterface
;
use
Symfony\Component\EventDispatcher\Event
;
use
Symfony\Component\Routing\RouteCollection
;
/**
* Application test cases.
...
...
@@ -635,6 +636,21 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
'Hello view listener'
,
$response
->
getContent
());
}
public
function
testDefaultRoutesFactory
()
{
$app
=
new
Application
();
$this
->
assertInstanceOf
(
'Symfony\Component\Routing\RouteCollection'
,
$app
[
'routes'
]);
}
public
function
testOverriddenRoutesFactory
()
{
$app
=
new
Application
();
$app
[
'routes_factory'
]
=
$app
->
factory
(
function
()
{
return
new
RouteCollectionSubClass
();
});
$this
->
assertInstanceOf
(
'Silex\Tests\RouteCollectionSubClass'
,
$app
[
'routes'
]);
}
}
class
FooController
...
...
@@ -652,3 +668,7 @@ class IncorrectControllerCollection implements ControllerProviderInterface
return
;
}
}
class
RouteCollectionSubClass
extends
RouteCollection
{
}
tests/Silex/Tests/ControllerCollectionTest.php
View file @
18f253f2
...
...
@@ -11,10 +11,12 @@
namespace
Silex\Tests
;
use
Silex\Application
;
use
Silex\Controller
;
use
Silex\ControllerCollection
;
use
Silex\Exception\ControllerFrozenException
;
use
Silex\Route
;
use
Symfony\Component\Routing\RouteCollection
;
/**
* ControllerCollection test cases.
...
...
@@ -194,6 +196,25 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
array
(
'before'
),
$c2
->
getRoute
()
->
getOption
(
'_before_middlewares'
));
$this
->
assertEquals
(
array
(
'before'
),
$c3
->
getRoute
()
->
getOption
(
'_before_middlewares'
));
}
public
function
testRoutesFactoryOmitted
()
{
$controllers
=
new
ControllerCollection
(
new
Route
());
$routes
=
$controllers
->
flush
();
$this
->
assertInstanceOf
(
'Symfony\Component\Routing\RouteCollection'
,
$routes
);
}
public
function
testRoutesFactoryInConstructor
()
{
$app
=
new
Application
();
$app
[
'routes_factory'
]
=
$app
->
factory
(
function
()
{
return
new
RouteCollectionSubClass2
();
});
$controllers
=
new
ControllerCollection
(
new
Route
(),
$app
[
'routes_factory'
]);
$routes
=
$controllers
->
flush
();
$this
->
assertInstanceOf
(
'Silex\Tests\RouteCollectionSubClass2'
,
$routes
);
}
}
class
MyRoute1
extends
Route
...
...
@@ -205,3 +226,7 @@ class MyRoute1 extends Route
$this
->
foo
=
$value
;
}
}
class
RouteCollectionSubClass2
extends
RouteCollection
{
}
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