Skip to content

Commit 5ad3460

Browse files
committed
Allow for a candidate to have a Closure as a class parameter
1 parent 664b05c commit 5ad3460

7 files changed

+44
-7
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
*/
@@ -153,4 +154,28 @@ protected static function evaluateCondition($condition)
153154

154155
return false;
155156
}
157+
158+
/**
159+
* Get an instance of the $class.
160+
*
161+
* @param string|\Closure $class A FQN of a class or a closure that instantiate the class.
162+
*
163+
* @return object
164+
*/
165+
protected static function instantiateClass($class)
166+
{
167+
try {
168+
if (is_string($class)) {
169+
return new $class();
170+
}
171+
172+
if (is_callable($class)) {
173+
return $class();
174+
}
175+
} catch (\Exception $e) {
176+
throw new ClassinstantiationFailedException('Unexcepced exception when instantiating class.', 0, $e);
177+
}
178+
179+
throw new ClassinstantiationFailedException('Could not instantiate class becuase parameter is neitehr a callable or a string');
180+
}
156181
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Http\Discovery\Exception;
4+
5+
/**
6+
* Thrown when a class fails to instantiate.
7+
*
8+
* @author Tobias Nyholm <[email protected]>
9+
*/
10+
class ClassinstantiationFailedException extends \RuntimeException
11+
{
12+
}

src/HttpAsyncClientDiscovery.php

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

28-
return new $asyncClient();
28+
return static::instantiateClass($asyncClient);
2929
} catch (DiscoveryFailedException $e) {
3030
throw new NotFoundException(
3131
'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
@@ -25,7 +25,7 @@ public static function find()
2525
try {
2626
$client = static::findOneByType(HttpClient::class);
2727

28-
return new $client();
28+
return static::instantiateClass($client);
2929
} catch (DiscoveryFailedException $e) {
3030
throw new NotFoundException(
3131
'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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public static function find()
2424
{
2525
try {
2626
$messageFactory = static::findOneByType(MessageFactory::class);
27-
28-
return new $messageFactory();
27+
28+
return static::instantiateClass($messageFactory);
2929
} catch (DiscoveryFailedException $e) {
3030
throw new NotFoundException(
3131
'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
@@ -25,7 +25,7 @@ public static function find()
2525
try {
2626
$streamFactory = static::findOneByType(StreamFactory::class);
2727

28-
return new $streamFactory();
28+
return static::instantiateClass($streamFactory);
2929
} catch (DiscoveryFailedException $e) {
3030
throw new NotFoundException(
3131
'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
@@ -25,7 +25,7 @@ public static function find()
2525
try {
2626
$uriFactory = static::findOneByType(UriFactory::class);
2727

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

0 commit comments

Comments
 (0)