1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Triadev\Leopard\Console\Commands\Mapping; |
|
|
|
|
3
|
|
|
|
4
|
|
|
use Illuminate\Console\ConfirmableTrait; |
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Composer; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
class Make extends BaseCommand |
|
|
|
|
10
|
|
|
{ |
|
|
|
|
11
|
|
|
use ConfirmableTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'triadev:mapping:make {mapping} {--model=}'; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Make a new mapping'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** @var Filesystem */ |
|
|
|
|
28
|
|
|
private $filesystem; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** @var Composer */ |
|
|
|
|
31
|
|
|
private $composer; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Make constructor. |
35
|
|
|
* @param Filesystem $filesystem |
|
|
|
|
36
|
|
|
* @param Composer $composer |
|
|
|
|
37
|
|
|
*/ |
38
|
132 |
|
public function __construct(Filesystem $filesystem, Composer $composer) |
|
|
|
|
39
|
|
|
{ |
40
|
132 |
|
parent::__construct(); |
41
|
|
|
|
42
|
132 |
|
$this->filesystem = $filesystem; |
43
|
132 |
|
$this->composer = $composer; |
|
|
|
|
44
|
132 |
|
} |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the console command. |
48
|
|
|
*/ |
|
|
|
|
49
|
2 |
|
public function handle() |
50
|
|
|
{ |
51
|
2 |
|
$mapping = $this->formatMappingName($this->argument('mapping')); |
|
|
|
|
52
|
|
|
|
53
|
2 |
|
$model = $this->option('model') ? |
|
|
|
|
54
|
1 |
|
$this->formatModelName($this->option('model')) : |
|
|
|
|
55
|
2 |
|
null; |
56
|
|
|
|
57
|
2 |
|
$this->filesystem->put( |
58
|
2 |
|
$this->buildMappingFilePath($mapping), |
59
|
2 |
|
$this->buildMapping( |
60
|
2 |
|
$this->getDefaultStub(), |
61
|
2 |
|
$mapping, |
62
|
2 |
|
$model |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
2 |
|
$this->composer->dumpAutoloads(); |
67
|
2 |
|
} |
|
|
|
|
68
|
|
|
|
69
|
2 |
|
private function formatMappingName(string $mapping) : string |
|
|
|
|
70
|
|
|
{ |
71
|
2 |
|
return trim(Str::studly($mapping)); |
72
|
|
|
} |
|
|
|
|
73
|
|
|
|
74
|
1 |
|
private function formatModelName(string $model) : string |
|
|
|
|
75
|
|
|
{ |
76
|
1 |
|
return trim($model); |
77
|
|
|
} |
|
|
|
|
78
|
|
|
|
79
|
2 |
|
private function getDefaultStub() : string |
|
|
|
|
80
|
|
|
{ |
81
|
2 |
|
return $this->filesystem->get(__DIR__ . DIRECTORY_SEPARATOR . 'default.stub'); |
|
|
|
|
82
|
|
|
} |
|
|
|
|
83
|
|
|
|
84
|
2 |
|
private function buildMappingFilePath(string $mapping) : string |
|
|
|
|
85
|
|
|
{ |
86
|
2 |
|
return $this->getMappingPath() . DIRECTORY_SEPARATOR . $mapping . '.php'; |
|
|
|
|
87
|
|
|
} |
|
|
|
|
88
|
|
|
|
89
|
2 |
|
private function buildMapping(string $stub, string $mapping, ?string $model) : string |
|
|
|
|
90
|
|
|
{ |
91
|
2 |
|
$stub = str_replace('DefaultClass', $mapping, $stub); |
92
|
|
|
|
93
|
2 |
|
if ($model) { |
94
|
1 |
|
$stub = str_replace('DefaultModel', $model, $stub); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return $stub; |
98
|
|
|
} |
|
|
|
|
99
|
|
|
} |
|
|
|
|
100
|
|
|
|