Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Routing] Add PHP fluent DSL for configuring routes
- Loading branch information
1 parent
a7dbe78
commit be3da3c
Showing
10 changed files
with
498 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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; | ||
} | ||
} |
This file contains 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; | ||
} | ||
} |
This file contains 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; | ||
} | ||
} |
This file contains 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); | ||
} | ||
} |
This file contains 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.