From 3b1975951a537de4cb100089ac72111b61542f21 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 30 Jun 2016 14:30:12 +0200 Subject: [PATCH 1/3] Allow for a candidate to have a Closure as a class parameter --- src/ClassDiscovery.php | 27 ++++++++++++++++++- .../ClassinstantiationFailedException.php | 12 +++++++++ src/HttpAsyncClientDiscovery.php | 2 +- src/HttpClientDiscovery.php | 2 +- src/MessageFactoryDiscovery.php | 4 +-- src/StreamFactoryDiscovery.php | 2 +- src/UriFactoryDiscovery.php | 2 +- 7 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/Exception/ClassinstantiationFailedException.php diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index 855145f..d32c80f 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..d070cbc --- /dev/null +++ b/src/Exception/ClassinstantiationFailedException.php @@ -0,0 +1,12 @@ + + */ +class ClassinstantiationFailedException extends \RuntimeException +{ +} 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..e4662d5 100644 --- a/src/MessageFactoryDiscovery.php +++ b/src/MessageFactoryDiscovery.php @@ -23,8 +23,8 @@ 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.', From 7766a778864ef4cfddd300d1118c02301482924e Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sat, 2 Jul 2016 15:02:25 +0200 Subject: [PATCH 2/3] style fix --- src/ClassDiscovery.php | 2 +- src/MessageFactoryDiscovery.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ClassDiscovery.php b/src/ClassDiscovery.php index d32c80f..5ae7dee 100644 --- a/src/ClassDiscovery.php +++ b/src/ClassDiscovery.php @@ -199,7 +199,7 @@ protected static function instantiateClass($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/MessageFactoryDiscovery.php b/src/MessageFactoryDiscovery.php index e4662d5..30a6f5e 100644 --- a/src/MessageFactoryDiscovery.php +++ b/src/MessageFactoryDiscovery.php @@ -23,7 +23,7 @@ public static function find() { try { $messageFactory = static::findOneByType(MessageFactory::class); - + return static::instantiateClass($messageFactory); } catch (DiscoveryFailedException $e) { throw new NotFoundException( From a8d976f3f76eadf4ea6e9a70495d8a6e91d6dc44 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 5 Jul 2016 09:32:40 +0200 Subject: [PATCH 3/3] Added "implements exception" --- src/Exception/ClassinstantiationFailedException.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Exception/ClassinstantiationFailedException.php b/src/Exception/ClassinstantiationFailedException.php index d070cbc..5bb880a 100644 --- a/src/Exception/ClassinstantiationFailedException.php +++ b/src/Exception/ClassinstantiationFailedException.php @@ -2,11 +2,13 @@ namespace Http\Discovery\Exception; +use Http\Discovery\Exception; + /** * Thrown when a class fails to instantiate. * * @author Tobias Nyholm */ -class ClassinstantiationFailedException extends \RuntimeException +class ClassinstantiationFailedException extends \RuntimeException implements Exception { }