merged branch fabpot/yaml-optim (PR #7935)
This PR was merged into the master branch.
Discussion
----------
made some optimization when parsing YAML files
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | n/a
| License | MIT
| Doc PR | n/a
This change makes a small speed optimization when loading a YAML file, but more important, it allows to use local stream wrappers instead of regular files on the filesystem.
Commits
-------
0586c7e
made some optimization when parsing YAML files
This commit is contained in:
commit
59867f921e
@ -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.
|
||||
*
|
||||
@ -243,7 +245,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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user