Skip to content

Commit 627e674

Browse files
committed
Fix current working directory issues
1 parent 5d5ffdf commit 627e674

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Glob/GlobClassExplorer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace TheCodingMachine\ClassExplorer\Glob;
55

6+
use function chdir;
67
use DirectoryIterator;
78
use GlobIterator;
89
use Mouf\Composer\ClassNameMapper;
@@ -47,14 +48,19 @@ class GlobClassExplorer implements ClassExplorerInterface
4748
* @var bool
4849
*/
4950
private $recursive;
51+
/**
52+
* @var string|null
53+
*/
54+
private $rootPath;
5055

51-
public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true)
56+
public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true, ?string $rootPath = null)
5257
{
5358
$this->namespace = $namespace;
5459
$this->cache = $cache;
5560
$this->cacheTtl = $cacheTtl;
5661
$this->classNameMapper = $classNameMapper;
5762
$this->recursive = $recursive;
63+
$this->rootPath = ($rootPath === null) ? __DIR__.'/../../../../../' : rtrim($rootPath, '/').'/';
5864
}
5965

6066
/**
@@ -107,14 +113,18 @@ private function doGetClasses(): array
107113
*/
108114
private function getPhpFilesForDir(string $directory): \Iterator
109115
{
116+
$oldCwd = getcwd();
117+
chdir($this->rootPath);
110118
if (!\is_dir($directory)) {
111119
return new \EmptyIterator();
112120
}
113121
if ($this->recursive) {
114122
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS));
115-
return new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
123+
$iterator = new RegexIterator($allFiles, '/\.php$/i'/*, \RecursiveRegexIterator::GET_MATCH*/);
116124
} else {
117-
return new GlobIterator($directory.'/*.php');
125+
$iterator = new GlobIterator($directory.'/*.php');
118126
}
127+
chdir($oldCwd);
128+
return $iterator;
119129
}
120130
}

tests/Glob/GlobClassExplorerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ class GlobClassExplorerTest extends TestCase
1111
{
1212
public function testGetClasses()
1313
{
14-
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache());
14+
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, true, __DIR__.'/../..');
1515
$classes = $explorer->getClasses();
1616

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)