|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Copyright (c) 2017 Constantin Galbenu <[email protected]> * |
|
4
|
|
|
******************************************************************************/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Gica\Cqrs\CodeGeneration; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery; |
|
10
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ClassSorter\ByConstructorDependencySorter; |
|
11
|
|
|
use Gica\CodeAnalysis\MethodListenerDiscovery\ReadModelMapperWriter; |
|
12
|
|
|
use Gica\Cqrs\Command\CodeAnalysis\ReadModelEventHandlerDetector; |
|
13
|
|
|
use Gica\Cqrs\ReadModel\ListenerClassValidator\OnlyReadModels; |
|
14
|
|
|
use Gica\FileSystem\FileSystemInterface; |
|
15
|
|
|
use Gica\FileSystem\OperatingSystemFileSystem; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
|
|
18
|
|
View Code Duplication |
class ReadModelsMapCodeGenerator |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
1 |
|
public function generate( |
|
21
|
|
|
LoggerInterface $logger, |
|
22
|
|
|
FileSystemInterface $fileSystem = null, |
|
23
|
|
|
string $readModelMapTemplateClassName, |
|
24
|
|
|
string $searchDirectory, |
|
25
|
|
|
string $outputFilePath, |
|
26
|
|
|
string $outputShortClassName |
|
27
|
|
|
) |
|
28
|
|
|
{ |
|
29
|
1 |
|
$fileSystem = $fileSystem ?? new OperatingSystemFileSystem(); |
|
30
|
|
|
|
|
31
|
1 |
|
$classInfo = new \ReflectionClass($readModelMapTemplateClassName); |
|
32
|
|
|
|
|
33
|
1 |
|
$this->deleteFileIfExists($fileSystem, $outputFilePath); |
|
34
|
|
|
|
|
35
|
1 |
|
$discoverer = new MethodListenerDiscovery( |
|
36
|
1 |
|
new ReadModelEventHandlerDetector(), |
|
37
|
1 |
|
new OnlyReadModels(), |
|
38
|
1 |
|
new ByConstructorDependencySorter() |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
1 |
|
$discoverer->discoverListeners($searchDirectory); |
|
42
|
|
|
|
|
43
|
1 |
|
$map = $discoverer->getAllEventsListeners(); |
|
44
|
|
|
|
|
45
|
1 |
|
$writer = new ReadModelMapperWriter(); |
|
46
|
|
|
|
|
47
|
1 |
|
$template = $fileSystem->fileGetContents($classInfo->getFileName()); |
|
48
|
|
|
|
|
49
|
1 |
|
$template = str_replace($classInfo->getShortName() /*ReadModelMapTemplate*/, $outputShortClassName /* ReadModelMap */, $template); |
|
50
|
|
|
|
|
51
|
1 |
|
$template = str_replace('--- This is just a template ---', '--- generated by ' . __FILE__ . ' at ' . date('c') . ' ---', $template); |
|
52
|
|
|
|
|
53
|
1 |
|
$code = $writer->generateAndGetFileContents($map, $template); |
|
54
|
|
|
|
|
55
|
1 |
|
$fileSystem->filePutContents($outputFilePath, $code); |
|
56
|
|
|
|
|
57
|
1 |
|
$fileSystem->fileSetPermissions($outputFilePath, 0777); |
|
58
|
|
|
|
|
59
|
1 |
|
$logger->info("Read models map wrote to: $outputFilePath (searched in $searchDirectory)"); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
private function deleteFileIfExists(FileSystemInterface $fileSystem, string $outputFilePath) |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
1 |
|
$fileSystem->fileDelete($outputFilePath); |
|
66
|
1 |
|
} catch (\Exception $exception) { |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.