14
14
use Symfony \Component \DependencyInjection \ContainerBuilder ;
15
15
use Symfony \Component \Config \Loader \FileLoader as BaseFileLoader ;
16
16
use Symfony \Component \Config \FileLocatorInterface ;
17
+ use Symfony \Component \Config \Exception \FileLoaderLoadException ;
18
+ use Symfony \Component \Config \Resource \DirectoryResource ;
19
+ use Symfony \Component \Config \Resource \FileResource ;
17
20
18
21
/**
19
22
* FileLoader is the abstract class used by all built-in loaders that are file based.
@@ -24,6 +27,17 @@ abstract class FileLoader extends BaseFileLoader
24
27
{
25
28
protected $ container ;
26
29
30
+ private $ currentDir ;
31
+
32
+ /**
33
+ * {@inheritdoc}
34
+ */
35
+ public function setCurrentDir ($ dir )
36
+ {
37
+ $ this ->currentDir = $ dir ;
38
+ parent ::setCurrentDir ($ dir );
39
+ }
40
+
27
41
/**
28
42
* @param ContainerBuilder $container A ContainerBuilder instance
29
43
* @param FileLocatorInterface $locator A FileLocator instance
@@ -34,4 +48,47 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
34
48
35
49
parent ::__construct ($ locator );
36
50
}
51
+
52
+ /**
53
+ * {@inheritdoc}
54
+ */
55
+ public function import ($ resource , $ type = null , $ ignoreErrors = false , $ sourceResource = null )
56
+ {
57
+ if (strlen ($ resource ) === $ i = strcspn ($ resource , '*?{[ ' )) {
58
+ $ directoryPrefix = $ resource ;
59
+ $ directoryGlob = '' ;
60
+ } else {
61
+ $ directoryPrefix = dirname (substr ($ resource , 0 , 1 + $ i ));
62
+ $ directoryGlob = substr ($ resource , strlen ($ directoryPrefix ));
63
+ }
64
+
65
+ try {
66
+ $ directoryPrefix = $ this ->locator ->locate ($ directoryPrefix , $ this ->currentDir , true );
67
+ $ directoryPrefix = realpath ($ directoryPrefix ) ?: $ directoryPrefix ;
68
+ $ directoryGlob = $ directoryPrefix .$ directoryGlob ;
69
+
70
+ if (!$ files = glob ($ directoryGlob , defined ('GLOB_BRACE ' ) ? GLOB_BRACE : 0 )) {
71
+ throw new FileLoaderLoadException ($ directoryGlob , $ sourceResource );
72
+ }
73
+
74
+ foreach ($ files as $ file ) {
75
+ if (is_dir ($ file )) {
76
+ $ this ->container ->addResource (new DirectoryResource ($ file , '/^$/ ' ));
77
+ parent ::import ($ file , 'directory ' , $ ignoreErrors , $ sourceResource );
78
+ } else {
79
+ $ this ->container ->addResource (new FileResource ($ file ));
80
+ parent ::import ($ file , $ type , $ ignoreErrors , $ sourceResource );
81
+ }
82
+ }
83
+ } catch (\Exception $ e ) {
84
+ if (!$ ignoreErrors ) {
85
+ // prevent embedded imports from nesting multiple exceptions
86
+ if ($ e instanceof FileLoaderLoadException) {
87
+ throw $ e ;
88
+ }
89
+
90
+ throw new FileLoaderLoadException ($ resource , $ sourceResource );
91
+ }
92
+ }
93
+ }
37
94
}
0 commit comments