1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: shanmaseen |
5
|
|
|
* Date: 10/08/19 |
6
|
|
|
* Time: 04:48 م |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Repository\Generator\Commands; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Illuminate\Console\Command; |
13
|
|
|
use Illuminate\Support\Facades\Config; |
14
|
|
|
use Illuminate\Support\Str; |
15
|
|
|
|
16
|
|
|
class Remover extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'remove:repository |
24
|
|
|
{name : Class (singular) for example User} {--only-view}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Remove repository files'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The repository name. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $repoName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new command instance. |
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Execute the console command. |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function handle() |
53
|
|
|
{ |
54
|
|
|
$file = preg_split(' (/|\\\\) ', (string) $this->argument('name')) ?? []; |
55
|
|
|
|
56
|
|
|
if (! $file) { |
57
|
|
|
return 'Something wrong with the inputs !'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->repoName = $file[count($file) - 1]; |
61
|
|
|
|
62
|
|
|
unset($file[count($file) - 1]); |
63
|
|
|
$path = implode('\\', $file); |
64
|
|
|
|
65
|
|
|
if (!$this->confirm('This will delete '.$this->repoName.' files and folder, Do you want to continue ?')) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$model = Str::plural(Config::get('repository.model')); |
70
|
|
|
$interface = Str::plural(Config::get('repository.interface')); |
71
|
|
|
$repository = Str::plural(Config::get('repository.repository')); |
72
|
|
|
$controllerFolder = Config::get('repository.controllers_folder'); |
73
|
|
|
$requestFolder = Config::get('repository.requests_folder'); |
74
|
|
|
|
75
|
|
|
$this->remove('Entity',$model,$path); |
76
|
|
|
$this->remove('Controller',$controllerFolder,$path); |
77
|
|
|
$this->remove('Request',$requestFolder,$path); |
78
|
|
|
$this->remove('Repository',$repository,$path); |
79
|
|
|
$this->remove('Interface',$interface,$path); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function remove($type,$folder,$relativePath) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
|
86
|
|
|
$folder = str_replace('\\', '/', $folder); |
87
|
|
|
$relativePath = str_replace('\\', '/', $relativePath); |
88
|
|
|
|
89
|
|
|
switch ($type) { |
90
|
|
|
case 'Entity': |
91
|
|
|
$filePath = Config::get('repository.app_path')."/{$folder}/{$relativePath}/"; |
92
|
|
|
$fileName = "{$this->repoName}.php"; |
93
|
|
|
break; |
94
|
|
|
case 'Controller': |
95
|
|
|
case 'Request': |
96
|
|
|
case 'Repository': |
97
|
|
|
case 'Interface': |
98
|
|
|
default: |
99
|
|
|
$filePath = Config::get('repository.app_path') . "/{$folder}/{$relativePath}/"; |
100
|
|
|
$fileName = "{$this->repoName}{$type}.php"; |
101
|
|
|
} |
102
|
|
|
if(!is_file($filePath.$fileName)) |
103
|
|
|
{ |
104
|
|
|
$this->warn($filePath.' is not a valid file'); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
unlink($filePath.$fileName); |
109
|
|
|
if(!(new \FilesystemIterator($filePath))->valid()) |
110
|
|
|
{ |
111
|
|
|
rmdir($filePath); |
112
|
|
|
} |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.