ClassDiscovery
- Source
wp-includes/php-ai-client/third-party/Http/Discovery/ClassDiscovery.php:17
Registry that based find results on class existence.
Compatibility
- WordPress
- core
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases.
Properties · 3
$strategiesWordPress\AiClientDependencies\Http\Discovery\Strategy\DiscoveryStrategy[]private static- A list of strategies to find classes.
$deprecatedStrategiesprivate static$cachearrayprivate static- Discovery cache to make the second time we use discovery faster.
Methods · 11
- findOneByType()Finds a class.
- getFromCache()Get a value from cache.
- storeInCache()Store a value in cache.
- setStrategies()Set new strategies and clear the cache.
- getStrategies()Returns the currently configured discovery strategies as fully qualified class names.
- appendStrategy()Append a strategy at the end of the strategy queue.
- prependStrategy()Prepend a strategy at the beginning of the strategy queue.
- clearCache()
- evaluateCondition()Evaluates conditions to boolean.
- instantiateClass()Get an instance of the $class.
- safeClassExists()We need a "safe" version of PHP's "class_exists" because Magento has a bug (or they call it a "feature"). Magento is throwing an exception if you do class_exists() on a class that ends with "Factory" and if that file does not exits.
Source code
abstract class ClassDiscovery{ /** * A list of strategies to find classes. * * @var DiscoveryStrategy[] */ private static $strategies = [Strategy\GeneratedDiscoveryStrategy::class, Strategy\CommonClassesStrategy::class, Strategy\CommonPsr17ClassesStrategy::class, Strategy\PuliBetaStrategy::class]; private static $deprecatedStrategies = [Strategy\PuliBetaStrategy::class => \true]; /** * Discovery cache to make the second time we use discovery faster. * * @var array */ private static $cache = []; /** * Finds a class. * * @param string $type * * @return string|\Closure * * @throws DiscoveryFailedException */ protected static function findOneByType($type) { // Look in the cache if (null !== $class = self::getFromCache($type)) { return $class; } static $skipStrategy; $skipStrategy ?? $skipStrategy = self::safeClassExists(Strategy\GeneratedDiscoveryStrategy::class) ? \false : Strategy\GeneratedDiscoveryStrategy::class; $exceptions = []; foreach (self::$strategies as $strategy) { if ($skipStrategy === $strategy) { continue; } try { $candidates = $strategy::getCandidates($type); } catch (StrategyUnavailableException $e) { if (!isset(self::$deprecatedStrategies[$strategy])) { $exceptions[] = $e; } continue; } foreach ($candidates as $candidate) { if (isset($candidate['condition'])) { if (!self::evaluateCondition($candidate['condition'])) { continue; } } // save the result for later use self::storeInCache($type, $candidate); return $candidate['class']; } $exceptions[] = new NoCandidateFoundException($strategy, $candidates); } throw DiscoveryFailedException::create($exceptions); } /** * Get a value from cache. * * @param string $type * * @return string|null */ private static function getFromCache($type) { if (!isset(self::$cache[$type])) { return; } $candidate = self::$cache[$type]; if (isset($candidate['condition'])) { if (!self::evaluateCondition($candidate['condition'])) { return; } } return $candidate['class']; } /**Changelog
Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/php-ai-client/third-party/Http/Discovery/ClassDiscovery.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.