1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceTestUtilities; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Generator; |
7
|
|
|
use RecursiveDirectoryIterator; |
8
|
|
|
use RecursiveIteratorIterator; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
class Types |
12
|
|
|
{ |
13
|
|
|
protected static $types = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected static $doneScanning = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return Generator<Type> |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public static function types(): Generator |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
if (self::$doneScanning && count(self::$types) > 0) { |
26
|
|
|
yield from self::$types; |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$path = __DIR__ . DIRECTORY_SEPARATOR . 'Type' . DIRECTORY_SEPARATOR; |
31
|
|
|
$directory = new RecursiveDirectoryIterator($path); |
32
|
|
|
$directory = new RecursiveIteratorIterator($directory); |
33
|
|
|
foreach ($directory as $node) { |
34
|
|
|
$nodePath = $node->getPath() . DIRECTORY_SEPARATOR . $node->getFilename(); |
35
|
|
|
if (!is_file($nodePath)) { |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$fileName = str_replace('/', '\\', $nodePath); |
40
|
|
|
$class = __NAMESPACE__ . '\\Type\\' . substr(substr($fileName, strlen($path)), 0, -4); |
41
|
|
|
if (class_exists($class) && (new ReflectionClass($class))->implementsInterface(Type::class)) { |
42
|
|
|
$type = new $class; |
43
|
|
|
self::$types[$type->scalar()] = $type; |
44
|
|
|
yield $type; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
self::$doneScanning = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $type |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public static function has(string $type): bool |
56
|
|
|
{ |
57
|
|
|
self::ensureTypes(); |
58
|
|
|
|
59
|
|
|
return isset(self::$types[$type]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $type |
64
|
|
|
* @return Type |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public static function get(string $type): Type |
68
|
|
|
{ |
69
|
|
|
self::ensureTypes(); |
70
|
|
|
|
71
|
|
|
if (isset(self::$types[$type])) { |
72
|
|
|
return self::$types[$type]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new Exception('Type "' . $type . '" not found, use has to check"'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* A wee bit hacky, but this ensures that when ever `has` or `get` is called before `types` |
80
|
|
|
* all types are detected and available for `has` and `get`. |
81
|
|
|
*/ |
82
|
|
|
protected function ensureTypes() |
83
|
|
|
{ |
84
|
|
|
if (self::$doneScanning && count(self::$types) > 0) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach (self::types() as $t) { |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function reset() |
93
|
|
|
{ |
94
|
|
|
self::$types = []; |
95
|
|
|
self::$doneScanning = false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.