8000 Fluid interface for building routes in PHP by weaverryan · Pull Request #15778 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fluid interface for building routes in PHP #15778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
51f60fc
Adding a new framework-specific Route class
weaverryan Sep 9, 2015
6891ec8
Adding a class to make adding/importing routes easier and more fluid
weaverryan Sep 9, 2015
4e430a0
Maintaining all the RouteCollection abilities to RouteCollectionBuilder
weaverryan Sep 12, 2015
0ddadc5
Moving the prefix to the builder, so that it's consistent with other …
weaverryan Sep 13, 2015
06ea900
Adding phpdoc
weaverryan Sep 13, 2015
6b922a6
Using InvalidArgumentException
weaverryan Sep 13, 2015
14518ed
No change - renaming variable
weaverryan Sep 13, 2015
7972fc9
Adding many more tests, which included a few small bug 8000 fixes with val…
weaverryan Sep 13, 2015
e11b7e0
Removed prefix argument from mount() - and added it instead to create…
weaverryan Sep 13, 2015
4d90916
fabbot!
weaverryan Sep 14, 2015
729ccbb
Renaming flush() to build()
weaverryan Sep 15, 2015
e509953
Not clearing everything on build - unnecessary, and the RouteCollecti…
weaverryan Sep 15, 2015
01e1329
Fixing phpdoc
weaverryan Sep 15, 2015
e39e0c4
Removing the FrameworkBundle Route and the ability to call Route::set…
weaverryan Sep 16, 2015
df1849f
Simplifying by transforming RouteCollection's into RouteCollectionBui…
weaverryan Sep 16, 2015
97b1eea
Fixing a bug with knowing which keys should be auto-generated
weaverryan Sep 16, 2015
e1ecde4
Minor code improvement to centralize things
weaverryan Sep 16, 2015
ecf4346
Renaming methods for clarity and consistency
weaverryan Sep 16, 2015
0dce55d
fabbot and possible test fixes
weaverryan Sep 16, 2015
f3d71ad
Allowing LoaderInterface instead of Loader
weaverryan Sep 17, 2015
8132fcb
Updating setRequirements to avoid deprecated calls
weaverryan Sep 17, 2015
b2676ec
phpdoc typo
weaverryan Sep 26, 2015
bf6790b
Making RouteCollectionBuilder's LoaderInteface optional
weaverryan Sep 30, 2015
61e4bf7
removing extra spaces
weaverryan Sep 30, 2015
8f0b956
moving into the component
weaverryan Sep 30, 2015
fbab6d4
Removing the ability to set a prefix on a builder: that only happens …
weaverryan Sep 30, 2015
012cb92
Removing the prefix from import, and making the user actually put tha…
weaverryan Sep 30, 2015
33255dd
Fixing a bug with route name collission because the prefix wasn't acc…
weaverryan Sep 30, 2015
356b114
Refactoring into private method
weaverryan Sep 30, 2015
7cb8996
fabbot
weaverryan Sep 30, 2015
573a7f1
Small tweaks suggested by fabpot, including removal of addResource(),…
weaverryan Sep 30, 2015
83f3194
stretching out logic into multiple lines: there's some discussion abo…
weaverryan Sep 30, 2015
26d656b
Fabbot
weaverryan Sep 30, 2015
42e73a2
Adding throws
weaverryan Oct 1, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Removing the prefix from import, and making the user actually put tha…
…t into the parent builder
  • Loading branch information
weaverryan committed Sep 30, 2015
commit 012cb92ad792635fb35b160e0870a7df76428b12
48 changes: 16 additions & 32 deletions src/Symfony/Component/Routing/RouteCollectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,31 @@ public function __construct(LoaderInterface $loader = null)
}

/**
* Import an external routing resource, like a file.
* Import an external routing resource and returns the RouteCollectionBuilder
*
* Returns a RouteCollectionBuilder so you can continue to tweak options on the routes.
* $routes->mount('/blog', $routes->import('blog.yml'));
*
* @param mixed $resource
* @param string $prefix
* @param string $type
*
* @return RouteCollectionBuilder
*/
public function import($resource, $prefix = null, $type = null)
public function import($resource, $type = null)
{
/** @var RouteCollection $subCollection */
$subCollection = $this->resolve($resource, $type)->load($resource, $type);
/** @var RouteCollection $collection */
$collection = $this->resolve($resource, $type)->load($resource, $type);

return $this->addRouteCollection($subCollection, $prefix);
// create a builder from the RouteCollection
8000 $builder = $this->createBuilder();
foreach ($collection->all() as $name => $route) {
$builder->addRoute($route, $name);
}

foreach ($collection->getResources() as $resource) {
$builder->addResource($resource);
}

return $builder;
}

/**
Expand Down Expand Up @@ -111,31 +120,6 @@ public function mount($prefix, RouteCollectionBuilder $builder)
$this->routes[] = $builder;
}

/**
* Adds a RouteCollection directly and returns those routes in a RouteCollectionBuilder.
*
* @param RouteCollection $collection
* @param string|null $prefix
*
* @return $this
*/
public function addRouteCollection(RouteCollection $collection, $prefix = null)
{
// create a builder from the RouteCollection
$builder = $this->createBuilder();
foreach ($collection->all() as $name => $route) {
$builder->addRoute($route, $name);
}

foreach ($collection->getResources() as $resource) {
$builder->addResource($resource);
}

$this->mount($prefix, $builder);

return $builder;
}

/**
* Adds a Route object to the builder.
*
Expand Down
56 changes: 20 additions & 36 deletions src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public function testImport()
->with('admin_routing.yml', 'yaml')
->will($this->returnValue($resolvedLoader));

$originalRoute = new Route('/foo/path');
$expectedCollection = new RouteCollection();
$expectedCollection->add('one_test_route', new Route('/foo/path'));
$expectedCollection->add('one_test_route', $originalRoute);

$resolvedLoader
->expects($this->once())
Expand All @@ -41,18 +42,17 @@ public function testImport()
->method('getResolver')
->will($this->returnValue($resolver));

// import the file! (with a prefix)
$collectionBuilder = new RouteCollectionBuilder($loader);
$addedBuilder = $collectionBuilder->import('admin_routing.yml', '/admin', 'yaml');
// import the file!
$routes = new RouteCollectionBuilder($loader);
$importedRoutes = $routes->import('admin_routing.yml', 'yaml');

// we should get back a RouteCollectionBuilder
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $addedBuilder);
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $importedRoutes);

// get the collection back so we can look at it
$addedCollection = $addedBuilder->build();
$addedCollection = $importedRoutes->build();
$route = $addedCollection->get('one_test_route');
$this->assertNotNull($route);
$this->assertEquals('/admin/foo/path', $route->getPath(), 'The prefix should be applied');
$this->assertSame($originalRoute, $route);
}

/**
Expand All @@ -79,14 +79,6 @@ public function testAdd()

public function testFlushOrdering()
{
$loadedCollection1 = new RouteCollection();
$loadedCollection1->add('first_collection_route1', new Route('/collection/first/blog1'));
$loadedCollection1->add('first_collection_route2', new Route('/collection/first/blog2'));

$loadedCollection2 = new RouteCollection();
$loadedCollection2->add('second_collection_route1', new Route('/collection/second/product1'));
$loadedCollection2->add('second_collection_route2', new Route('/collection/second/product2'));

$importedCollection = new RouteCollection();
$importedCollection->add('imported_route1', new Route('/imported/foo1'));
$importedCollection->add('imported_route2', new Route('/imported/foo2'));
Expand All @@ -101,39 +93,31 @@ public function testFlushOrdering()
->method('load')
->will($this->returnValue($importedCollection));

$collectionBuilder = new RouteCollectionBuilder($loader);
$routes = new RouteCollectionBuilder($loader);

// 1) Add a route
$collectionBuilder->add('/checkout', 'AppBundle:Order:checkout', 'checkout_route');
// 2) Add a collection directly
$collectionBuilder->addRouteCollection($loadedCollection1);
// 3) Import from a file
$collectionBuilder->import('admin_routing.yml');
$routes->add('/checkout', 'AppBundle:Order:checkout', 'checkout_route');
// 2) Import from a file
$routes->mount('/', $routes->import('admin_routing.yml'));
// 3) Add another route
$routes->add('/', 'AppBundle:Default:homepage', 'homepage');
// 4) Add another route
$collectionBuilder->add('/', 'AppBundle:Default:homepage', 'homepage');
// 5) Add another collection
$collectionBuilder->addRouteCollection($loadedCollection2);
// 6) Add another route
$collectionBuilder->add('/admin', 'AppBundle:Admin:dashboard', 'admin_dashboard');
$routes->add('/admin',  A60B 9;AppBundle:Admin:dashboard', 'admin_dashboard');

// set a default value
$collectionBuilder->setDefault('_locale', 'fr');
$routes->setDefault('_locale', 'fr');
// set an extra resource
$collectionBuilder->addResource(new FileResource('foo_routing.xml'));
$routes->addResource(new FileResource('foo_routing.xml'));

$actualCollection = $collectionBuilder->build();
$actualCollection = $routes->build();

$this->assertCount(9, $actualCollection);
$this->assertCount(5, $actualCollection);
$actualRouteNames = array_keys($actualCollection->all());
$this->assertEquals(array(
'checkout_route',
'first_collection_route1',
'first_collection_route2',
'imported_route1',
'imported_route2',
'homepage',
'second_collection_route1',
'second_collection_route2',
'admin_dashboard',
), $actualRouteNames);

Expand Down Expand Up @@ -304,7 +288,7 @@ public function testFlushSetsPrefixedWithMultipleLevels()
->method('load')
->will($this->returnValue($importedCollection));
// import this from the /admin route builder
$adminRoutes->import('admin.yml', '/imported');
$adminRoutes->mount('/imported', $adminRoutes->import('admin.yml'));

$collection = $routes->build();
$this->assertEquals('/admin/dashboard', $collection->get('admin_dashboard')->getPath(), 'Routes before mounting have the prefix');
Expand Down
0