|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "elao/enum" package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Elao |
|
7
|
|
|
* |
|
8
|
|
|
* @author Elao <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Elao\Enum\Bridge\Doctrine\DBAL\Types; |
|
12
|
|
|
|
|
13
|
|
|
use Elao\Enum\Exception\LogicException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @internal |
|
17
|
|
|
*/ |
|
18
|
|
|
class TypesDumper |
|
19
|
|
|
{ |
|
20
|
|
|
const MARKER = 'ELAO_ENUM_DT'; |
|
21
|
|
|
|
|
22
|
|
|
public function dumpToFile(string $file, array $types) |
|
23
|
|
|
{ |
|
24
|
|
|
file_put_contents($file, $this->dump($types)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function dump(array $types): string |
|
28
|
|
|
{ |
|
29
|
|
|
$namespaces = []; |
|
30
|
|
|
foreach ($types as [$enumClass, $type, $name]) { |
|
31
|
|
|
$fqcn = self::getTypeClassname($enumClass); |
|
|
|
|
|
|
32
|
|
|
$classname = basename(str_replace('\\', '/', $fqcn)); |
|
33
|
|
|
$ns = substr($fqcn, 0, -\strlen($classname) - 1); |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($namespaces[$ns])) { |
|
36
|
|
|
$namespaces[$ns] = ''; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$namespaces[$ns] .= $this->getTypeCode($fqcn, $classname, $enumClass, $type, $name); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$code = "<?php\n"; |
|
43
|
|
|
foreach ($namespaces as $namespace => $typeCode) { |
|
44
|
|
|
$code .= <<<PHP |
|
45
|
|
|
|
|
46
|
|
|
namespace $namespace { |
|
47
|
|
|
$typeCode |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
PHP; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $code; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function getTypeCode(string $fqcn, string $classname, string $enumClass, string $type, string $name): string |
|
57
|
|
|
{ |
|
58
|
|
|
switch ($type) { |
|
59
|
|
|
case 'int': |
|
60
|
|
|
$baseClass = AbstractIntegerEnumType::class; |
|
61
|
|
|
break; |
|
62
|
|
|
case 'string': |
|
63
|
|
|
$baseClass = AbstractEnumType::class; |
|
64
|
|
|
break; |
|
65
|
|
|
case 'enum': |
|
66
|
|
|
$baseClass = AbstractEnumSQLDeclarationType::class; |
|
67
|
|
|
break; |
|
68
|
|
|
case 'collection': |
|
69
|
|
|
$baseClass = AbstractEnumCollectionType::class; |
|
70
|
|
|
break; |
|
71
|
|
|
default: |
|
72
|
|
|
throw new LogicException(sprintf('Unexpected type "%s"', $type)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return <<<PHP |
|
76
|
|
|
|
|
77
|
|
|
if (!\class_exists(\\$fqcn::class)) { |
|
78
|
|
|
class $classname extends \\{$baseClass} |
|
79
|
|
|
{ |
|
80
|
|
|
const NAME = '$name'; |
|
81
|
|
|
|
|
82
|
|
|
protected function getEnumClass(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return \\{$enumClass}::class; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getName(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return static::NAME; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
PHP; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function getTypeClassname(string $class): string |
|
98
|
|
|
{ |
|
99
|
|
|
return self::MARKER . "\\{$class}Type"; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.