8000 [Routing] Add PHP fluent DSL for configuring routes · symfony/symfony@0b4198c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0b4198c

Browse files
[Routing] Add PHP fluent DSL for configuring routes
1 parent b1b6860 commit 0b4198c

File tree

10 files changed

+502
-13
lines changed

10 files changed

+502
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator;
13+
14+
use Symfony\Component\Routing\RouteCollection;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class ImportConfigurator
20+
{
21+
use Traits\RouteTrait;
22+
23+
private $parent;
24+
25+
public function __construct(RouteCollection $parent, RouteCollection $route)
26+
{
27+
$this->parent = $parent;
28+
$this->route = $route;
29+
}
30+
31+
public function __destruct()
32+
{
33+
$this->parent->addCollection($this->route);
34+
}
35+
36+
/**
37+
* Sets the prefix to add the path of all child routes.
38+
*
39+
* @param string $prefix
40+
*
41+
* @return $this
42+
*/
43+
final public function prefix($prefix)
44+
{
45+
$this->route->addPrefix($prefix);
46+
47+
return $this;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator;
13+
14+
use Symfony\Component\Routing\Route;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*/
20+
class RouteConfigurator
21+
{
22+
use Traits\AddTrait;
23+
use Traits\RouteTrait;
24+
25+
public function __construct(RouteCollection $collection, Route $route, $name = '')
26+
{
27+
$this->collection = $collection;
28+
$this->route = $route;
29+
$this->name = $name;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator;
13+
14+
use Symfony\Component\Routing\Loader\PhpFileLoader;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*/
20+
class RoutingConfigurator
21+
{
22+
use Traits\AddTrait;
23+
24+
private $loader;
25+
private $path;
26+
private $file;
27+
28+
public function __construct(RouteCollection $collection, PhpFileLoader $loader, $path, $file)
29+
{
30+
$this->collection = $collection;
31+
$this->loader = $loader;
32+
$this->path = $path;
33+
$this->file = $file;
34+
}
35+
36+
/**
37+
* @return ImportConfigurator
38+
*/
39+
final public function import($resource, $type = null, $ignoreErrors = false)
40+
{
41+
$this->loader->setCurrentDir(dirname($this->path));
42+
$subCollection = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
43+
44+
return new ImportConfigurator($this->collection, $subCollection);
45+
}
46+
47+
/**
48+
* @return SubCollectionConfigurator
49+
*/
50+
final public function collection($name = '')
51+
{
52+
return new SubCollectionConfigurator($this->collection, $name);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator;
13+
14+
use Symfony\Component\Routing\Route;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* @author Nicolas Grekas <p@tchwork.com>
19+
*/
20+
class SubCollectionConfigurator
21+
{
22+
use Traits\AddTrait;
23+
use Traits\RouteTrait;
24+
25+
private $parent;
26+
27+
public function __construct(RouteCollection $parent, $name)
28+
{
29+
$this->parent = $parent;
30+
$this->name = $name;
31+
$this->collection = new RouteCollection();
32+
$this->route = new Route('');
33+
}
34+
35+
public function __destruct()
36+
{
37+
$this->collection->addPrefix(rtrim($this->route->getPath(), '/'));
38+
$this->parent->addCollection($this->collection);
39+
}
40+
41+
/**
42+
* Adds a route.
43+
*
44+
* @param string $name
45+
* @param string $value
46+
*
47+
* @return RouteConfigurator
48+
*/
49+
final public function add($name, $path)
50+
{
51+
$this->collection->add($this->name.$name, $route = clone $this->route);
52+
53+
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name);
54+
}
55+
56+
/**
57+
* Creates a sub-collection.
58+
*
59+
* @return self
60+
*/
61+
final public function collection($name = '')
62+
{
63+
return new self($this->collection, $this->name.$name);
64+
}
65+
66+
/**
67+
* Sets the prefix to add the path of all child routes.
68+
*
69+
* @param string $prefix
70+
*
71+
* @return $this
72+
*/
73+
final public function prefix($prefix)
74+
{
75+
$this->route->setPath($prefix);
76+
77+
return $this;
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Loader\Configurator\Traits;
13+
14+
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
15+
use Symfony\Component\Routing\Route;
16+
use Symfony\Component\Routing\RouteCollection;
17+
18+
trait AddTrait
19+
{
20+
/**
21+
* @var RouteCollection
22+
*/
23+
private $collection;
24+
25+
private $name = '';
26+
27+
/**
28+
* Adds a route.
29+
*
30+
* @param string $name
31+
* @param string $value
32+
*
33+
* @return RouteConfigurator
34+
*/
35+
final public function add($name, $path)
36+
{
37+
$this->collection->add($this->name.$name, $route = new Route($path));
38+
39+
return new RouteConfigurator($this->collection, $route);
40+
}
41+
42+
/**
43+
* Adds a route.
44+
*
45+
* @param string $name
46+
* @param string $path
47+
*
48+
* @return RouteConfigurator
49+
*/
50+
final public function __invoke($name, $path)
51+
{
52+
return $this->add($name, $path);
53+
}
54+
}

0 commit comments

Comments
 (0)
0