Skip to content

Commit

Permalink
Merge pull request #11 from moufmouf/filemap_cache_fix
Browse files Browse the repository at this point in the history
Changing the return type of getClassMap
  • Loading branch information
moufmouf committed Jan 13, 2020
2 parents 78ec2b0 + a292a38 commit 433e3d0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
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
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
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
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]);
}
}

0 comments on commit 433e3d0

Please sign in to comment.