Skip to content

Commit 4ce2007

Browse files
committed
[DI] handle yaml file with configurator, ref symfony#30446
1 parent 4574f85 commit 4ce2007

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Config\Resource\FileResource;
1415
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1516
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1617
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -20,6 +21,7 @@
2021
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2122
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2223
use Symfony\Component\ExpressionLanguage\Expression;
24+
use Symfony\Component\Yaml\Yaml;
2325

2426
/**
2527
* @author Nicolas Grekas <p@tchwork.com>
@@ -42,6 +44,8 @@ public function __construct(ContainerBuilder $container, PhpFileLoader $loader,
4244
$this->instanceof = &$instanceof;
4345
$this->path = $path;
4446
$this->file = $file;
47+
48+
__helper::__init($this);
4549
}
4650

4751
final public function extension(string $namespace, array $config)
@@ -75,6 +79,49 @@ final public function services(): ServicesConfigurator
7579
{
7680
return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
7781
}
82+
83+
protected function yamlFile(string $filepath): array
84+
{
85+
if (!\class_exists(Yaml::class)) {
86+
throw new \InvalidArgumentException('You need to install the YAML component to parse YAML files.');
87+
}
88+
89+
if (!\is_file($filepath = static::processValue($filepath))) {
90+
$rootDir = \dirname($this->file);
91+
92+
if (!\is_file($filepath = "$rootDir/$filepath")) {
93+
throw new InvalidArgumentException("Unable to locate file \"{$filepath}\". Please provide a path relative to \"$rootDir\" or an absolute path.");
94+
}
95+
}
96+
97+
$this->container->addResource(new FileResource($filepath));
98+
99+
return static::processValue(Yaml::parseFile($filepath, Yaml::PARSE_CONSTANT));
100+
}
101+
}
102+
103+
/**
104+
* A class to call protected method from the configurator outside the class definition.
105+
*
106+
* @internal
107+
*/
108+
class __helper extends ContainerConfigurator
109+
{
110+
private static $configurator;
111+
112+
public static function __init(ContainerConfigurator $configurator)
113+
{
114+
self::$configurator = $configurator;
115+
}
116+
117+
public static function call(string $method, ...$args)
118+
{
119+
if (0 !== strpos(debug_backtrace(2, 1)[0]['class'], __NAMESPACE__)) {
120+
throw new \BadFunctionCallException(sprintf('The "%s" class is internal.', __CLASS__));
121+
}
122+
123+
return call_user_func([self::$configurator, $method], ...$args);
124+
}
78125
}
79126

80127
/**
@@ -136,3 +183,8 @@ function expr(string $expression): Expression
136183
{
137184
return new Expression($expression);
138185
}
186+
187+
function yamlFile(string $filepath): array
188+
{
189+
return __helper::call('yamlFile', $filepath);
190+
}

0 commit comments

Comments
 (0)