Skip to content

Introdce a safe_class_exists #110

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

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"autoload": {
"psr-4": {
"Http\\Discovery\\": "src/"
}
},
"files": ["src/functions_include.php"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there no autoloading for functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only classes are autoloaded.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, otherwise we would need a naming scheme for function to filename...

},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 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 safe_class_exists($condition);
} elseif (is_callable($condition)) {
return $condition();
} elseif (is_bool($condition)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Strategy/PuliBetaStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\Discovery\Strategy;

use Http\Discovery\Exception\PuliUnavailableException;
use function Http\Discovery\safe_class_exists;
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 (!safe_class_exists($puliFactoryClass)) {
throw new PuliUnavailableException('Puli Factory class does not exist');
}

Expand Down
24 changes: 24 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Http\Discovery;

/**
* 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
*/
function safe_class_exists($class, $autoload = true)
{
try {
return class_exists($class, $autoload);
} catch (\Exception $e) {
return false;
}
}
6 changes: 6 additions & 0 deletions src/functions_include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Don't redefine the functions if included multiple times.
if (!function_exists('Http\Discovery\safe_class_exists')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not require_once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this allows you to override... Im not sure. I just did it the "guzzle" way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that allows to override by hijacking the namespace. sounds like either horrible situations or really bad design but i guess it does not hurt too much.

also will be quite some issue with having things loaded in the right order. but if its helpful to people to have it, i guess the overhad is insignificant so lets do it this way.

require __DIR__.'/functions.php';
}