From 2bc1ae7e6b4dd431de005478436eff1057dc1541 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 17 Aug 2018 13:59:53 +0200 Subject: [PATCH 1/2] Add ClassDiscovery::safeClassExists --- src/ClassDiscovery.php | 24 +++++++++++++++++++++++- src/Strategy/PuliBetaStrategy.php | 3 ++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index 760df6b..10cfd9d 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -161,7 +161,7 @@ protected static function evaluateCondition($condition) { if (is_string($condition)) { // Should be extended for functions, extensions??? - return class_exists($condition); + return self::safeClassExists($condition); } elseif (is_callable($condition)) { return $condition(); } elseif (is_bool($condition)) { @@ -205,4 +205,26 @@ protected static function instantiateClass($class) throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string'); } + + /** + * We want to do 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. + * + * This function will catch all potential exceptions and make sure it returns a boolean. + * + * @param string $class + * @param bool $autoload + * + * @return bool + */ + public static function safeClassExists($class) + { + try { + return class_exists($class); + } catch (\Exception $e) { + return false; + } + } + } diff --git a/src/Strategy/PuliBetaStrategy.php b/src/Strategy/PuliBetaStrategy.php index 2666fb3..c1e1fb7 100644 --- a/src/Strategy/PuliBetaStrategy.php +++ b/src/Strategy/PuliBetaStrategy.php @@ -2,6 +2,7 @@ namespace Http\Discovery\Strategy; +use Http\Discovery\ClassDiscovery; use Http\Discovery\Exception\PuliUnavailableException; use Puli\Discovery\Api\Discovery; use Puli\GeneratedPuliFactory; @@ -41,7 +42,7 @@ private static function getPuliFactory() $puliFactoryClass = PULI_FACTORY_CLASS; - if (!class_exists($puliFactoryClass)) { + if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { throw new PuliUnavailableException('Puli Factory class does not exist'); } From 43fb8c1790fdfe8d4f8e49d63561a30caef58528 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 17 Aug 2018 14:00:57 +0200 Subject: [PATCH 2/2] cs --- src/ClassDiscovery.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index 10cfd9d..b2d0bd4 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -226,5 +226,4 @@ public static function safeClassExists($class) return false; } } - }