10000 [Routing] Add PHP fluent DSL for configuring routes by nicolas-grekas · Pull Request #24180 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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;
Copy link
Member

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 ?

Copy link
Member Author

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:

$collection->defaults(['foo' => 'bar'])->add(...);

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.


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;
}
}
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;
}
}
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;
}
}
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);
}
}
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);
}
}
Loading
0