1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\LaravelEventSauce\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use EventSauce\EventSourcing\CodeGeneration\CodeDumper; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use Spatie\LaravelEventSauce\Exceptions\CouldNotMakeAggregateRoot; |
11
|
|
|
use Spatie\LaravelEventSauce\Exceptions\InvalidConfiguration; |
12
|
|
|
use EventSauce\EventSourcing\CodeGeneration\YamlDefinitionLoader; |
13
|
|
|
|
14
|
|
|
class MakeAggregateRootCommand extends Command |
15
|
|
|
{ |
16
|
|
|
protected $signature = 'make:aggregate-root {class}'; |
17
|
|
|
|
18
|
|
|
protected $description = 'Create a new aggregate root class'; |
19
|
|
|
|
20
|
|
|
/** @var \Illuminate\Filesystem\Filesystem*/ |
21
|
|
|
protected $files; |
22
|
|
|
|
23
|
|
|
public function __construct(Filesystem $files) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
|
27
|
|
|
$this->files = $files; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function handle() |
31
|
|
|
{ |
32
|
|
|
$aggregateRootFqcn = $this->qualifyClass($this->argument('class')); |
|
|
|
|
33
|
|
|
$aggregateRootPath = $this->getPath($aggregateRootFqcn); |
34
|
|
|
|
35
|
|
|
$aggregateRootRepositoryFqcn = $this->qualifyClass($this->argument('class') . 'Repository'); |
36
|
|
|
$aggregateRootRepositoryPath = $this->getPath($aggregateRootRepositoryFqcn); |
37
|
|
|
|
38
|
|
|
$this->ensureValidPaths([$aggregateRootPath, $aggregateRootRepositoryPath]); |
39
|
|
|
|
40
|
|
|
$this->makeDirectory($aggregateRootPath); |
41
|
|
|
|
42
|
|
|
$replacements = [ |
43
|
|
|
'aggregateRootClass' => class_basename($aggregateRootFqcn), |
44
|
|
|
'namespace' => substr($aggregateRootFqcn, 0, strrpos( $aggregateRootFqcn, '\\')), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$this->files->put($aggregateRootPath, $this->getClassContent('AggregateRoot', $replacements)); |
48
|
|
|
$this->files->put($aggregateRootRepositoryPath, $this->getClassContent('AggregateRootRepository', $replacements)); |
49
|
|
|
|
50
|
|
|
$this->info('Aggregate root created successfully!'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getPath($name) |
54
|
|
|
{ |
55
|
|
|
$name = Str::replaceFirst($this->rootNamespace(), '', $name); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Parse the class name and format according to the root namespace. |
62
|
|
|
* |
63
|
|
|
* @param string $name |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function qualifyClass($name) |
68
|
|
|
{ |
69
|
|
|
$name = ltrim($name, '\\/'); |
70
|
|
|
|
71
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
72
|
|
|
|
73
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
74
|
|
|
return $name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$name = str_replace('/', '\\', $name); |
78
|
|
|
|
79
|
|
|
return $this->qualifyClass(trim($rootNamespace, '\\') . '\\' . $name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function ensureValidPaths(array $paths) |
83
|
|
|
{ |
84
|
|
|
foreach ($paths as $path) { |
85
|
|
|
if (file_exists($path)) { |
86
|
|
|
throw CouldNotMakeAggregateRoot::fileAlreadyExists($path); |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function makeDirectory($path) |
92
|
|
|
{ |
93
|
|
|
if (! $this->files->isDirectory(dirname($path))) { |
94
|
|
|
$this->files->makeDirectory(dirname($path), 0777, true, true); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $path; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function getClassContent(string $stubName, array $replacements) |
101
|
|
|
{ |
102
|
|
|
$content = $this->files->get(__DIR__ . "/stubs/{$stubName}.php.stub"); |
103
|
|
|
|
104
|
|
|
foreach($replacements as $search => $replace) |
105
|
|
|
{ |
106
|
|
|
$content = str_replace("{{ {$search} }}", $replace, $content); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $content; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..