Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Added factory for RouteCollection, makes subclassing of RouteCollection possible. #1175

Merged
merged 1 commit into from Jun 24, 2015

Conversation

lschricke
Copy link
Contributor

Note: replaces #1174, which was targeting a wrong branch (and where I messed a bit with the commits ;)).

The aim of this PR is to allow developpers to subclass Symfony\Component\Routing\RouteCollection to change some of its behavior.

In most of my Silex apps, I figured I don't want the Symfony's RouteCollection regular behavior where if you define two routes with the same name (bad copy-paste for instance), the first route added simply gets overriden by the second. Instead, I would like to be able to throw an Exception if that happens. So I've written a tiny subclass of RouteCollection, but to be able to use it (through a service provider like this for instance (needs to be updated for Silex 2)), I need to be able to replace RouteCollection by my subclass. This PR makes it possible.

Thanks! :)

@@ -64,8 +63,12 @@ public function __construct(array $values = array())

$app = $this;

$this['routes'] = function () {
return new RouteCollection();
$this['routes_class'] = 'Symfony\\Component\\Routing\\RouteCollection';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this as overriding the routes_factory would do the same. So, it's not really a good extension point.

@lschricke
Copy link
Contributor Author

I updated the code and added some tests to cover this new feature as you suggested @fabpot. Do you think the tests provide enough coverage?

@stof, this PR does not solve my use case directly, but using it to have Silex use a subclass of Symfony's RouteCollection like this one seems to work in my tests. Am I missing something?

use Silex\Api\BootableProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\Api\ControllerProviderInterface;
use Silex\Provider\RoutingServiceProvider;
use Silex\Provider\KernelServiceProvider;
use Symfony\Component\Routing\RouteCollection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted back to its original place.

@lschricke
Copy link
Contributor Author

@fabpot PR updated.

@lschricke lschricke force-pushed the allow-routecollection-subclassing branch from 6808670 to 18f253f Compare June 16, 2015 02:08
@lschricke
Copy link
Contributor Author

I squashed my commits and rebased the PR.

@fabpot
Copy link
Member

fabpot commented Jun 24, 2015

Thank you @lschricke.

@fabpot fabpot merged commit 18f253f into silexphp:master Jun 24, 2015
fabpot added a commit that referenced this pull request Jun 24, 2015
… RouteCollection possible. (lschricke)

This PR was merged into the 2.0.x-dev branch.

Discussion
----------

Added factory for RouteCollection, makes subclassing of RouteCollection possible.

Note: replaces #1174, which was targeting a wrong branch (and where I messed a bit with the commits ;)).

--

The aim of this PR is to allow developpers to subclass `Symfony\Component\Routing\RouteCollection` to change some of its behavior.

In most of my Silex apps, I figured I don't want the Symfony's `RouteCollection` regular behavior where if you define two routes with the same name (bad copy-paste for instance), the first route added simply gets overriden by the second. Instead, I would like to be able to throw an Exception if that happens. So I've written [a tiny subclass of RouteCollection](https://github.com/lschricke/symfony-strict-route-collection), but to be able to use it (through [a service provider like this](https://github.com/lschricke/silex-strict-route-collection-service-provider) for instance (needs to be updated for Silex 2)), I need to be able to replace `RouteCollection` by my subclass. This PR makes it possible.

Thanks! :)

Commits
-------

18f253f Added factory for RouteCollection, makes subclassing of RouteCollection possible.
@lschricke
Copy link
Contributor Author

And thank you @fabpot and all the contributors for this awesome tool! :)

@lschricke lschricke deleted the allow-routecollection-subclassing branch June 25, 2015 13:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants