Example #1
0
 /**
  * Loads a Yaml file.
  *
  * @param  string $file A Yaml file path
  *
  * @return RouteCollection A RouteCollection instance
  *
  * @throws \InvalidArgumentException When route can't be parsed
  */
 public function load($file)
 {
     $path = $this->findFile($file);
     $config = $this->loadFile($path);
     $collection = new RouteCollection();
     $collection->addResource(new FileResource($path));
     foreach ($config as $name => $config) {
         if (isset($config['resource'])) {
             $prefix = isset($config['prefix']) ? $config['prefix'] : null;
             $this->currentDir = dirname($path);
             $collection->addCollection($this->import($config['resource']), $prefix);
         } elseif (isset($config['pattern'])) {
             $this->parseRoute($collection, $name, $config, $path);
         } else {
             throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name));
         }
     }
     return $collection;
 }
 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);
 }
 public function testAddCollection()
 {
     $collection = new RouteCollection();
     $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);
     $this->assertEquals(array('foo' => $foo1, 'bar' => $bar1), $collection->getRoutes(), '->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');
     $this->assertEquals('/foo/foo1', $collection->getRoute('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
     $collection = new RouteCollection();
     $collection->addResource($foo = new FileResource(__DIR__ . '/Fixtures/foo.xml'));
     $collection1 = new RouteCollection();
     $collection1->addResource($foo1 = new FileResource(__DIR__ . '/Fixtures/foo1.xml'));
     $collection->addCollection($collection1);
     $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
 }
Example #4
0
 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'));
 }
$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');