-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandGenerator.php
More file actions
183 lines (157 loc) · 3.97 KB
/
Copy pathCommandGenerator.php
File metadata and controls
183 lines (157 loc) · 3.97 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Pluralizer;
use Illuminate\Support\Str;
class CommandGenerator extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'commnand:name {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* the stub file path
*
* @var string
*/
protected $stubFile;
/**
* the class namespace
*
* @var string
*/
protected $namespace;
/**
* the path of generate file
*
* @var string
*/
protected $pathGenerateFile;
/**
* Filesystem instance
* @var Filesystem
*/
protected $files;
/**
* Create a new command instance.
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/**
* Execute the console command.
*/
public function handle()
{
if (!$this->stubFile) {
$this->info('the stubFile property must be a value');
return Command::FAILURE;
}
if (!$this->namespace) {
$this->info('the namespace property must be a value');
return Command::FAILURE;
}
if (!$this->pathGenerateFile) {
$this->info('the pathGenerateFile property must be a value');
return Command::FAILURE;
}
$path = $this->getSourceFilePath();
$this->makeDirectory(dirname($path));
if (!$this->files->exists($path)) {
$this->files->put($path, $this->getSourceFile());
$this->info("File : {$path} created");
} else {
$this->info("File : {$path} already exits");
}
return Command::SUCCESS;
}
/**
* Return the stub file path
*
* @return string
*/
public function getStubPath()
{
return __DIR__ . "/../stubs/{$this->stubFile}.stub";
}
/**
* Map the stub variables present in stub to its value
*
* @return array
*/
public function getStubVariables()
{
return [
'namespace' => $this->namespace, // exp: App\\Enums
'class' => $this->getSingularClassName($this->argument('name')),
];
}
/**
* Get the stub path and the stub variables
*
* @return bool|mixed|string
*
*/
public function getSourceFile()
{
return $this->getStubContents($this->getStubPath(), $this->getStubVariables());
}
/**
* Replace the stub variables(key) with the desire value
*
* @param $stub
* @param array $stubVariables
* @return bool|mixed|string
*/
public function getStubContents($stub , $stubVariables = [])
{
$contents = file_get_contents($stub);
foreach ($stubVariables as $search => $replace){
$contents = str_replace('{{ '.$search.' }}' , $replace, $contents);
}
return $contents;
}
/**
* Get the full path of generate class
*
* @return string
*/
public function getSourceFilePath()
{
return base_path($this->pathGenerateFile) . '/' . $this->getSingularClassName($this->argument('name')) . '.php';
}
/**
* Return the Singular Capitalize Name
* @param $name
* @return string
*/
public function getSingularClassName($name)
{
return ucwords(Pluralizer::singular($name));
}
/**
* Build the directory for the class if necessary.
*
* @param string $path
* @return string
*/
protected function makeDirectory($path)
{
if (! $this->files->isDirectory($path)) {
$this->files->makeDirectory($path, 0755, true, true);
}
return $path;
}
}