From d60a47a55e09560bbe0b3441bc6d146e0270e7a3 Mon Sep 17 00:00:00 2001 From: Hayden Pierce Date: Sat, 8 Sep 2018 20:10:11 -0500 Subject: [PATCH] Implement isNamespaceKnown on ClassmapFinder --- src/ClassFinder.php | 19 ++++++++++++++++--- src/Classmap/ClassmapEntry.php | 5 +++++ src/Classmap/ClassmapFinder.php | 13 +++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/ClassFinder.php b/src/ClassFinder.php index a2fa73e..93d8735 100644 --- a/src/ClassFinder.php +++ b/src/ClassFinder.php @@ -1,6 +1,8 @@ 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) diff --git a/src/Classmap/ClassmapEntry.php b/src/Classmap/ClassmapEntry.php index 1a67e8e..b2f4383 100644 --- a/src/Classmap/ClassmapEntry.php +++ b/src/Classmap/ClassmapEntry.php @@ -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. diff --git a/src/Classmap/ClassmapFinder.php b/src/Classmap/ClassmapFinder.php index c1276ed..b776008 100644 --- a/src/Classmap/ClassmapFinder.php +++ b/src/Classmap/ClassmapFinder.php @@ -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