/** * Adds a route collection to the current set of routes (at the end of the current set). * * @param RouteCollection $collection A RouteCollection instance * @param string $prefix An optional prefix to add before each pattern of the route collection */ public function addCollection(RouteCollection $collection, $prefix = '') { $collection->addPrefix($prefix); foreach ($collection->getResources() as $resource) { $this->addResource($resource); } $this->routes = array_merge($this->routes, $collection->getRoutes()); }
public function testNormalizeUrl() { $collection = new RouteCollection(); $collection->addRoute('foo', new Route('/:foo')); $matcher = new UrlMatcherForTests($collection, array(), array()); $this->assertEquals('/', $matcher->normalizeUrl(''), '->normalizeUrl() adds a / at the beginning of the URL if needed'); $this->assertEquals('/foo', $matcher->normalizeUrl('foo'), '->normalizeUrl() adds a / at the beginning of the URL if needed'); $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string'); $this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /'); }
/** * @covers Symfony\Components\Routing\Loader\ClosureLoader::load */ public function testLoad() { $loader = new ClosureLoader(); $route = new Route('/'); $routes = $loader->load(function () use($route) { $routes = new RouteCollection(); $routes->addRoute('foo', $route); return $routes; }); $this->assertEquals($route, $routes->getRoute('foo'), '->load() loads a \\Closure resource'); }
/** * @throws \InvalidArgumentException When config pattern is not defined for the given route */ protected function parseRoute(RouteCollection $collection, $name, $config, $file) { $defaults = isset($config['defaults']) ? $config['defaults'] : array(); $requirements = isset($config['requirements']) ? $config['requirements'] : array(); $options = isset($config['options']) ? $config['options'] : array(); if (!isset($config['pattern'])) { throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name)); } $route = new Route($config['pattern'], $defaults, $requirements, $options); $collection->addRoute($name, $route); }
protected function parseRoute(RouteCollection $collection, $definition, $file) { $defaults = array(); $requirements = array(); $options = array(); foreach ($definition->childNodes as $node) { if (!$node instanceof \DOMElement) { continue; } switch ($node->tagName) { case 'default': $defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; case 'option': $options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; case 'requirement': $requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue); break; default: throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName)); } } $route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options); $collection->addRoute((string) $definition->getAttribute('id'), $route); }
/** * @covers Symfony\Components\Routing\Loader\DelegatingLoader::load */ public function testLoad() { $resolver = new LoaderResolver(array(new ClosureLoader())); $loader = new DelegatingLoader($resolver); $route = new Route('/'); $routes = $loader->load(function () use($route) { $routes = new RouteCollection(); $routes->addRoute('foo', $route); return $routes; }); $this->assertSame($route, $routes->getRoute('foo'), '->load() loads a resource using the loaders from the resolver'); try { $loader->load('foo.foo'); $this->fail('->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } catch (\Exception $e) { $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an \\InvalidArgumentException if the resource cannot be loaded'); } }
protected function parseImport(RouteCollection $collection, $name, $import, $file) { if (!isset($import['resource'])) { throw new \InvalidArgumentException(sprintf('You must define a "resource" when importing (%s).', $name)); } $class = null; if (isset($import['class']) && $import['class'] !== get_class($this)) { $class = $import['class']; } else { // try to detect loader with the extension switch (pathinfo($import['resource'], PATHINFO_EXTENSION)) { case 'xml': $class = 'Symfony\\Components\\Routing\\Loader\\XmlFileLoader'; break; } } $loader = null === $class ? $this : new $class($this->paths); $importedFile = $this->getAbsolutePath($import['resource'], dirname($file)); $collection->addCollection($loader->load($importedFile), isset($import['prefix']) ? $import['prefix'] : null); }
protected function parseImport(RouteCollection $collection, $node, $file) { $class = null; if ($node->hasAttribute('class') && $import->getAttribute('class') !== get_class($this)) { $class = (string) $node->getAttribute('class'); } else { // try to detect loader with the extension switch (pathinfo((string) $node->getAttribute('resource'), PATHINFO_EXTENSION)) { case 'yml': $class = 'Symfony\\Components\\Routing\\Loader\\YamlFileLoader'; break; } } $loader = null === $class ? $this : new $class($this->paths); $importedFile = $this->getAbsolutePath((string) $node->getAttribute('resource'), dirname($file)); $collection->addCollection($loader->load($importedFile), (string) $node->getAttribute('prefix')); }
<?php /* * This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once __DIR__ . '/../../../../bootstrap.php'; use Symfony\Components\Routing\Matcher\UrlMatcher; use Symfony\Components\Routing\RouteCollection; use Symfony\Components\Routing\Route; $t = new LimeTest(1); $collection = new RouteCollection(); $collection->addRoute('foo', new Route('/:foo')); class UrlMatcherTest extends UrlMatcher { public function normalizeUrl($url) { return parent::normalizeUrl($url); } } $matcher = new UrlMatcherTest($collection, array(), array()); // ->normalizeUrl() $t->diag('->normalizeUrl()'); $t->is($matcher->normalizeUrl(''), '/', '->normalizeUrl() adds a / at the beginning of the URL if needed'); $t->is($matcher->normalizeUrl('foo'), '/foo', '->normalizeUrl() adds a / at the beginning of the URL if needed'); $t->is($matcher->normalizeUrl('/foo?foo=bar'), '/foo', '->normalizeUrl() removes the query string'); $t->is($matcher->normalizeUrl('/foo//bar'), '/foo/bar', '->normalizeUrl() removes duplicated /'); // ->match()
<?php use Symfony\Components\Routing\RouteCollection; use Symfony\Components\Routing\Route; $collection = new RouteCollection(); $collection->addRoute('homepage', new Route('/', array('_controller' => 'FrameworkBundle:Default:index'))); return $collection;
public function testResource() { $collection = new RouteCollection(); $collection->addResource($foo = new FileResource(__DIR__ . '/Fixtures/foo.xml')); $this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource'); }
$collection->addRoute('foo', $foo = new Route('/foo')); $collection1 = new RouteCollection(); $collection1->addRoute('foo', $foo1 = new Route('/foo1')); $collection1->addRoute('bar', $bar1 = new Route('/bar1')); $collection->addCollection($collection1); $t->is($collection->getRoutes(), array('foo' => $foo1, 'bar' => $bar1), '->addCollection() adds routes from another collection'); $collection = new RouteCollection(); $collection->addRoute('foo', $foo = new Route('/foo')); $collection1 = new RouteCollection(); $collection1->addRoute('foo', $foo1 = new Route('/foo1')); $collection->addCollection($collection1, '/foo'); $t->is($collection->getRoute('foo')->getPattern(), '/foo/foo1', '->addCollection() can add a prefix to all merged routes'); $collection = new RouteCollection(); $collection->addResource($foo = new FileResource('foo')); $collection1 = new RouteCollection(); $collection1->addResource($foo1 = new FileResource('foo1')); $collection->addCollection($collection1); $t->is($collection->getResources(), array($foo, $foo1), '->addCollection() merges resources'); // ->addPrefix() $t->diag('->addPrefix()'); $collection = new RouteCollection(); $collection->addRoute('foo', $foo = new Route('/foo')); $collection->addRoute('bar', $bar = new Route('/bar')); $collection->addPrefix('/admin'); $t->is($collection->getRoute('foo')->getPattern(), '/admin/foo', '->addPrefix() adds a prefix to all routes'); $t->is($collection->getRoute('bar')->getPattern(), '/admin/bar', '->addPrefix() adds a prefix to all routes'); // ->getResources() ->addResource() $t->diag('->getResources() ->addResource()'); $collection = new RouteCollection(); $collection->addResource($foo = new FileResource('foo')); $t->is($collection->getResources(), array($foo), '->addResources() adds a resource');