|
| 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 | +} |
0 commit comments