diff --git a/README.md b/README.md index da483a3..9c48fcf 100644 --- a/README.md +++ b/README.md @@ -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; } ``` diff --git a/composer.json b/composer.json index b9b3769..c8520a3 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Glob/GlobClassExplorer.php b/src/Glob/GlobClassExplorer.php index accc980..0c08c32 100644 --- a/src/Glob/GlobClassExplorer.php +++ b/src/Glob/GlobClassExplorer.php @@ -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) { @@ -78,15 +82,17 @@ public function getClasses(): array /** * Returns an array mapping the fully qualified class name to the file path. * - * @return array + * @return array */ 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; } @@ -94,7 +100,7 @@ public function getClassMap(): array /** * Returns an array of fully qualified class names, without the cache. * - * @return array + * @return array */ private function doGetClassMap(): array { @@ -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); diff --git a/tests/Glob/GlobClassExplorerTest.php b/tests/Glob/GlobClassExplorerTest.php index a9b6110..0cd1565 100644 --- a/tests/Glob/GlobClassExplorerTest.php +++ b/tests/Glob/GlobClassExplorerTest.php @@ -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]); } }