1
0
mirror of https://github.com/danog/class-finder.git synced 2024-11-30 04:29:03 +01:00

Add support for finding classes declared in a classmap

This commit is contained in:
Hayden Pierce 2018-09-08 11:01:08 -05:00
parent 67831937e3
commit c12a9779cc
3 changed files with 42 additions and 8 deletions

View File

@ -1,16 +1,36 @@
<?php <?php
namespace HaydenPierce\ClassFinder\Classmap; namespace HaydenPierce\ClassFinder\Classmap;
use HaydenPierce\ClassFinder\Exception\ClassFinderException;
class ClassmapEntry class ClassmapEntry
{ {
private $namespace; private $className;
private $directories;
public function __construct($namespace, $directories) public function __construct($fullyQualifiedClassName)
{ {
$this->namespace = $namespace; $this->className = $fullyQualifiedClassName;
$this->directories = $directories;
} }
/**
* Checks if the class is a DIRECT child of the given namespace. Currently, no other finders support "recursively"
* discovering classes, so the Classmap module will not be the exception to that rule.
*
* @param $namespace
* @return bool
*/
public function matches($namespace)
{
$classNameFragments = explode('\\', $this->getClassName());
array_pop($classNameFragments);
$classNamespace = implode('\\', $classNameFragments);
$namespace = trim($namespace, '\\');
return $namespace === $classNamespace;
}
public function getClassName()
{
return $this->className;
}
} }

View File

@ -2,7 +2,6 @@
namespace HaydenPierce\ClassFinder\Classmap; namespace HaydenPierce\ClassFinder\Classmap;
use HaydenPierce\ClassFinder\AppConfig; use HaydenPierce\ClassFinder\AppConfig;
use HaydenPierce\ClassFinder\Exception\ClassFinderException;
class ClassmapEntryFactory class ClassmapEntryFactory
{ {
@ -19,6 +18,13 @@ class ClassmapEntryFactory
*/ */
public function getClassmapEntries() public function getClassmapEntries()
{ {
// Composer will compile user declared mappings to autoload_classmap.php. So no additional work is needed
// to fetch user provided entries.
$classmap = require($this->appConfig->getAppRoot() . 'vendor/composer/autoload_classmap.php');
$classmapKeys = array_keys($classmap);
return array_map(function($index) use ($classmapKeys){
return new ClassmapEntry($classmapKeys[$index]);
}, range(0, count($classmap)));
} }
} }

View File

@ -19,6 +19,14 @@ class ClassmapFinder implements FinderInterface
*/ */
public function findClasses($namespace) public function findClasses($namespace)
{ {
$classmapEntries = $this->factory->getClassmapEntries();
$matchingEntries = array_filter($classmapEntries, function(ClassmapEntry $entry) use ($namespace) {
return $entry->matches($namespace);
});
return array_map(function(ClassmapEntry $entry) {
return $entry->getClassName();
}, $matchingEntries);
} }
} }