|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Copyright (c) 2017 Constantin Galbenu <[email protected]> * |
|
4
|
|
|
******************************************************************************/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Gica\Cqrs\CodeGeneration; |
|
7
|
|
|
|
|
8
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery; |
|
9
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter\ByConstructorDependencySorter; |
|
10
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ListenerClassValidator\AnyPhpClassIsAccepted; |
|
11
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\MethodListenerMapperWriter; |
|
12
|
|
|
use Gica\Cqrs\Command\CodeAnalysis\AggregateCommandHandlerDetector; |
|
13
|
|
|
use Gica\FileSystem\FileSystemInterface; |
|
14
|
|
|
use Gica\FileSystem\OperatingSystemFileSystem; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class CommandHandlersMapCodeGenerator |
|
18
|
|
|
{ |
|
19
|
2 |
|
public function generate( |
|
20
|
|
|
LoggerInterface $logger, |
|
21
|
|
|
FileSystemInterface $fileSystem = null, |
|
22
|
|
|
string $commandSubscriberTemplateClassName, |
|
23
|
|
|
string $searchDirectory, |
|
24
|
|
|
string $outputFilePath, |
|
25
|
|
|
string $outputShortClassName = 'CommandHandlerSubscriber') |
|
26
|
|
|
{ |
|
27
|
2 |
|
$fileSystem = $fileSystem ?? new OperatingSystemFileSystem(); |
|
28
|
|
|
|
|
29
|
2 |
|
$classInfo = new \ReflectionClass($commandSubscriberTemplateClassName); |
|
30
|
|
|
|
|
31
|
2 |
|
$classInfo->getShortName(); |
|
32
|
|
|
|
|
33
|
2 |
|
$this->deleteFileIfExists($fileSystem, $outputFilePath); |
|
34
|
|
|
|
|
35
|
2 |
|
$discoverer = new MethodListenerDiscovery( |
|
36
|
2 |
|
new AggregateCommandHandlerDetector(), |
|
37
|
2 |
|
new AnyPhpClassIsAccepted, |
|
38
|
2 |
|
new ByConstructorDependencySorter()); |
|
39
|
|
|
|
|
40
|
2 |
|
$discoverer->discoverListeners($searchDirectory); |
|
41
|
|
|
|
|
42
|
2 |
|
$map = $discoverer->getEventToListenerMap(); |
|
43
|
|
|
|
|
44
|
2 |
|
$this->validateMap($map); |
|
45
|
|
|
|
|
46
|
1 |
|
$writer = new MethodListenerMapperWriter(); |
|
47
|
|
|
|
|
48
|
1 |
|
$template = file_get_contents($classInfo->getFileName()); |
|
49
|
|
|
|
|
50
|
1 |
|
$template = str_replace($classInfo->getShortName() /*CommandSubscriberTemplate*/, $outputShortClassName /*CommandHandlerSubscriber*/, $template); |
|
51
|
|
|
|
|
52
|
1 |
|
$template = str_replace('--- This is just a template ---', '--- generated by ' . __FILE__ . ' at ' . date('c') . ' ---', $template); |
|
53
|
|
|
|
|
54
|
1 |
|
$code = $writer->generateAndGetFileContents($map, $template); |
|
55
|
|
|
|
|
56
|
1 |
|
$fileSystem->filePutContents($outputFilePath, $code); |
|
57
|
|
|
|
|
58
|
1 |
|
$fileSystem->fileSetPermissions($outputFilePath, 0777); |
|
59
|
|
|
|
|
60
|
1 |
|
$logger->info("Commands map wrote to: $outputFilePath (searched in $searchDirectory)"); |
|
61
|
|
|
|
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
private function deleteFileIfExists(FileSystemInterface $fileSystem, string $outputFilePath) |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
2 |
|
$fileSystem->fileDelete($outputFilePath); |
|
68
|
2 |
|
} catch (\Exception $exception) { |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
private function validateMap(array $map) |
|
73
|
|
|
{ |
|
74
|
2 |
|
foreach ($map as $command => $commandHandlers) { |
|
75
|
2 |
|
if (count($commandHandlers) > 1) { |
|
76
|
1 |
|
throw new \Exception( |
|
77
|
2 |
|
sprintf("multiple handlers exists for command %s", $command)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |