Skip to content

Commit 5d5ffdf

Browse files
authored
Merge pull request #5 from moufmouf/non_recursive_mode
Adding non-recursive loading option
2 parents d91a0e5 + 6969828 commit 5d5ffdf

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ script:
1717
- composer cs-check
1818
- composer phpstan
1919
after_script:
20-
- php vendor/bin/coveralls -v
20+
- php vendor/bin/php-coveralls -v

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ This explorer:
3333
- assumes that if a file exists in a PSR-0 or PSR-4 directory, the class is available (assumes the file respects PSR-1)
3434
- makes no attempt at autoloading the class
3535
- is pretty fast, even when no cache is involved
36+
37+
By default, `GlobClassExplorer` will load classes recursively in sub-namespaces. You can prevent it to load classes
38+
recursively by passing `false` to the 5th parameter:
39+
40+
```php
41+
$explorer = new GlobClassExplorer('\\This\\Namespace\\Only\\', $psr16Cache, $cacheTtl, null, false);
42+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"extra": {
4040
"class": "TheCodingMachine\\ClassExplorer\\ClassExplorerPlugin",
4141
"branch-alias": {
42-
"dev-master": "1.0.x-dev"
42+
"dev-master": "1.1.x-dev"
4343
}
4444
},
4545
"scripts": {

src/Glob/GlobClassExplorer.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
namespace TheCodingMachine\ClassExplorer\Glob;
55

6+
use DirectoryIterator;
7+
use GlobIterator;
68
use Mouf\Composer\ClassNameMapper;
79
use Psr\SimpleCache\CacheInterface;
810
use RecursiveDirectoryIterator;
911
use RecursiveIteratorIterator;
1012
use RegexIterator;
1113
use 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
}

tests/Glob/GlobClassExplorerTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
use Mouf\Composer\ClassNameMapper;
66
use PHPUnit\Framework\TestCase;
77
use Symfony\Component\Cache\Simple\NullCache;
8+
use TheCodingMachine\ClassExplorer\ClassExplorerInterface;
89

910
class GlobClassExplorerTest extends TestCase
1011
{
1112
public function testGetClasses()
1213
{
13-
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new NullCache());
14+
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache());
1415
$classes = $explorer->getClasses();
1516

16-
$this->assertSame([GlobClassExplorer::class], $classes);
17+
$this->assertSame([GlobClassExplorer::class, ClassExplorerInterface::class], $classes);
18+
}
19+
20+
public function testGetClassesNonRecursive()
21+
{
22+
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, false);
23+
$classes = $explorer->getClasses();
24+
25+
$this->assertSame([ClassExplorerInterface::class], $classes);
1726
}
1827

1928
public function testGetDevClasses()

0 commit comments

Comments
 (0)