This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Routing/Loader/YamlFileLoader.php

94 lines
2.9 KiB
PHP
Raw Normal View History

2010-02-17 13:53:31 +00:00
<?php
namespace Symfony\Component\Routing\Loader;
2010-02-17 13:53:31 +00:00
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
2010-02-17 13:53:31 +00:00
/*
* This file is part of the Symfony framework.
2010-02-17 13:53:31 +00:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* YamlFileLoader loads Yaml routing files.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class YamlFileLoader extends FileLoader
{
/**
* 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)
2010-02-17 13:53:31 +00:00
{
$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;
2010-02-17 13:53:31 +00:00
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
*
* @return Boolean true if this class supports the given resource, false otherwise
*/
public function supports($resource)
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
}
/**
* @throws \InvalidArgumentException When config pattern is not defined for the given route
*/
protected function parseRoute(RouteCollection $collection, $name, $config, $file)
2010-02-17 13:53:31 +00:00
{
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
$options = isset($config['options']) ? $config['options'] : array();
2010-02-17 13:53:31 +00:00
if (!isset($config['pattern'])) {
throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name));
}
2010-02-17 13:53:31 +00:00
$route = new Route($config['pattern'], $defaults, $requirements, $options);
2010-02-17 13:53:31 +00:00
$collection->addRoute($name, $route);
2010-02-17 13:53:31 +00:00
}
protected function loadFile($file)
2010-02-17 13:53:31 +00:00
{
return Yaml::load($file);
2010-02-17 13:53:31 +00:00
}
}