Skip to content

Commit

Permalink
[Routing] Add PHP fluent DSL for configuring routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 20, 2017
1 parent a7dbe78 commit be3da3c
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 13 deletions.
79 changes: 79 additions & 0 deletions Loader/Configurator/CollectionConfigurator.php
@@ -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 Loader/Configurator/ImportConfigurator.php
@@ -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 Loader/Configurator/RouteConfigurator.php
@@ -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 Loader/Configurator/RoutingConfigurator.php
@@ -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 Loader/Configurator/Traits/AddTrait.php
@@ -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);
}
}

0 comments on commit be3da3c

Please sign in to comment.