Skip to content

Commit

Permalink
Allow for a candidate to have a Closure as a class parameter (#66)
Browse files Browse the repository at this point in the history
Allow for a candidate to have a Closure as a class parameter
  • Loading branch information
Nyholm authored and dbu committed Jul 5, 2016
1 parent 410465b commit 2ed6663
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
27 changes: 26 additions & 1 deletion src/ClassDiscovery.php
Expand Up @@ -2,6 +2,7 @@

namespace Http\Discovery;

use Http\Discovery\Exception\ClassinstantiationFailedException;
use Http\Discovery\Exception\DiscoveryFailedException;
use Http\Discovery\Exception\StrategyUnavailableException;

Expand Down Expand Up @@ -36,7 +37,7 @@ abstract class ClassDiscovery
*
* @param string $type
*
* @return string
* @return string|\Closure
*
* @throws DiscoveryFailedException
*/
Expand Down Expand Up @@ -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');
}
}
14 changes: 14 additions & 0 deletions src/Exception/ClassinstantiationFailedException.php
@@ -0,0 +1,14 @@
<?php

namespace Http\Discovery\Exception;

use Http\Discovery\Exception;

/**
* Thrown when a class fails to instantiate.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ClassinstantiationFailedException extends \RuntimeException implements Exception
{
}
2 changes: 1 addition & 1 deletion src/HttpAsyncClientDiscovery.php
Expand Up @@ -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".',
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientDiscovery.php
Expand Up @@ -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".',
Expand Down
2 changes: 1 addition & 1 deletion src/MessageFactoryDiscovery.php
Expand Up @@ -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.',
Expand Down
2 changes: 1 addition & 1 deletion src/StreamFactoryDiscovery.php
Expand Up @@ -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.',
Expand Down
2 changes: 1 addition & 1 deletion src/UriFactoryDiscovery.php
Expand Up @@ -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.',
Expand Down

0 comments on commit 2ed6663

Please sign in to comment.