Skip to content

Commit

Permalink
merged branch fabpot/yaml-optim (PR #7935)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed May 6, 2013
2 parents 6604ff8 + 0586c7e commit 59867f9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
Expand Up @@ -18,7 +18,7 @@
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.
Expand All @@ -29,6 +29,8 @@
*/
class YamlFileLoader extends FileLoader
{
private $yamlParser;

/**
* Loads a Yaml file.
*
Expand Down Expand Up @@ -243,7 +245,19 @@ private function parseDefinition($id, $service, $file)
*/
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);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -14,7 +14,7 @@
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;

/**
Expand All @@ -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.
Expand All @@ -47,7 +48,19 @@ public function load($file, $type = null)
{
$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));
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Translation/Loader/YamlFileLoader.php
Expand Up @@ -14,7 +14,7 @@
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;

/**
Expand All @@ -26,6 +26,8 @@
*/
class YamlFileLoader extends ArrayLoader implements LoaderInterface
{
private $yamlParser;

/**
* {@inheritdoc}
*
Expand All @@ -41,8 +43,12 @@ public function load($resource, $locale, $domain = 'messages')
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);
}
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php
Expand Up @@ -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
*
Expand All @@ -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) {
Expand Down

0 comments on commit 59867f9

Please sign in to comment.