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