11
11
12
12
namespace Symfony \Component \DependencyInjection \Loader \Configurator ;
13
13
14
+ use Symfony \Component \Config \Resource \FileResource ;
14
15
use Symfony \Component \DependencyInjection \Argument \IteratorArgument ;
15
16
use Symfony \Component \DependencyInjection \Argument \ServiceLocatorArgument ;
16
17
use Symfony \Component \DependencyInjection \Argument \TaggedIteratorArgument ;
20
21
use Symfony \Component \DependencyInjection \Extension \ExtensionInterface ;
21
22
use Symfony \Component \DependencyInjection \Loader \PhpFileLoader ;
22
23
use Symfony \Component \ExpressionLanguage \Expression ;
24
+ use Symfony \Component \Yaml \Yaml ;
23
25
24
26
/**
25
27
* @author Nicolas Grekas <p@tchwork.com>
@@ -42,6 +44,8 @@ public function __construct(ContainerBuilder $container, PhpFileLoader $loader,
42
44
$ this ->instanceof = &$ instanceof ;
43
45
$ this ->path = $ path ;
44
46
$ this ->file = $ file ;
47
+
48
+ __helper::__init ($ this );
45
49
}
46
50
47
51
final public function extension (string $ namespace , array $ config )
@@ -75,6 +79,49 @@ final public function services(): ServicesConfigurator
75
79
{
76
80
return new ServicesConfigurator ($ this ->container , $ this ->loader , $ this ->instanceof , $ this ->path , $ this ->anonymousCount );
77
81
}
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
+ }
78
125
}
79
126
80
127
/**
@@ -136,3 +183,8 @@ function expr(string $expression): Expression
136
183
{
137
184
return new Expression ($ expression );
138
185
}
186
+
187
+ function yamlFile (string $ filepath ): array
188
+ {
189
+ return __helper::call ('yamlFile ' , $ filepath );
190
+ }
0 commit comments