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
|
34 |
|
public function __construct(Filesystem $filesystem, Composer $composer) |
39
|
|
|
{ |
40
|
34 |
|
parent::__construct(); |
41
|
|
|
|
42
|
34 |
|
$this->filesystem = $filesystem; |
43
|
34 |
|
$this->composer = $composer; |
44
|
34 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the console command. |
48
|
|
|
*/ |
49
|
2 |
|
public function handle() |
50
|
|
|
{ |
51
|
2 |
|
$mapping = $this->formatMappingName((string)$this->argument('mapping')); |
52
|
|
|
|
53
|
2 |
|
$model = $this->option('model') ? $this->formatModelName($this->option('model')) : null; |
54
|
|
|
|
55
|
2 |
|
$this->filesystem->put( |
56
|
2 |
|
$this->buildMappingFilePath($mapping), |
57
|
2 |
|
$this->buildMapping( |
58
|
2 |
|
$this->getDefaultStub(), |
59
|
2 |
|
$mapping, |
60
|
2 |
|
$model |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
|
64
|
2 |
|
$this->composer->dumpAutoloads(); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
private function formatMappingName(string $mapping) : string |
68
|
|
|
{ |
69
|
2 |
|
return trim(Str::studly($mapping)); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
private function formatModelName(string $model) : string |
73
|
|
|
{ |
74
|
1 |
|
return trim($model); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
private function getDefaultStub() : string |
78
|
|
|
{ |
79
|
2 |
|
return $this->filesystem->get(__DIR__ . DIRECTORY_SEPARATOR . 'default.stub'); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
private function buildMappingFilePath(string $mapping) : string |
83
|
|
|
{ |
84
|
2 |
|
return $this->getMappingPath() . DIRECTORY_SEPARATOR . $mapping . '.php'; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
private function buildMapping(string $stub, string $mapping, ?string $model) : string |
88
|
|
|
{ |
89
|
2 |
|
$stub = str_replace('DefaultClass', $mapping, $stub); |
90
|
|
|
|
91
|
2 |
|
if ($model) { |
92
|
1 |
|
$stub = str_replace('DefaultModel', $model, $stub); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return $stub; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|