Skip to content

Commit cf5d247

Browse files
committed
minor #14231 [DependencyInjection] Show better error when the Yaml component is not installed (dosten)
This PR was squashed before being merged into the 2.3 branch (closes #14231). Discussion ---------- [DependencyInjection] Show better error when the Yaml component is not installed | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT As the Yaml component is a suggested dependency, if someone try to use the `YamlFileLoader` or the `YamlDumper` without the component will get an ugly message: ``` PHP Fatal error: Class 'Symfony\Component\Yaml\Dumper' not found in xxxx ``` With this PR the error will be an exception with the message: `Unable to dump the container as the Symfony Yaml Component is not installed.` for `YamlDumper` and `Unable to load YAML files service definitions as the Symfony Yaml Component is not installed.'` for `YamlFileLoader` Commits ------- 870a299 [DependencyInjection] Show better error when the Yaml component is not installed
2 parents 9a9ba27 + bad9deb commit cf5d247

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

Dumper/YamlDumper.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\DependencyInjection\Parameter;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21-
use Symfony\Component\DependencyInjection\ContainerBuilder;
2221

2322
/**
2423
* YamlDumper dumps a service container as a YAML string.
@@ -31,20 +30,6 @@ class YamlDumper extends Dumper
3130
{
3231
private $dumper;
3332

34-
/**
35-
* Constructor.
36-
*
37-
* @param ContainerBuilder $container The service container to dump
38-
*
39-
* @api
40-
*/
41-
public function __construct(ContainerBuilder $container)
42-
{
43-
parent::__construct($container);
44-
45-
$this->dumper = new YmlDumper();
46-
}
47-
4833
/**
4934
* Dumps the service container as an YAML string.
5035
*
@@ -56,6 +41,14 @@ public function __construct(ContainerBuilder $container)
5641
*/
5742
public function dump(array $options = array())
5843
{
44+
if (!class_exists('Symfony\Component\Yaml\Dumper')) {
45+
throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.');
46+
}
47+
48+
if (null === $this->dumper) {
49+
$this->dumper = new YmlDumper();
50+
}
51+
5952
return $this->addParameters()."\n".$this->addServices();
6053
}
6154

Loader/YamlFileLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Definition;
1818
use Symfony\Component\DependencyInjection\Reference;
1919
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2021
use Symfony\Component\Config\Resource\FileResource;
2122
use Symfony\Component\Yaml\Parser as YamlParser;
2223

@@ -269,6 +270,10 @@ private function parseDefinition($id, $service, $file)
269270
*/
270271
protected function loadFile($file)
271272
{
273+
if (!class_exists('Symfony\Component\Yaml\Parser')) {
274+
throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
275+
}
276+
272277
if (!stream_is_local($file)) {
273278
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
274279
}

0 commit comments

Comments
 (0)