|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Murtukov\PHPCodeGenerator; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Static config to be used by all generator components. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Config |
|
11
|
|
|
{ |
|
12
|
|
|
public static string $indent = ' '; |
|
13
|
|
|
public static bool $shortenQualifiers = true; |
|
14
|
|
|
public static bool $manageUseStatements = true; |
|
15
|
|
|
public static string $suppressSymbol = '@'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ConverterInterface[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private static array $customStringifiers = [ |
|
21
|
|
|
// e.g.: 'App\Stringifiers\ExpressionStringifier' => object, |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* A map of FCQNs and their types registered as custom stringifiers. |
|
26
|
|
|
*/ |
|
27
|
|
|
private static array $customStringifiersTypeMap = [ |
|
28
|
|
|
// e.g.: 'string' => [App\Stringifiers\ExpressionStringifier, App\Stringifiers\AnotherStringifier], |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Registers user defined stringifiers. |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public static function registerConverter(ConverterInterface $converter, string $type) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$fqcn = get_class($converter); |
|
37
|
|
|
|
|
38
|
1 |
|
self::$customStringifiers[$fqcn] = $converter; |
|
39
|
1 |
|
self::$customStringifiersTypeMap[$type][] = $fqcn; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Unregister a previously registered custom stringifier. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $fqcn - Fully qualified class name |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public static function unregisterConverter(string $fqcn) |
|
48
|
|
|
{ |
|
49
|
|
|
// Remove instance |
|
50
|
1 |
|
unset(self::$customStringifiers[$fqcn]); |
|
51
|
|
|
// Remove map entry |
|
52
|
1 |
|
$type = array_search($fqcn, self::$customStringifiersTypeMap); |
|
53
|
1 |
|
unset(self::$customStringifiersTypeMap[$type]); |
|
54
|
1 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns an instance of registered custom stringifier. |
|
58
|
|
|
* |
|
59
|
|
|
* @return ConverterInterface|null |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public static function getConverter(string $fqcn): ?object |
|
62
|
|
|
{ |
|
63
|
1 |
|
return self::$customStringifiers[$fqcn] ?? null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public static function getConverterClasses(string $type): ?array |
|
67
|
|
|
{ |
|
68
|
1 |
|
return self::$customStringifiersTypeMap[$type] ?? []; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|