Skip to content

Commit

Permalink
Merge pull request #7 from moufmouf/relative_directory
Browse files Browse the repository at this point in the history
Fix current working directory issues
  • Loading branch information
moufmouf committed Jan 18, 2019
2 parents 5d5ffdf + 3d7c893 commit 3c09649
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -23,7 +23,7 @@
"squizlabs/php_codesniffer": "^3.2.3",
"phpstan/phpstan": "^0.10.3",
"maglnet/composer-require-checker": "^1.0",
"thecodingmachine/phpstan-strict-rules": "^0.10.3",
"thecodingmachine/phpstan-strict-rules": "^0.11.0",
"symfony/cache": "^4.1.4"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions phpstan.neon
@@ -1,5 +1,5 @@
#parameters:
# ignoreErrors:
# - "#Instantiated class WeakRef not found.#"
parameters:
ignoreErrors:
- "/Parameter #1 $directory of function chdir expects string, string|false given./"
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
16 changes: 13 additions & 3 deletions src/Glob/GlobClassExplorer.php
Expand Up @@ -3,6 +3,7 @@

namespace TheCodingMachine\ClassExplorer\Glob;

use function chdir;
use DirectoryIterator;
use GlobIterator;
use Mouf\Composer\ClassNameMapper;
Expand Down Expand Up @@ -47,14 +48,19 @@ class GlobClassExplorer implements ClassExplorerInterface
* @var bool
*/
private $recursive;
/**
* @var string
*/
private $rootPath;

public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true)
public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true, ?string $rootPath = null)
{
$this->namespace = $namespace;
$this->cache = $cache;
$this->cacheTtl = $cacheTtl;
$this->classNameMapper = $classNameMapper;
$this->recursive = $recursive;
$this->rootPath = ($rootPath === null) ? __DIR__.'/../../../../../' : rtrim($rootPath, '/').'/';
}

/**
Expand Down Expand Up @@ -107,14 +113,18 @@ private function doGetClasses(): array
*/
private function getPhpFilesForDir(string $directory): \Iterator
{
$oldCwd = getcwd();
chdir($this->rootPath);
if (!\is_dir($directory)) {
return new \EmptyIterator();
}
if ($this->recursive) {
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS));
return new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
$iterator = new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
} else {
return new GlobIterator($directory.'/*.php');
$iterator = new GlobIterator($directory.'/*.php');
}
chdir($oldCwd);
return $iterator;
}
}
8 changes: 4 additions & 4 deletions tests/Glob/GlobClassExplorerTest.php
Expand Up @@ -11,31 +11,31 @@ class GlobClassExplorerTest extends TestCase
{
public function testGetClasses()
{
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache());
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, true, __DIR__.'/../..');
$classes = $explorer->getClasses();

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

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

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

public function testGetDevClasses()
{
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new NullCache(), null, ClassNameMapper::createFromComposerFile(null, null, true));
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\', new NullCache(), null, ClassNameMapper::createFromComposerFile(null, null, true), true, __DIR__.'/../..');
$classes = $explorer->getClasses();

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

public function testGetNotExistingClasses()
{
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\Foobar\\', new NullCache());
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\Glob\\Foobar\\', new NullCache(), null, null, true, __DIR__.'/../..');
$classes = $explorer->getClasses();

$this->assertSame([], $classes);
Expand Down

0 comments on commit 3c09649

Please sign in to comment.