33
44namespace TheCodingMachine \ClassExplorer \Glob ;
55
6+ use DirectoryIterator ;
7+ use GlobIterator ;
68use Mouf \Composer \ClassNameMapper ;
79use Psr \SimpleCache \CacheInterface ;
810use RecursiveDirectoryIterator ;
911use RecursiveIteratorIterator ;
1012use RegexIterator ;
1113use TheCodingMachine \ClassExplorer \ClassExplorerInterface ;
14+ use function var_dump ;
1215
1316/**
1417 * Returns a set of classes by analyzing the PHP files in a directory.
@@ -40,13 +43,18 @@ class GlobClassExplorer implements ClassExplorerInterface
4043 * @var ClassNameMapper|null
4144 */
4245 private $ classNameMapper ;
46+ /**
47+ * @var bool
48+ */
49+ private $ recursive ;
4350
44- public function __construct (string $ namespace , CacheInterface $ cache , ?int $ cacheTtl = null , ?ClassNameMapper $ classNameMapper = null )
51+ public function __construct (string $ namespace , CacheInterface $ cache , ?int $ cacheTtl = null , ?ClassNameMapper $ classNameMapper = null , bool $ recursive = true )
4552 {
4653 $ this ->namespace = $ namespace ;
4754 $ this ->cache = $ cache ;
4855 $ this ->cacheTtl = $ cacheTtl ;
4956 $ this ->classNameMapper = $ classNameMapper ;
57+ $ this ->recursive = $ recursive ;
5058 }
5159
5260 /**
@@ -82,7 +90,7 @@ private function doGetClasses(): array
8290
8391 $ classes = [];
8492 foreach ($ dirs as $ dir ) {
85- $ filesForDir = \iterator_to_array (self :: getPhpFilesForDir ($ dir ));
93+ $ filesForDir = \iterator_to_array ($ this -> getPhpFilesForDir ($ dir ));
8694 $ dirLen = \strlen ($ dir )+1 ;
8795 foreach ($ filesForDir as $ file ) {
8896 // Trim the root directory name and the PHP extension
@@ -97,12 +105,16 @@ private function doGetClasses(): array
97105 * @param string $directory
98106 * @return \Iterator
99107 */
100- private static function getPhpFilesForDir (string $ directory ): \Iterator
108+ private function getPhpFilesForDir (string $ directory ): \Iterator
101109 {
102110 if (!\is_dir ($ directory )) {
103111 return new \EmptyIterator ();
104112 }
105- $ allFiles = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ directory , RecursiveDirectoryIterator::SKIP_DOTS ));
106- return new RegexIterator ($ allFiles , '/\.php$/i ' /*, \RecursiveRegexIterator::GET_MATCH*/ );
113+ if ($ this ->recursive ) {
114+ $ allFiles = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($ directory , RecursiveDirectoryIterator::SKIP_DOTS ));
115+ return new RegexIterator ($ allFiles , '/\.php$/i ' /*, \RecursiveRegexIterator::GET_MATCH*/ );
116+ } else {
117+ return new GlobIterator ($ directory .'/*.php ' );
118+ }
107119 }
108120}
0 commit comments