Skip to content

Commit 9fcc4c9

Browse files
authored
Merge pull request #111 from php-http/patch-107b
Add ClassDiscovery::safeClassExists
2 parents 7e9a472 + 43fb8c1 commit 9fcc4c9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/ClassDiscovery.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected static function evaluateCondition($condition)
161161
{
162162
if (is_string($condition)) {
163163
// Should be extended for functions, extensions???
164-
return class_exists($condition);
164+
return self::safeClassExists($condition);
165165
} elseif (is_callable($condition)) {
166166
return $condition();
167167
} elseif (is_bool($condition)) {
@@ -205,4 +205,25 @@ protected static function instantiateClass($class)
205205

206206
throw new ClassInstantiationFailedException('Could not instantiate class because parameter is neither a callable nor a string');
207207
}
208+
209+
/**
210+
* We want to do a "safe" version of PHP's "class_exists" because Magento has a bug
211+
* (or they call it a "feature"). Magento is throwing an exception if you do class_exists()
212+
* on a class that ends with "Factory" and if that file does not exits.
213+
*
214+
* This function will catch all potential exceptions and make sure it returns a boolean.
215+
*
216+
* @param string $class
217+
* @param bool $autoload
218+
*
219+
* @return bool
220+
*/
221+
public static function safeClassExists($class)
222+
{
223+
try {
224+
return class_exists($class);
225+
} catch (\Exception $e) {
226+
return false;
227+
}
228+
}
208229
}

src/Strategy/PuliBetaStrategy.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Discovery\Strategy;
44

5+
use Http\Discovery\ClassDiscovery;
56
use Http\Discovery\Exception\PuliUnavailableException;
67
use Puli\Discovery\Api\Discovery;
78
use Puli\GeneratedPuliFactory;
@@ -41,7 +42,7 @@ private static function getPuliFactory()
4142

4243
$puliFactoryClass = PULI_FACTORY_CLASS;
4344

44-
if (!class_exists($puliFactoryClass)) {
45+
if (!ClassDiscovery::safeClassExists($puliFactoryClass)) {
4546
throw new PuliUnavailableException('Puli Factory class does not exist');
4647
}
4748

0 commit comments

Comments
 (0)