|
1
|
|
|
<?php |
|
2
|
|
|
namespace Joesama\Pintu\Consoles; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Arr; |
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Joesama\Pintu\Components\Manager; |
|
8
|
|
|
use Illuminate\Support\ServiceProvider; |
|
9
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
10
|
|
|
|
|
11
|
|
|
class ComponentGenerator extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'pintu |
|
19
|
|
|
{ provider : Fully qualified service provider name } |
|
20
|
|
|
{ --f|force : Force to recreate component file. } |
|
21
|
|
|
'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command description. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $description = 'Define package component & routing'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Execute the console command. |
|
32
|
|
|
* |
|
33
|
|
|
* @return mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
public function handle(Application $app, Filesystem $file) |
|
36
|
|
|
{ |
|
37
|
|
|
$providerOption = $this->argument('provider'); |
|
38
|
|
|
|
|
39
|
|
|
$provider = $this->isServiceProvider($app, $providerOption); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (!$this->fileIsExist($providerOption) |
|
|
|
|
|
|
42
|
|
|
&& |
|
43
|
|
|
($provider === false) ) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$manager = new Manager($provider); |
|
48
|
|
|
|
|
49
|
|
|
$filePath = $manager->getComponentFilePath(); |
|
50
|
|
|
|
|
51
|
|
|
$stub = $file->get($manager->getComponentFileStub()); |
|
52
|
|
|
|
|
53
|
|
|
$this->alreadyExist($file, $filePath); |
|
54
|
|
|
|
|
55
|
|
|
$this->forceTriggered($file, $filePath); |
|
56
|
|
|
|
|
57
|
|
|
if (! $file->isDirectory(dirname($filePath))) { |
|
58
|
|
|
$file->makeDirectory(dirname($filePath), 0655, true, true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$file->put($filePath, $stub); |
|
62
|
|
|
|
|
63
|
|
|
$this->line('Component file successfully created!'); |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Check service provider file exist. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $provider |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
private function fileIsExist(string $provider): bool |
|
75
|
|
|
{ |
|
76
|
|
|
if (\class_exists($provider)) { |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->error("class {$provider} must be exist"); |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Provider passed is registered provider. |
|
87
|
|
|
* |
|
88
|
|
|
* @param [type] $app |
|
|
|
|
|
|
89
|
|
|
* @param string $providerOption |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
private function isServiceProvider($app, string $providerOption) |
|
94
|
|
|
{ |
|
95
|
|
|
$provider = Arr::first($app->getProviders($providerOption)); |
|
96
|
|
|
|
|
97
|
|
|
if ($provider === null) { |
|
98
|
|
|
$this->error("class {$providerOption} must be instance of ". ServiceProvider::class); |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $provider; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Validate if component file already exist. |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
private function alreadyExist($file, $filePath) |
|
112
|
|
|
{ |
|
113
|
|
|
if (! $this->option('force') && $file->exists($filePath)) { |
|
114
|
|
|
$this->error( |
|
115
|
|
|
'Component file already exists!. Please use --force to re-create component file' |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
exit; |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Process if --force indicator is used. |
|
124
|
|
|
* |
|
125
|
|
|
* @param [type] $file |
|
|
|
|
|
|
126
|
|
|
* @param [type] $filePath |
|
127
|
|
|
* |
|
128
|
|
|
* @return void |
|
129
|
|
|
*/ |
|
130
|
|
|
private function forceTriggered($file, $filePath) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->option('force') && $file->exists($filePath)) { |
|
133
|
|
|
$force = $this->choice( |
|
134
|
|
|
'This will replace current component file. Do you wish to continoue?', |
|
135
|
|
|
[ 'Y', "N"] |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
if ($force === 'N') { |
|
139
|
|
|
$this->line('Component file are remained same!!!'); |
|
140
|
|
|
|
|
141
|
|
|
exit; |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$file->copy($filePath, \str_replace('component.php', date('dmyHis') . '.php', $filePath)); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|