|
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
|
|
|
final 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
|
4 |
|
public static function types(): Generator |
|
24
|
|
|
{ |
|
25
|
4 |
|
if (self::$doneScanning && count(self::$types) > 0) { |
|
26
|
2 |
|
yield from self::$types; |
|
27
|
|
|
|
|
28
|
2 |
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
$path = __DIR__ . DIRECTORY_SEPARATOR . 'Type' . DIRECTORY_SEPARATOR; |
|
32
|
2 |
|
$directory = new RecursiveDirectoryIterator($path); |
|
33
|
2 |
|
$directory = new RecursiveIteratorIterator($directory); |
|
34
|
2 |
|
foreach ($directory as $node) { |
|
35
|
2 |
|
$nodePath = $node->getPath() . DIRECTORY_SEPARATOR . $node->getFilename(); |
|
36
|
2 |
|
if (!is_file($nodePath)) { |
|
37
|
2 |
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
$fileName = str_replace('/', '\\', $nodePath); |
|
41
|
2 |
|
$class = __NAMESPACE__ . '\\Type\\' . substr(substr($fileName, strlen($path)), 0, -4); |
|
42
|
2 |
|
if (class_exists($class) && (new ReflectionClass($class))->implementsInterface(Type::class)) { |
|
43
|
2 |
|
$type = new $class(); |
|
44
|
2 |
|
self::$types[$type->scalar()] = $type; |
|
45
|
2 |
|
yield $type; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
self::$doneScanning = true; |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $type |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public static function has(string $type): bool |
|
57
|
|
|
{ |
|
58
|
2 |
|
self::ensureTypes(); |
|
59
|
|
|
|
|
60
|
2 |
|
return isset(self::$types[$type]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $type |
|
65
|
|
|
* @throws Exception |
|
66
|
|
|
* @return Type |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public static function get(string $type): Type |
|
69
|
|
|
{ |
|
70
|
2 |
|
self::ensureTypes(); |
|
71
|
|
|
|
|
72
|
2 |
|
if (isset(self::$types[$type])) { |
|
73
|
1 |
|
return self::$types[$type]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
throw new Exception('Type "' . $type . '" not found, use has to check"'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Reset state. |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public static function reset() |
|
83
|
|
|
{ |
|
84
|
1 |
|
self::$types = []; |
|
85
|
1 |
|
self::$doneScanning = false; |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* A wee bit hacky, but this ensures that when ever `has` or `get` is called before `types` |
|
90
|
|
|
* all types are detected and available for `has` and `get`. |
|
91
|
|
|
*/ |
|
92
|
4 |
|
protected static function ensureTypes() |
|
93
|
|
|
{ |
|
94
|
4 |
|
if (self::$doneScanning && count(self::$types) > 0) { |
|
95
|
3 |
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
foreach (self::types() as $t) { |
|
99
|
|
|
} |
|
100
|
1 |
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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.