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

Implement isNamespaceKnown on ClassmapFinder

This commit is contained in:
Hayden Pierce 2018-09-08 20:10:11 -05:00
parent f92b617082
commit d60a47a55e
3 changed files with 34 additions and 3 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace HaydenPierce\ClassFinder;
use HaydenPierce\ClassFinder\Classmap\ClassmapEntryFactory;
use HaydenPierce\ClassFinder\Classmap\ClassmapFinder;
use HaydenPierce\ClassFinder\Exception\ClassFinderException;
use HaydenPierce\ClassFinder\PSR4\PSR4Finder;
use HaydenPierce\ClassFinder\PSR4\PSR4NamespaceFactory;
@ -13,6 +15,9 @@ class ClassFinder
/** @var PSR4Finder */
private static $psr4;
/** @var ClassmapFinder */
private static $classmap;
private static function initialize()
{
if (!(self::$config instanceof AppConfig)) {
@ -23,6 +28,11 @@ class ClassFinder
$PSR4Factory = new PSR4NamespaceFactory(self::$config);
self::$psr4 = new PSR4Finder($PSR4Factory);
}
if (!(self::$classmap instanceof ClassmapFinder)) {
$classmapFactory = new ClassmapEntryFactory(self::$config);
self::$classmap = new ClassmapFinder($classmapFactory);
}
}
/**
@ -34,7 +44,7 @@ class ClassFinder
{
self::initialize();
$isNamespaceKnown = self::$psr4->isNamespaceKnown($namespace);
$isNamespaceKnown = self::$psr4->isNamespaceKnown($namespace) || self::$classmap->isNamespaceKnown($namespace);
if (!$isNamespaceKnown) {
throw new ClassFinderException(sprintf("Unknown namespace '%s'. You should add the namespace to composer.json. See '%s' for details.",
$namespace,
@ -42,9 +52,12 @@ class ClassFinder
));
}
$classes = self::$psr4->findClasses($namespace);
$psr4classes = self::$psr4->findClasses($namespace);
$classmapClasses = self::$classmap->findClasses($namespace);
return array_values($classes);
$classes = array_merge($psr4classes, $classmapClasses);
return array_unique($classes);
}
public static function setAppRoot($appRoot)

View File

@ -10,6 +10,11 @@ class ClassmapEntry
$this->className = $fullyQualifiedClassName;
}
public function knowsNamespace($namespace)
{
return strpos($this->className, $namespace) !== false;
}
/**
* 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.

View File

@ -12,6 +12,19 @@ class ClassmapFinder implements FinderInterface
$this->factory = $factory;
}
public function isNamespaceKnown($namespace)
{
$classmapEntries = $this->factory->getClassmapEntries();
foreach($classmapEntries as $classmapEntry) {
if ($classmapEntry->knowsNamespace($namespace)) {
return true;
}
}
return false;
}
/**
* @param $namespace
* @return bool|string