Skip to content

Add ClassDiscovery::safeClassExists #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/ClassDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -205,4 +205,25 @@ 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;
}
}
}
3 changes: 2 additions & 1 deletion src/Strategy/PuliBetaStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down