Skip to content

Commit 59867f9

Browse files
committed
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
2 parents 6604ff8 + 0586c7e commit 59867f9

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\DependencyInjection\Reference;
1919
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2020
use Symfony\Component\Config\Resource\FileResource;
21-
use Symfony\Component\Yaml\Yaml;
21+
use Symfony\Component\Yaml\Parser as YamlParser;
2222

2323
/**
2424
* YamlFileLoader loads YAML files service definitions.
@@ -29,6 +29,8 @@
2929
*/
3030
class YamlFileLoader extends FileLoader
3131
{
32+
private $yamlParser;
33+
3234
/**
3335
* Loads a Yaml file.
3436
*
@@ -243,7 +245,19 @@ private function parseDefinition($id, $service, $file)
243245
*/
244246
protected function loadFile($file)
245247
{
246-
return $this->validate(Yaml::parse($file), $file);
248+
if (!stream_is_local($file)) {
249+
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
250+
}
251+
252+
if (!file_exists($file)) {
253+
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
254+
}
255+
256+
if (null === $this->yamlParser) {
257+
$this->yamlParser = new YamlParser();
258+
}
259+
260+
return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
247261
}
248262

249263
/**

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Routing\RouteCollection;
1515
use Symfony\Component\Routing\Route;
1616
use Symfony\Component\Config\Resource\FileResource;
17-
use Symfony\Component\Yaml\Yaml;
17+
use Symfony\Component\Yaml\Parser as YamlParser;
1818
use Symfony\Component\Config\Loader\FileLoader;
1919

2020
/**
@@ -30,6 +30,7 @@ class YamlFileLoader extends FileLoader
3030
private static $availableKeys = array(
3131
'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options',
3232
);
33+
private $yamlParser;
3334

3435
/**
3536
* Loads a Yaml file.
@@ -47,7 +48,19 @@ public function load($file, $type = null)
4748
{
4849
$path = $this->locator->locate($file);
4950

50-
$config = Yaml::parse($path);
51+
if (!stream_is_local($path)) {
52+
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
53+
}
54+
55+
if (!file_exists($path)) {
56+
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
57+
}
58+
59+
if (null === $this->yamlParser) {
60+
$this->yamlParser = new YamlParser();
61+
}
62+
63+
$config = $this->yamlParser->parse(file_get_contents($path));
5164

5265
$collection = new RouteCollection();
5366
$collection->addResource(new FileResource($path));

src/Symfony/Component/Translation/Loader/YamlFileLoader.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Translation\Exception\InvalidResourceException;
1515
use Symfony\Component\Translation\Exception\NotFoundResourceException;
1616
use Symfony\Component\Config\Resource\FileResource;
17-
use Symfony\Component\Yaml\Yaml;
17+
use Symfony\Component\Yaml\Parser as YamlParser;
1818
use Symfony\Component\Yaml\Exception\ParseException;
1919

2020
/**
@@ -26,6 +26,8 @@
2626
*/
2727
class YamlFileLoader extends ArrayLoader implements LoaderInterface
2828
{
29+
private $yamlParser;
30+
2931
/**
3032
* {@inheritdoc}
3133
*
@@ -41,8 +43,12 @@ public function load($resource, $locale, $domain = 'messages')
4143
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
4244
}
4345

46+
if (null === $this->yamlParser) {
47+
$this->yamlParser = new YamlParser();
48+
}
49+
4450
try {
45-
$messages = Yaml::parse($resource);
51+
$messages = $this->yamlParser->parse(file_get_contents($resource));
4652
} catch (ParseException $e) {
4753
throw new InvalidResourceException('Error parsing YAML.', 0, $e);
4854
}

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Component\Validator\Mapping\Loader;
1313

1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
15-
use Symfony\Component\Yaml\Yaml;
15+
use Symfony\Component\Yaml\Parser as YamlParser;
1616

1717
class YamlFileLoader extends FileLoader
1818
{
19+
private $yamlParser;
20+
1921
/**
2022
* An array of YAML class descriptions
2123
*
@@ -29,7 +31,19 @@ class YamlFileLoader extends FileLoader
2931
public function loadClassMetadata(ClassMetadata $metadata)
3032
{
3133
if (null === $this->classes) {
32-
$this->classes = Yaml::parse($this->file);
34+
if (!stream_is_local($this->file)) {
35+
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $this->file));
36+
}
37+
38+
if (!file_exists($this->file)) {
39+
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $this->file));
40+
}
41+
42+
if (null === $this->yamlParser) {
43+
$this->yamlParser = new YamlParser();
44+
}
45+
46+
$this->classes = $this->yamlParser->parse(file_get_contents($this->file));
3347

3448
// empty file
3549
if (null === $this->classes) {

0 commit comments

Comments
 (0)