Skip to content

Commit

Permalink
merged branch Crell/routecollection-count (PR #4642)
Browse files Browse the repository at this point in the history
Commits
-------

c350944 Add the Countable interface to RouteCollection.

Discussion
----------

Add the Countable interface to RouteCollection.

Since RouteCollection is a fancy-pants array of Routes, and it already is iterable, it would be nice if it were also countable.

There may be optimizations to be had here, but I figure this is a decent start.  There should be no BC breaks here, just some DX convenience.

---------------------------------------------------------------------------

by Crell at 2012-06-23T17:01:43Z

You Symfony people and your gratuitous whitespace... :-P

Both fixed and rebased.

---------------------------------------------------------------------------

by travisbot at 2012-06-23T17:25:31Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1689898) (merged 6b588eb3 into 0d4b02e).

---------------------------------------------------------------------------

by travisbot at 2012-06-23T17:26:06Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1689902) (merged abd74cf5 into 0d4b02e).

---------------------------------------------------------------------------

by travisbot at 2012-06-23T18:10:57Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1689975) (merged c350944 into 0d4b02e).

---------------------------------------------------------------------------

by mvrhov at 2012-06-24T07:11:12Z

@Crell running [php-cs-fixer](http://cs.sensiolabs.org/) on your changes will prevent you from being stoffed :P about CS

---------------------------------------------------------------------------

by stof at 2012-06-24T09:34:24Z

@mvrhov depends. The CS fixer will replace tabs with 4 spaces, but it will not fix it if using 2 spaces instead of 4. it cannot fix everything

---------------------------------------------------------------------------

by vicb at 2012-06-24T17:14:14Z

@Crell what is your use case ?

---------------------------------------------------------------------------

by fabpot at 2012-06-25T06:59:00Z

I'm not opposed to the change but what's the use case?

---------------------------------------------------------------------------

by lsmith77 at 2012-06-25T11:22:01Z

This is semi related to ideas we have for the dynamic router in the CMF. The 2 main reasons for the existence of the dynamic router are:
1) the fact that end users should be able to define new routes
2) the fact that there might just be too many routes to dump those efficiently to PHP

Now in some cases despite 1) the users might still want to dump key routes or even all routes as part of some deployment process to mod_rewrite or PHP when moving content from a staging database to a production database for better performance and reduced load on the database server.

Also even with the dynamic router it would be good to have tools available like ``app/console router:debug``.

But for both the use case of dumping some/all routes or when listing routes it might be necessary to implement some stop gap solutions to first check if this wouldn't lead to a too big collection. So it could be useful to be able to use whatever efficient solution the dynamic data store has for determining the count before starting to actually read the data from the data source.
  • Loading branch information
fabpot committed Jun 25, 2012
2 parents 03c8d4d + c350944 commit db9a8c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Symfony/Component/Routing/RouteCollection.php
Expand Up @@ -24,7 +24,7 @@
*
* @api
*/
class RouteCollection implements \IteratorAggregate
class RouteCollection implements \IteratorAggregate, \Countable
{
private $routes;
private $resources;
Expand Down Expand Up @@ -88,6 +88,16 @@ public function getIterator()
return new \ArrayIterator($this->routes);
}

/**
* Gets the number of Routes in this collection.
*
* @return int The number of routes in this collection, including nested collections
*/
public function count()
{
return count($this->routes->all());
}

/**
* Adds a route.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Expand Up @@ -76,6 +76,18 @@ public function testIteratorWithOverridenRoutes()
$this->assertEquals('/foo1', $this->getFirstNamedRoute($collection, 'foo')->getPattern());
}

public function testCount()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));

$collection1 = new RouteCollection();
$collection->addCollection($collection1);
$collection1->add('foo', new Route('/foo1'));

$this->assertCount(2, $collection);
}

protected function getFirstNamedRoute(RouteCollection $routeCollection, $name)
{
foreach ($routeCollection as $key => $route) {
Expand Down

0 comments on commit db9a8c1

Please sign in to comment.