-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakeMapperCommand.php
More file actions
117 lines (89 loc) · 2.88 KB
/
Copy pathMakeMapperCommand.php
File metadata and controls
117 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
class MakeMapperCommand extends Command
{
protected $name = 'make:mapper';
protected $description = 'Create a new mapper class';
protected string $argumentName = 'mapper';
public function __construct()
{
parent::__construct();
}
public function getDefaultNamespace(): string
{
return 'App\\Http\\Mappers';
}
public function getClass(): string
{
return class_basename($this->argument($this->argumentName));
}
public function getClassNamespace(): string
{
$extra = str_replace($this->getClass(), '', $this->argument($this->argumentName));
$extra = str_replace('/', '\\', $extra);
$namespace = $this->getDefaultNamespace() . '\\' . $extra;
return trim(str_replace('/', '\\', $namespace), '\\');
}
public function handle()
{
$path = str_replace('\\', '/', $this->getDestinationFilePath());
$fileContents = $this->getTemplateContents();
$this->createDir($path);
if (File::exists($path)) {
$this->error("File {$path} already exists!");
return 1;
}
File::put($path, $fileContents);
$this->info("Mapper generated successfully! path : {$path}");
return 0;
}
protected function getArguments(): array
{
return [
[$this->argumentName, InputArgument::REQUIRED, 'The name of the mapper class.'],
];
}
protected function getDestinationFilePath(): string
{
return app_path() . '/Http/Mappers/' . $this->getMapperName() . '.php';
}
protected function getStubFilePath(): string
{
return '/stubs/mappers.stub';
}
protected function getTemplateContents(): string
{
$template = file_get_contents(__DIR__ . $this->getStubFilePath());
$replaceOptions = [
'CLASS_NAMESPACE' => $this->getClassNamespace(),
'CLASS' => $this->getMapperNameWithoutNamespace(),
];
foreach ($replaceOptions as $search => $replace) {
$template = str_replace('$' . strtoupper($search) . '$', $replace, $template);
}
return $template;
}
protected function getMapperName(): string
{
$mapper = Str::studly($this->argument($this->argumentName));
if (! Str::endsWith(strtolower($mapper), 'mapper')) {
$mapper .= 'Mapper';
}
return $mapper;
}
protected function getMapperNameWithoutNamespace(): string
{
return class_basename($this->getMapperName());
}
protected function createDir(string $path)
{
$dir = dirname($path);
if (! file_exists($dir)) {
mkdir($dir, 0777, true);
}
}
}