|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eloquentFilter\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class MakeEloquentFilter. |
|
11
|
|
|
*/ |
|
12
|
|
|
class MakeEloquentFilter extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The name and signature of the console command. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $signature = 'eloquentFilter:filter {name}'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The console command description. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $description = ' |
|
27
|
|
|
███████╗██╗░░░░░░█████╗░░██████╗░██╗░░░██╗███████╗███╗░░██╗████████╗ ███████╗██╗██╗░░░░░████████╗███████╗██████╗░ |
|
28
|
|
|
██╔════╝██║░░░░░██╔══██╗██╔═══██╗██║░░░██║██╔════╝████╗░██║╚══██╔══╝ ██╔════╝██║██║░░░░░╚══██╔══╝██╔════╝██╔══██╗ |
|
29
|
|
|
█████╗░░██║░░░░░██║░░██║██║██╗██║██║░░░██║█████╗░░██╔██╗██║░░░██║░░░ █████╗░░██║██║░░░░░░░░██║░░░█████╗░░██████╔╝ |
|
30
|
|
|
██╔══╝░░██║░░░░░██║░░██║╚██████╔╝██║░░░██║██╔══╝░░██║╚████║░░░██║░░░ ██╔══╝░░██║██║░░░░░░░░██║░░░██╔══╝░░██╔══██╗ |
|
31
|
|
|
███████╗███████╗╚█████╔╝░╚═██╔═╝░╚██████╔╝███████╗██║░╚███║░░░██║░░░ ██║░░░░░██║███████╗░░░██║░░░███████╗██║░░██║ |
|
32
|
|
|
╚══════╝╚══════╝░╚════╝░░░░╚═╝░░░░╚═════╝░╚══════╝╚═╝░░╚══╝░░░╚═╝░░░ ╚═╝░░░░░╚═╝╚══════╝░░░╚═╝░░░╚══════╝╚═╝░░╚═╝ |
|
33
|
|
|
|
|
34
|
|
|
Create A New Eloquent Custom Model Filter'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class to create. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array|string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $class; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The filesystem instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @var Filesystem |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $files; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* MakeEloquentFilter constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Filesystem $files |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(Filesystem $files) |
|
56
|
|
|
{ |
|
57
|
|
|
parent::__construct(); |
|
58
|
|
|
|
|
59
|
|
|
$this->files = $files; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Execute the console command. |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
66
|
|
|
* |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function handle() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->makeClassName()->compileStub(); |
|
72
|
|
|
$this->info(class_basename($this->getClassName()).' Created Successfully!'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function compileStub() |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->files->exists($path = $this->getPath())) { |
|
81
|
|
|
$this->error("\n\n\t".$path.' Already Exists!'."\n"); |
|
82
|
|
|
exit; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
$this->makeDirectory($path); |
|
85
|
|
|
|
|
86
|
|
|
$stubPath = __DIR__.'/modelfilter.stub'; |
|
87
|
|
|
|
|
88
|
|
|
if (!$this->files->exists($stubPath) || !is_readable($stubPath)) { |
|
89
|
|
|
$this->error(sprintf('File "%s" does not exist or is unreadable.', $stubPath)); |
|
90
|
|
|
exit; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$tmp = $this->applyValuesToStub($this->files->get($stubPath)); |
|
94
|
|
|
$this->files->put($path, $tmp); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $stub |
|
99
|
|
|
* |
|
100
|
|
|
* @return string|string[] |
|
101
|
|
|
*/ |
|
102
|
|
|
public function applyValuesToStub($stub) |
|
103
|
|
|
{ |
|
104
|
|
|
$className = $this->getClassBasename($this->getClassName()); |
|
105
|
|
|
$search = ['{{class}}', '{{namespace}}']; |
|
106
|
|
|
$replace = [$className, str_replace('\\'.$className, '', $this->getClassName())]; |
|
107
|
|
|
|
|
108
|
|
|
return str_replace($search, $replace, $stub); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $class |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
private function getClassBasename($class) |
|
117
|
|
|
{ |
|
118
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
|
119
|
|
|
|
|
120
|
|
|
return basename(str_replace('\\', '/', $class)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getPath() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->laravel->path.DIRECTORY_SEPARATOR.$this->getFileName(); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string|string[] |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getFileName() |
|
135
|
|
|
{ |
|
136
|
|
|
return str_replace([$this->getAppNamespace(), '\\'], ['', DIRECTORY_SEPARATOR], $this->getClassName().'.php'); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getAppNamespace() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->laravel->getNamespace(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Build the directory for the class if necessary. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $path |
|
151
|
|
|
* |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function makeDirectory(string $path) |
|
154
|
|
|
{ |
|
155
|
|
|
if (!$this->files->isDirectory(dirname($path))) { |
|
156
|
|
|
$this->files->makeDirectory(dirname($path), 0777, true, true); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Create Filter Class Name. |
|
162
|
|
|
* |
|
163
|
|
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
|
|
public function makeClassName() |
|
166
|
|
|
{ |
|
167
|
|
|
$parts = array_map([Str::class, 'studly'], explode('\\', $this->argument('name'))); |
|
168
|
|
|
$className = array_pop($parts); |
|
169
|
|
|
$ns = count($parts) > 0 ? implode('\\', $parts).'\\' : ''; |
|
170
|
|
|
|
|
171
|
|
|
$fqClass = config('eloquentFilter.namespace', 'App\\ModelFilters\\').$ns.$className; |
|
172
|
|
|
|
|
173
|
|
|
if (substr($fqClass, -6, 6) !== 'Filter') { |
|
174
|
|
|
$fqClass .= 'Filter'; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (class_exists($fqClass)) { |
|
178
|
|
|
$this->error("\n\n\t$fqClass Already Exists!\n"); |
|
179
|
|
|
exit; |
|
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$this->setClassName($fqClass); |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param $name |
|
189
|
|
|
* |
|
190
|
|
|
* @return $this |
|
191
|
|
|
*/ |
|
192
|
|
|
public function setClassName($name) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->class = $name; |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return array|string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getClassName() |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->class; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.