/** * {@inheritdoc} */ public function load($file, $type = null) { $path = $this->locator->locate($file); $xml = $this->loadFile($path); $collection = new I18nRouteCollection(); $collection->addResource(new FileResource($path)); // process routes and imports foreach ($xml->documentElement->childNodes as $node) { if (!$node instanceof \DOMElement) { continue; } $this->parseNode($collection, $node, $path, $file); } return $collection; }
public function testCollectionLocalizeRoutes() { $collection = new I18nRouteCollection(); $collection->add('test', new Route('/test')); $collection->addPrefix(array('en' => '/en/', 'nl' => '/nl/')); $this->assertCount(2, $collection, '(count)'); $enRoute = $collection->get('test.en'); $this->assertNotNull($enRoute, '(en.missing)'); $this->assertInstanceOf('\\Symfony\\Component\\Routing\\Route', $enRoute, '(en.instanceOf)'); $this->assertEquals('/en/test', $enRoute->getPath(), '(en.path)'); $this->assertEquals('en', $enRoute->getDefault('_locale'), '(en._locale)'); $nlRoute = $collection->get('test.nl'); $this->assertNotNull($nlRoute, '(nl.missing)'); $this->assertInstanceOf('\\Symfony\\Component\\Routing\\Route', $nlRoute, '(nl.instanceOf)'); $this->assertEquals('/nl/test', $nlRoute->getPath(), '(nl.path)'); $this->assertEquals('nl', $nlRoute->getDefault('_locale'), '(nl._locale)'); }
/** * {@inheritDoc} */ public function load($file, $type = null) { $path = $this->locator->locate($file); if (!stream_is_local($path)) { throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path)); } if (!file_exists($path)) { throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path)); } $config = Yaml::parse(file_get_contents($path)); $collection = new I18nRouteCollection(); $collection->addResource(new FileResource($path)); // empty file if (null === $config) { $config = array(); } // not an array if (!is_array($config)) { throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file)); } foreach ($config as $name => $config) { if (isset($config['pattern'])) { if (isset($config['path'])) { throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path)); } $config['path'] = $config['pattern']; unset($config['pattern']); } $this->validate($config, $name, $path); if (isset($config['resource'])) { $this->parseImport($collection, $config, $path, $file); } else { $this->parseRoute($collection, $name, $config, $path); } } return $collection; }