-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Add PHP fluent DSL for configuring routes #24180
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class CollectionConfigurator | ||
{ | ||
use Traits\AddTrait; | ||
use Traits\RouteTrait; | ||
|
||
private $parent; | ||
|
||
public function __construct(RouteCollection $parent, $name) | ||
{ | ||
$this->parent = $parent; | ||
$this->name = $name; | ||
$this->collection = new RouteCollection(); | ||
$this->route = new Route(''); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->collection->addPrefix(rtrim($this->route->getPath(), '/')); | ||
$this->parent->addCollection($this->collection); | ||
} | ||
|
||
/** | ||
* Adds a route. | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* | ||
* @return RouteConfigurator | ||
*/ | ||
final public function add($name, $path) | ||
{ | ||
$this->collection->add($this->name.$name, $route = clone $this->route); | ||
|
||
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name); | ||
} | ||
|
||
/** | ||
* Creates a sub-collection. | ||
* | ||
* @return self | ||
*/ | ||
final public function collection($name = '') | ||
{ | ||
return new self($this->collection, $this->name.$name); | ||
} | ||
|
||
/** | ||
* Sets the prefix to add to the path of all child routes. | ||
* | ||
* @param string $prefix | ||
* | ||
* @return $this | ||
*/ | ||
final public function prefix($prefix) | ||
{ | ||
$this->route->setPath($prefix); | ||
|
||
return $this; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class ImportConfigurator | ||
{ | ||
use Traits\RouteTrait; | ||
|
||
private $parent; | ||
|
||
public function __construct(RouteCollection $parent, RouteCollection $route) | ||
{ | ||
$this->parent = $parent; | ||
$this->route = $route; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->parent->addCollection($this->route); | ||
} | ||
|
||
/** | ||
* Sets the prefix to add to the path of all child routes. | ||
* | ||
* @param string $prefix | ||
* | ||
* @return $this | ||
*/ | ||
final public function prefix($prefix) | ||
{ | ||
$this->route->addPrefix($prefix); | ||
|
||
return $this; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Routing/Loader/Configurator/RouteConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class RouteConfigurator | ||
{ | ||
use Traits\AddTrait; | ||
use Traits\RouteTrait; | ||
|
||
public function __construct(RouteCollection $collection, Route $route, $name = '') | ||
{ | ||
$this->collection = $collection; | ||
$this->route = $route; | ||
$this->name = $name; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
use Symfony\Component\Routing\Loader\PhpFileLoader; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class RoutingConfigurator | ||
{ | ||
use Traits\AddTrait; | ||
|
||
private $loader; | ||
private $path; | ||
private $file; | ||
|
||
public function __construct(RouteCollection $collection, PhpFileLoader $loader, $path, $file) | ||
{ | ||
$this->collection = $collection; | ||
$this->loader = $loader; | ||
$this->path = $path; | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* @return ImportConfigurator | ||
*/ | ||
final public function import($resource, $type = null, $ignoreErrors = false) | ||
{ | ||
$this->loader->setCurrentDir(dirname($this->path)); | ||
$subCollection = $this->loader->import($resource, $type, $ignoreErrors, $this->file); | ||
|
||
return new ImportConfigurator($this->collection, $subCollection); | ||
} | ||
|
||
/** | ||
* @return CollectionConfigurator | ||
*/ | ||
final public function collection($name = '') | ||
{ | ||
return new CollectionConfigurator($this->collection, $name); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Routing/Loader/Configurator/Traits/AddTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator\Traits; | ||
|
||
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
trait AddTrait | ||
{ | ||
/** | ||
* @var RouteCollection | ||
*/ | ||
private $collection; | ||
|
||
private $name = ''; | ||
|
||
/** | ||
* Adds a route. | ||
* | ||
* @param string $name | ||
* @param string $value | ||
* | ||
* @return RouteConfigurator | ||
*/ | ||
final public function add($name, $path) | ||
{ | ||
$this->collection->add($this->name.$name, $route = new Route($path)); | ||
|
||
return new RouteConfigurator($this->collection, $route); | ||
} | ||
|
||
/** | ||
* Adds a route. | ||
* | ||
* @param string $name | ||
* @param string $path | ||
* | ||
* @return RouteConfigurator | ||
*/ | ||
final public function __invoke($name, $path) | ||
{ | ||
return $this->add($name, $path); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't
->defaults
,->requirements
and so on apply on$this->collection
in this class ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the case, but not on the collection yes, because it works reverse: calling on the collection means changing routes declared before. Here, we want first to configure the route prototype (
$this->route
in this class), then use this prototype as a template for new routes added.The goal is to make this work as expected:
but I'm not sure doing
$collection->add(...);
then$collection->defaults(['foo' => 'bar']);
and having it applied to previously declared routes in the collection is something we want to have.