Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ $explorer = new GlobClassExplorer('\\This\\Namespace\\Only\\', $psr16Cache, $cac

You can also get a class map using the `getClassMap` method.
A class map is an array associating the name of the classes found (in key), to the file they are
linked to (a `SplFileInfo` in value).
linked to (the real path of the file).

```php
$classMap = $explorer->getClassMap();
foreach ($classMap as $class => $fileInfo) {
echo 'Class '.$class.' found in file '.$fileInfo->getPathname();
foreach ($classMap as $class => $file) {
echo 'Class '.$class.' found in file '.$file;
}
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"require": {
"php": ">=7.1",
"psr/simple-cache": "^1",
"mouf/classname-mapper": "^1"
"mouf/classname-mapper": "^1",
"ext-hash": "*"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
Expand Down
18 changes: 12 additions & 6 deletions src/Glob/GlobClassExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class GlobClassExplorer implements ClassExplorerInterface
* @var string
*/
private $rootPath;
/**
* @var string|null
*/
private $key;

public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true, ?string $rootPath = null)
{
Expand All @@ -78,23 +82,25 @@ public function getClasses(): array
/**
* Returns an array mapping the fully qualified class name to the file path.
*
* @return array<string,SplFileInfo>
* @return array<string,string>
*/
public function getClassMap(): array
{
$key = 'globClassExplorer_'.str_replace('\\', '_', $this->namespace);
$classes = $this->cache->get($key);
if ($this->key === null) {
$this->key = 'globClassExplorer_'.hash('md4', $this->namespace.'___'.$this->recursive.$this->rootPath);
}
$classes = $this->cache->get($this->key);
if ($classes === null) {
$classes = $this->doGetClassMap();
$this->cache->set($key, $classes, $this->cacheTtl);
$this->cache->set($this->key, $classes, $this->cacheTtl);
}
return $classes;
}

/**
* Returns an array of fully qualified class names, without the cache.
*
* @return array<string,SplFileInfo>
* @return array<string,string>
*/
private function doGetClassMap(): array
{
Expand All @@ -115,7 +121,7 @@ private function doGetClassMap(): array
foreach ($filesForDir as $file) {
// Trim the root directory name and the PHP extension
$fileTrimPrefixSuffix = \substr($file, $dirLen, -4);
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file;
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file->getRealPath();
}
}
chdir($oldCwd);
Expand Down
2 changes: 1 addition & 1 deletion tests/Glob/GlobClassExplorerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public function testGetClassMap()
$classMap = $explorer->getClassMap();

$this->assertArrayHasKey(GlobClassExplorer::class, $classMap);
$this->assertSame('src/Glob/GlobClassExplorer.php', (string) $classMap[GlobClassExplorer::class]);
$this->assertStringEndsWith('src/Glob/GlobClassExplorer.php', (string) $classMap[GlobClassExplorer::class]);
}
}