Skip to content

Commit 433e3d0

Browse files
authored
Merge pull request #11 from moufmouf/filemap_cache_fix
Changing the return type of getClassMap
2 parents 78ec2b0 + a292a38 commit 433e3d0

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ $explorer = new GlobClassExplorer('\\This\\Namespace\\Only\\', $psr16Cache, $cac
4343

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

4848
```php
4949
$classMap = $explorer->getClassMap();
50-
foreach ($classMap as $class => $fileInfo) {
51-
echo 'Class '.$class.' found in file '.$fileInfo->getPathname();
50+
foreach ($classMap as $class => $file) {
51+
echo 'Class '.$class.' found in file '.$file;
5252
}
5353
```

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require": {
1616
"php": ">=7.1",
1717
"psr/simple-cache": "^1",
18-
"mouf/classname-mapper": "^1"
18+
"mouf/classname-mapper": "^1",
19+
"ext-hash": "*"
1920
},
2021
"require-dev": {
2122
"php-coveralls/php-coveralls": "^2.1",

src/Glob/GlobClassExplorer.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class GlobClassExplorer implements ClassExplorerInterface
5454
* @var string
5555
*/
5656
private $rootPath;
57+
/**
58+
* @var string|null
59+
*/
60+
private $key;
5761

5862
public function __construct(string $namespace, CacheInterface $cache, ?int $cacheTtl = null, ?ClassNameMapper $classNameMapper = null, bool $recursive = true, ?string $rootPath = null)
5963
{
@@ -78,23 +82,25 @@ public function getClasses(): array
7882
/**
7983
* Returns an array mapping the fully qualified class name to the file path.
8084
*
81-
* @return array<string,SplFileInfo>
85+
* @return array<string,string>
8286
*/
8387
public function getClassMap(): array
8488
{
85-
$key = 'globClassExplorer_'.str_replace('\\', '_', $this->namespace);
86-
$classes = $this->cache->get($key);
89+
if ($this->key === null) {
90+
$this->key = 'globClassExplorer_'.hash('md4', $this->namespace.'___'.$this->recursive.$this->rootPath);
91+
}
92+
$classes = $this->cache->get($this->key);
8793
if ($classes === null) {
8894
$classes = $this->doGetClassMap();
89-
$this->cache->set($key, $classes, $this->cacheTtl);
95+
$this->cache->set($this->key, $classes, $this->cacheTtl);
9096
}
9197
return $classes;
9298
}
9399

94100
/**
95101
* Returns an array of fully qualified class names, without the cache.
96102
*
97-
* @return array<string,SplFileInfo>
103+
* @return array<string,string>
98104
*/
99105
private function doGetClassMap(): array
100106
{
@@ -115,7 +121,7 @@ private function doGetClassMap(): array
115121
foreach ($filesForDir as $file) {
116122
// Trim the root directory name and the PHP extension
117123
$fileTrimPrefixSuffix = \substr($file, $dirLen, -4);
118-
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file;
124+
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file->getRealPath();
119125
}
120126
}
121127
chdir($oldCwd);

tests/Glob/GlobClassExplorerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public function testGetClassMap()
4747
$classMap = $explorer->getClassMap();
4848

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

0 commit comments

Comments
 (0)