-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerator.php
168 lines (137 loc) · 4.79 KB
/
Generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace DualMedia\DoctrineEventConverterBundle\Proxy;
use DualMedia\DoctrineEventConverterBundle\Event\AbstractEntityEvent;
use DualMedia\DoctrineEventConverterBundle\Exception\Proxy\DirectoryNotWritable;
use DualMedia\DoctrineEventConverterBundle\Exception\Proxy\NotProxyClassException;
use DualMedia\DoctrineEventConverterBundle\Exception\Proxy\TargetClassFinalException;
use DualMedia\DoctrineEventConverterBundle\Exception\Proxy\TargetClassNamingSchemeInvalidException;
/**
* Loosely based on Doctrine's EntityGenerator.
*/
class Generator
{
public const PROXY_NS = 'DualMedia\\DoctrineEventConverterProxy';
public const TEMPLATE = <<<EOF
<?php
namespace <namespace>;
/**
<doc> */
class <class> extends \<parent> <interfaces> {}
EOF;
public const DOC_TEMPLATE = [
'WARNING! Proxy class generated automatically, Do not modify!',
'',
'Event class for event <event>',
'@final',
];
public function __construct(
private readonly string $proxyDirectory,
) {
}
/**
* @template T of AbstractEntityEvent
*
* @param class-string<T> $class
* @param list<string> $interfaces
*
* @return class-string<T>
*
* @throws TargetClassFinalException
* @throws TargetClassNamingSchemeInvalidException
* @throws DirectoryNotWritable
* @throws \ReflectionException
*
* @see AbstractEntityEvent
*/
public function generateProxyClass(
string $class,
string $eventName,
array $interfaces = [],
): string {
$reflection = new \ReflectionClass($class);
if ($reflection->isFinal()) {
throw TargetClassFinalException::new([$class]);
}
$eventName = ucfirst($eventName);
$name = self::splitClassName($reflection->getShortName());
$namespace = self::getNamespace($class);
$parameters = [
'<class>' => $classNew = $name[0].$eventName.$name[1],
'<parent>' => $class,
'<namespace>' => $namespace,
'<interfaces>' => '',
];
if (!empty($interfaces)) {
$parameters['<interfaces>'] = 'implements '.implode(', ', array_map(static fn (string $i) => '\\'.ltrim($i, '\\'), $interfaces));
}
$doc = self::DOC_TEMPLATE;
/**
* @var class-string<T> $fqcn
*
* @noinspection PhpRedundantVariableDocTypeInspection
*/
$fqcn = $namespace.'\\'.$classNew;
$fileName = $this->proxyDirectory.DIRECTORY_SEPARATOR.str_replace('\\', '', mb_substr($fqcn, mb_strlen(self::PROXY_NS))).'.php';
$parameters['<doc>'] = ' *'.implode(' *', array_map(static fn (string $s) => mb_strlen($s) ? ' '.$s."\n" : "\n", $doc));
$parameters['<doc>'] = str_replace('<event>', $eventName, $parameters['<doc>']);
$parentDirectory = dirname($fileName);
if (!is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) {
throw new DirectoryNotWritable($this->proxyDirectory);
}
if (!is_writable($parentDirectory)) {
throw new DirectoryNotWritable($this->proxyDirectory);
}
$proxy = strtr(self::TEMPLATE, $parameters);
file_put_contents($fileName, $proxy);
@chmod($fileName, 0664);
return $fqcn;
}
/**
* @throws TargetClassNamingSchemeInvalidException
*/
public static function getProxyFqcn(
string $class,
string $eventName,
): string {
$exploded = explode('\\', $class);
end($exploded);
$name = self::splitClassName(current($exploded));
$namespace = self::getNamespace($class);
return $namespace.'\\'.$name[0].ucfirst($eventName).$name[1];
}
/**
* @return string[]
*
* @throws TargetClassNamingSchemeInvalidException
*/
public static function splitClassName(
string $classShort,
): array {
if (false === ($pos = mb_strpos($classShort, 'Event'))) {
throw TargetClassNamingSchemeInvalidException::new([$classShort]);
}
return [
mb_substr($classShort, 0, $pos),
'Event',
];
}
/**
* @throws NotProxyClassException
*/
public function resolveFilePath(
string $class,
): string {
if (!str_starts_with($class, self::PROXY_NS)) {
throw NotProxyClassException::new([$class]);
}
$classRelative = mb_substr($class, mb_strlen(self::PROXY_NS));
return $this->proxyDirectory.DIRECTORY_SEPARATOR.str_replace('\\', '', $classRelative).'.php';
}
public static function getNamespace(
string $class,
): string {
$exploded = explode('\\', $class);
array_pop($exploded);
return self::PROXY_NS.'\\'.implode('\\', $exploded);
}
}