Skip to content

Commit

Permalink
Merge pull request #5 from moufmouf/non_recursive_mode
Browse files Browse the repository at this point in the history
Adding non-recursive loading option
  • Loading branch information
moufmouf committed Jan 16, 2019
2 parents d91a0e5 + 6969828 commit 5d5ffdf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -17,4 +17,4 @@ script:
- composer cs-check
- composer phpstan
after_script:
- php vendor/bin/coveralls -v
- php vendor/bin/php-coveralls -v
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -33,3 +33,10 @@ This explorer:
- assumes that if a file exists in a PSR-0 or PSR-4 directory, the class is available (assumes the file respects PSR-1)
- makes no attempt at autoloading the class
- is pretty fast, even when no cache is involved

By default, `GlobClassExplorer` will load classes recursively in sub-namespaces. You can prevent it to load classes
recursively by passing `false` to the 5th parameter:

```php
$explorer = new GlobClassExplorer('\\This\\Namespace\\Only\\', $psr16Cache, $cacheTtl, null, false);
```
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -39,7 +39,7 @@
"extra": {
"class": "TheCodingMachine\\ClassExplorer\\ClassExplorerPlugin",
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "1.1.x-dev"
}
},
"scripts": {
Expand Down
22 changes: 17 additions & 5 deletions src/Glob/GlobClassExplorer.php
Expand Up @@ -3,12 +3,15 @@

namespace TheCodingMachine\ClassExplorer\Glob;

use DirectoryIterator;
use GlobIterator;
use Mouf\Composer\ClassNameMapper;
use Psr\SimpleCache\CacheInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
use TheCodingMachine\ClassExplorer\ClassExplorerInterface;
use function var_dump;

/**
* Returns a set of classes by analyzing the PHP files in a directory.
Expand Down Expand Up @@ -40,13 +43,18 @@ class GlobClassExplorer implements ClassExplorerInterface
* @var ClassNameMapper|null
*/
private $classNameMapper;
/**
* @var bool
*/
private $recursive;

public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null)
public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true)
{
$this->namespace = $namespace;
$this->cache = $cache;
$this->cacheTtl = $cacheTtl;
$this->classNameMapper = $classNameMapper;
$this->recursive = $recursive;
}

/**
Expand Down Expand Up @@ -82,7 +90,7 @@ private function doGetClasses(): array

$classes = [];
foreach ($dirs as $dir) {
$filesForDir = \iterator_to_array(self::getPhpFilesForDir($dir));
$filesForDir = \iterator_to_array($this->getPhpFilesForDir($dir));
$dirLen = \strlen($dir)+1;
foreach ($filesForDir as $file) {
// Trim the root directory name and the PHP extension
Expand All @@ -97,12 +105,16 @@ private function doGetClasses(): array
* @param string $directory
* @return \Iterator
*/
private static function getPhpFilesForDir(string $directory): \Iterator
private function getPhpFilesForDir(string $directory): \Iterator
{
if (!\is_dir($directory)) {
return new \EmptyIterator();
}
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS));
return new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
if ($this->recursive) {
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS));
return new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
} else {
return new GlobIterator($directory.'/*.php');
}
}
}
13 changes: 11 additions & 2 deletions tests/Glob/GlobClassExplorerTest.php
Expand Up @@ -5,15 +5,24 @@
use Mouf\Composer\ClassNameMapper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Simple\NullCache;
use TheCodingMachine\ClassExplorer\ClassExplorerInterface;

class GlobClassExplorerTest extends TestCase
{
public function testGetClasses()
{
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new NullCache());
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache());
$classes = $explorer->getClasses();

$this->assertSame([GlobClassExplorer::class], $classes);
$this->assertSame([GlobClassExplorer::class, ClassExplorerInterface::class], $classes);
}

public function testGetClassesNonRecursive()
{
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, false);
$classes = $explorer->getClasses();

$this->assertSame([ClassExplorerInterface::class], $classes);
}

public function testGetDevClasses()
Expand Down

0 comments on commit 5d5ffdf

Please sign in to comment.