made some optimization when parsing YAML files

This commit is contained in:
Fabien Potencier 2013-05-05 11:27:54 +02:00
parent c8f95b5dab
commit 0586c7eb16
4 changed files with 55 additions and 8 deletions

View File

@ -18,7 +18,7 @@ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser as YamlParser;
/**
* YamlFileLoader loads YAML files service definitions.
@ -29,6 +29,8 @@ use Symfony\Component\Yaml\Yaml;
*/
class YamlFileLoader extends FileLoader
{
private $yamlParser;
/**
* Loads a Yaml file.
*
@ -239,7 +241,19 @@ class YamlFileLoader extends FileLoader
*/
protected function loadFile($file)
{
return $this->validate(Yaml::parse($file), $file);
if (!stream_is_local($file)) {
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
}
if (!file_exists($file)) {
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
}
/**

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Config\Loader\FileLoader;
/**
@ -30,6 +30,7 @@ class YamlFileLoader extends FileLoader
private static $availableKeys = array(
'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options',
);
private $yamlParser;
/**
* Loads a Yaml file.
@ -47,7 +48,19 @@ class YamlFileLoader extends FileLoader
{
$path = $this->locator->locate($file);
$config = Yaml::parse($path);
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));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
$config = $this->yamlParser->parse(file_get_contents($path));
$collection = new RouteCollection();
$collection->addResource(new FileResource($path));

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
use Symfony\Component\Translation\Exception\NotFoundResourceException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Exception\ParseException;
/**
@ -26,6 +26,8 @@ use Symfony\Component\Yaml\Exception\ParseException;
*/
class YamlFileLoader extends ArrayLoader implements LoaderInterface
{
private $yamlParser;
/**
* {@inheritdoc}
*
@ -41,8 +43,12 @@ class YamlFileLoader extends ArrayLoader implements LoaderInterface
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
try {
$messages = Yaml::parse($resource);
$messages = $this->yamlParser->parse(file_get_contents($resource));
} catch (ParseException $e) {
throw new InvalidResourceException('Error parsing YAML.', 0, $e);
}

View File

@ -12,10 +12,12 @@
namespace Symfony\Component\Validator\Mapping\Loader;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Parser as YamlParser;
class YamlFileLoader extends FileLoader
{
private $yamlParser;
/**
* An array of YAML class descriptions
*
@ -29,7 +31,19 @@ class YamlFileLoader extends FileLoader
public function loadClassMetadata(ClassMetadata $metadata)
{
if (null === $this->classes) {
$this->classes = Yaml::parse($this->file);
if (!stream_is_local($this->file)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $this->file));
}
if (!file_exists($this->file)) {
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $this->file));
}
if (null === $this->yamlParser) {
$this->yamlParser = new YamlParser();
}
$this->classes = $this->yamlParser->parse(file_get_contents($this->file));
// empty file
if (null === $this->classes) {