diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index 855145f..5ae7dee 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -2,6 +2,7 @@ namespace Http\Discovery; +use Http\Discovery\Exception\ClassinstantiationFailedException; use Http\Discovery\Exception\DiscoveryFailedException; use Http\Discovery\Exception\StrategyUnavailableException; @@ -36,7 +37,7 @@ abstract class ClassDiscovery * * @param string $type * - * @return string + * @return string|\Closure * * @throws DiscoveryFailedException */ @@ -177,4 +178,28 @@ protected static function evaluateCondition($condition) return false; } + + /** + * Get an instance of the $class. + * + * @param string|\Closure $class A FQN of a class or a closure that instantiate the class. + * + * @return object + */ + protected static function instantiateClass($class) + { + try { + if (is_string($class)) { + return new $class(); + } + + if (is_callable($class)) { + return $class(); + } + } catch (\Exception $e) { + throw new ClassinstantiationFailedException('Unexcepced exception when instantiating class.', 0, $e); + } + + throw new ClassinstantiationFailedException('Could not instantiate class becuase parameter is neitehr a callable or a string'); + } } diff --git a/src/Exception/ClassinstantiationFailedException.php b/src/Exception/ClassinstantiationFailedException.php new file mode 100644 index 0000000..5bb880a --- /dev/null +++ b/src/Exception/ClassinstantiationFailedException.php @@ -0,0 +1,14 @@ + + */ +class ClassinstantiationFailedException extends \RuntimeException implements Exception +{ +} diff --git a/src/HttpAsyncClientDiscovery.php b/src/HttpAsyncClientDiscovery.php index 3b01c4a..1756f22 100644 --- a/src/HttpAsyncClientDiscovery.php +++ b/src/HttpAsyncClientDiscovery.php @@ -24,7 +24,7 @@ public static function find() try { $asyncClient = static::findOneByType(HttpAsyncClient::class); - return new $asyncClient(); + return static::instantiateClass($asyncClient); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".', diff --git a/src/HttpClientDiscovery.php b/src/HttpClientDiscovery.php index d7b2dcd..f7a08ed 100644 --- a/src/HttpClientDiscovery.php +++ b/src/HttpClientDiscovery.php @@ -24,7 +24,7 @@ public static function find() try { $client = static::findOneByType(HttpClient::class); - return new $client(); + return static::instantiateClass($client); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".', diff --git a/src/MessageFactoryDiscovery.php b/src/MessageFactoryDiscovery.php index 93af93c..30a6f5e 100644 --- a/src/MessageFactoryDiscovery.php +++ b/src/MessageFactoryDiscovery.php @@ -24,7 +24,7 @@ public static function find() try { $messageFactory = static::findOneByType(MessageFactory::class); - return new $messageFactory(); + return static::instantiateClass($messageFactory); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No message factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.', diff --git a/src/StreamFactoryDiscovery.php b/src/StreamFactoryDiscovery.php index c2fdeb8..b59e4b7 100644 --- a/src/StreamFactoryDiscovery.php +++ b/src/StreamFactoryDiscovery.php @@ -24,7 +24,7 @@ public static function find() try { $streamFactory = static::findOneByType(StreamFactory::class); - return new $streamFactory(); + return static::instantiateClass($streamFactory); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No stream factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.', diff --git a/src/UriFactoryDiscovery.php b/src/UriFactoryDiscovery.php index 7388752..fab6d4d 100644 --- a/src/UriFactoryDiscovery.php +++ b/src/UriFactoryDiscovery.php @@ -24,7 +24,7 @@ public static function find() try { $uriFactory = static::findOneByType(UriFactory::class); - return new $uriFactory(); + return static::instantiateClass($uriFactory); } catch (DiscoveryFailedException $e) { throw new NotFoundException( 'No uri factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.',