1
0
mirror of https://github.com/danog/class-finder.git synced 2024-11-26 20:14:59 +01:00

Avoid double preloading

This commit is contained in:
Daniil Gentili 2020-10-13 09:58:22 +02:00
parent fb18493be6
commit e9fbb58f22
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 20 additions and 22 deletions

View File

@ -18,26 +18,12 @@ class ClassmapEntryFactory
/** /**
* @return ClassmapEntry[] * @return ClassmapEntry[]
*/ */
public function getClassmapEntries($allowAdditional) public function getClassmapEntries()
{ {
// Composer will compile user declared mappings to autoload_classmap.php. So no additional work is needed // Composer will compile user declared mappings to autoload_classmap.php. So no additional work is needed
// to fetch user provided entries. // to fetch user provided entries.
$classmap = require($this->appConfig->getAppRoot().'vendor/composer/autoload_classmap.php'); $classmap = require($this->appConfig->getAppRoot().'vendor/composer/autoload_classmap.php');
$finalClassMap = [];
foreach ($classmap as $potentialClass => $file) {
if (\function_exists($potentialClass)) {
if ($allowAdditional & ClassFinder::ALLOW_FUNCTIONS) {
$finalClassMap[$potentialClass] = $file;
}
} elseif ($allowAdditional & ClassFinder::ALLOW_CLASSES && \class_exists($potentialClass)
|| ($allowAdditional & ClassFinder::ALLOW_INTERFACES && \interface_exists($potentialClass))
|| ($allowAdditional & ClassFinder::ALLOW_TRAITS && \trait_exists($potentialClass))) {
$finalClassMap[$potentialClass] = $file;
}
}
$classmap = $finalClassMap;
// if classmap has no entries return empty array // if classmap has no entries return empty array
if (\count($classmap) == 0) { if (\count($classmap) == 0) {
return []; return [];

View File

@ -21,7 +21,7 @@ class ClassmapFinder implements FinderInterface
*/ */
public function isNamespaceKnown($namespace) public function isNamespaceKnown($namespace)
{ {
$classmapEntries = $this->factory->getClassmapEntries(ClassFinder::ALLOW_ALL); $classmapEntries = $this->factory->getClassmapEntries();
foreach($classmapEntries as $classmapEntry) { foreach($classmapEntries as $classmapEntry) {
if ($classmapEntry->knowsNamespace($namespace)) { if ($classmapEntry->knowsNamespace($namespace)) {
@ -39,10 +39,22 @@ class ClassmapFinder implements FinderInterface
*/ */
public function findClasses($namespace, $options) public function findClasses($namespace, $options)
{ {
$classmapEntries = $this->factory->getClassmapEntries($options); $classmapEntries = $this->factory->getClassmapEntries();
$matchingEntries = array_filter($classmapEntries, function(ClassmapEntry $entry) use ($namespace, $options) { $matchingEntries = array_filter($classmapEntries, function(ClassmapEntry $entry) use ($namespace, $options) {
return $entry->matches($namespace, $options); if (!$entry->matches($namespace, $options)) return false;
$potentialClass = $entry->getClassName();
if (function_exists($potentialClass)) {
// For some reason calling class_exists() on a namespace'd function raises a Fatal Error (tested PHP 7.0.8)
// Example: DeepCopy\deep_copy
return $options & ClassFinder::ALLOW_FUNCTIONS;
} else if (class_exists($potentialClass)) {
return $options & ClassFinder::ALLOW_CLASSES;
} else if (interface_exists($potentialClass, false)) {
return $options & ClassFinder::ALLOW_INTERFACES;
} else if (trait_exists($potentialClass, false)) {
return $options & ClassFinder::ALLOW_TRAITS;
}
}); });
return array_map(function(ClassmapEntry $entry) { return array_map(function(ClassmapEntry $entry) {

View File

@ -206,8 +206,8 @@ class PSR4Namespace
return ($allowAdditional & ClassFinder::ALLOW_FUNCTIONS); return ($allowAdditional & ClassFinder::ALLOW_FUNCTIONS);
} else { } else {
return ($allowAdditional & ClassFinder::ALLOW_CLASSES && class_exists($potentialClass)) return ($allowAdditional & ClassFinder::ALLOW_CLASSES && class_exists($potentialClass))
|| ($allowAdditional & ClassFinder::ALLOW_INTERFACES && interface_exists($potentialClass)) || ($allowAdditional & ClassFinder::ALLOW_INTERFACES && interface_exists($potentialClass, false))
|| ($allowAdditional & ClassFinder::ALLOW_TRAITS && trait_exists($potentialClass)); || ($allowAdditional & ClassFinder::ALLOW_TRAITS && trait_exists($potentialClass, false));
} }
}); });
} }
@ -250,8 +250,8 @@ class PSR4Namespace
return ($allowAdditional & ClassFinder::ALLOW_FUNCTIONS); return ($allowAdditional & ClassFinder::ALLOW_FUNCTIONS);
} else { } else {
return ($allowAdditional & ClassFinder::ALLOW_CLASSES && class_exists($potentialClass)) return ($allowAdditional & ClassFinder::ALLOW_CLASSES && class_exists($potentialClass))
|| ($allowAdditional & ClassFinder::ALLOW_INTERFACES && interface_exists($potentialClass)) || ($allowAdditional & ClassFinder::ALLOW_INTERFACES && interface_exists($potentialClass, false))
|| ($allowAdditional & ClassFinder::ALLOW_TRAITS && trait_exists($potentialClass)); || ($allowAdditional & ClassFinder::ALLOW_TRAITS && trait_exists($potentialClass, false));
} }
}); });
} }