| 1 |
<?php |
| 2 |
|
| 3 |
namespace Http\Discovery\Strategy; |
| 4 |
|
| 5 |
use Http\Discovery\ClassDiscovery; |
| 6 |
use Http\Discovery\Exception\PuliUnavailableException; |
| 7 |
use Puli\Discovery\Api\Discovery; |
| 8 |
use Puli\GeneratedPuliFactory; |
| 9 |
|
| 10 |
/** |
| 11 |
* Find candidates using Puli. |
| 12 |
* |
| 13 |
* @internal |
| 14 |
* |
| 15 |
* @final |
| 16 |
* |
| 17 |
* @author David de Boer <david@ddeboer.nl> |
| 18 |
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 19 |
*/ |
| 20 |
class PuliBetaStrategy implements DiscoveryStrategy |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var GeneratedPuliFactory |
| 24 |
*/ |
| 25 |
protected static $puliFactory; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var Discovery |
| 29 |
*/ |
| 30 |
protected static $puliDiscovery; |
| 31 |
|
| 32 |
/** |
| 33 |
* @return GeneratedPuliFactory |
| 34 |
* |
| 35 |
* @throws PuliUnavailableException |
| 36 |
*/ |
| 37 |
private static function getPuliFactory() |
| 38 |
{ |
| 39 |
if (null === self::$puliFactory) { |
| 40 |
if (!defined('PULI_FACTORY_CLASS')) { |
| 41 |
throw new PuliUnavailableException('Puli Factory is not available'); |
| 42 |
} |
| 43 |
|
| 44 |
$puliFactoryClass = PULI_FACTORY_CLASS; |
| 45 |
|
| 46 |
if (!ClassDiscovery::safeClassExists($puliFactoryClass)) { |
| 47 |
throw new PuliUnavailableException('Puli Factory class does not exist'); |
| 48 |
} |
| 49 |
|
| 50 |
self::$puliFactory = new $puliFactoryClass(); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$puliFactory; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the Puli discovery layer. |
| 58 |
* |
| 59 |
* @return Discovery |
| 60 |
* |
| 61 |
* @throws PuliUnavailableException |
| 62 |
*/ |
| 63 |
private static function getPuliDiscovery() |
| 64 |
{ |
| 65 |
if (!isset(self::$puliDiscovery)) { |
| 66 |
$factory = self::getPuliFactory(); |
| 67 |
$repository = $factory->createRepository(); |
| 68 |
|
| 69 |
self::$puliDiscovery = $factory->createDiscovery($repository); |
| 70 |
} |
| 71 |
|
| 72 |
return self::$puliDiscovery; |
| 73 |
} |
| 74 |
|
| 75 |
public static function getCandidates($type) |
| 76 |
{ |
| 77 |
$returnData = []; |
| 78 |
$bindings = self::getPuliDiscovery()->findBindings($type); |
| 79 |
|
| 80 |
foreach ($bindings as $binding) { |
| 81 |
$condition = true; |
| 82 |
if ($binding->hasParameterValue('depends')) { |
| 83 |
$condition = $binding->getParameterValue('depends'); |
| 84 |
} |
| 85 |
$returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
| 86 |
} |
| 87 |
|
| 88 |
return $returnData; |
| 89 |
} |
| 90 |
} |
| 91 |
|