Skip to content

Commit 2ed6663

Browse files
Nyholmdbu
authored andcommitted
Allow for a candidate to have a Closure as a class parameter (#66)
Allow for a candidate to have a Closure as a class parameter
1 parent 410465b commit 2ed6663

7 files changed

+45
-6
lines changed

src/ClassDiscovery.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\Discovery;
44

5+
use Http\Discovery\Exception\ClassinstantiationFailedException;
56
use Http\Discovery\Exception\DiscoveryFailedException;
67
use Http\Discovery\Exception\StrategyUnavailableException;
78

@@ -36,7 +37,7 @@ abstract class ClassDiscovery
3637
*
3738
* @param string $type
3839
*
39-
* @return string
40+
* @return string|\Closure
4041
*
4142
* @throws DiscoveryFailedException
4243
*/
@@ -177,4 +178,28 @@ protected static function evaluateCondition($condition)
177178

178179
return false;
179180
}
181+
182+
/**
183+
* Get an instance of the $class.
184+
*
185+
* @param string|\Closure $class A FQN of a class or a closure that instantiate the class.
186+
*
187+
* @return object
188+
*/
189+
protected static function instantiateClass($class)
190+
{
191+
try {
192+
if (is_string($class)) {
193+
return new $class();
194+
}
195+
196+
if (is_callable($class)) {
197+
return $class();
198+
}
199+
} catch (\Exception $e) {
200+
throw new ClassinstantiationFailedException('Unexcepced exception when instantiating class.', 0, $e);
201+
}
202+
203+
throw new ClassinstantiationFailedException('Could not instantiate class becuase parameter is neitehr a callable or a string');
204+
}
180205
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Http\Discovery\Exception;
4+
5+
use Http\Discovery\Exception;
6+
7+
/**
8+
* Thrown when a class fails to instantiate.
9+
*
10+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class ClassinstantiationFailedException extends \RuntimeException implements Exception
13+
{
14+
}

src/HttpAsyncClientDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function find()
2424
try {
2525
$asyncClient = static::findOneByType(HttpAsyncClient::class);
2626

27-
return new $asyncClient();
27+
return static::instantiateClass($asyncClient);
2828
} catch (DiscoveryFailedException $e) {
2929
throw new NotFoundException(
3030
'No HTTPlug async clients found. Make sure to install a package providing "php-http/async-client-implementation". Example: "php-http/guzzle6-adapter".',

src/HttpClientDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function find()
2424
try {
2525
$client = static::findOneByType(HttpClient::class);
2626

27-
return new $client();
27+
return static::instantiateClass($client);
2828
} catch (DiscoveryFailedException $e) {
2929
throw new NotFoundException(
3030
'No HTTPlug clients found. Make sure to install a package providing "php-http/client-implementation". Example: "php-http/guzzle6-adapter".',

src/MessageFactoryDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function find()
2424
try {
2525
$messageFactory = static::findOneByType(MessageFactory::class);
2626

27-
return new $messageFactory();
27+
return static::instantiateClass($messageFactory);
2828
} catch (DiscoveryFailedException $e) {
2929
throw new NotFoundException(
3030
'No message factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.',

src/StreamFactoryDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function find()
2424
try {
2525
$streamFactory = static::findOneByType(StreamFactory::class);
2626

27-
return new $streamFactory();
27+
return static::instantiateClass($streamFactory);
2828
} catch (DiscoveryFailedException $e) {
2929
throw new NotFoundException(
3030
'No stream factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.',

src/UriFactoryDiscovery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function find()
2424
try {
2525
$uriFactory = static::findOneByType(UriFactory::class);
2626

27-
return new $uriFactory();
27+
return static::instantiateClass($uriFactory);
2828
} catch (DiscoveryFailedException $e) {
2929
throw new NotFoundException(
3030
'No uri factories found. To use Guzzle or Diactoros factories install php-http/message and the chosen message implementation.',

0 commit comments

Comments
 (0)