|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shamaseen\Repository\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use FilesystemIterator; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Shamaseen\Generator\Ungenerator; |
|
9
|
|
|
use Shamaseen\Repository\Events\RepositoryFilesRemoved; |
|
10
|
|
|
use Shamaseen\Repository\PathResolver; |
|
11
|
|
|
|
|
12
|
|
|
class Remover extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The name and signature of the console command. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $signature = 'ungenerate:repository |
|
20
|
|
|
{name : Class (singular) for example User} |
|
21
|
|
|
{--base= : Base path to inject the files\folders in} |
|
22
|
|
|
{--f|force : force deleting the files}'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Remove repository files'; |
|
30
|
|
|
|
|
31
|
|
|
protected array $userPaths; |
|
32
|
|
|
protected string $modelName; |
|
33
|
|
|
protected string $userPath; |
|
34
|
|
|
protected string $basePath; |
|
35
|
|
|
private Ungenerator $ungenerator; |
|
36
|
|
|
private PathResolver $pathResolver; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create a new command instance. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Ungenerator $ungenerator) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct(); |
|
44
|
|
|
$this->ungenerator = $ungenerator; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute the console command. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function handle(): int |
|
51
|
|
|
{ |
|
52
|
|
|
$this->userPaths = preg_split(' ([/\\\]) ', $this->argument('name')); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$this->modelName = array_pop($this->userPaths); |
|
55
|
|
|
$this->userPath = implode('/', str_replace('\\', '/', $this->userPaths)); |
|
56
|
|
|
$this->basePath = $this->option('base') ? $this->option('base') : Config::get('repository.base_path'); |
|
57
|
|
|
|
|
58
|
|
|
$this->pathResolver = new PathResolver($this->modelName, $this->userPath, $this->basePath); |
|
59
|
|
|
// Configure the generator |
|
60
|
|
|
config(['generator.base_path' => base_path($this->basePath)]); |
|
61
|
|
|
|
|
62
|
|
|
if (!$this->option('force') && !$this->confirm('This will delete '.$this->modelName.' files and folder, Do you want to continue ?')) { |
|
63
|
|
|
return false; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->remove('Controller'); |
|
67
|
|
|
$this->remove('Model'); |
|
68
|
|
|
$this->remove('Request'); |
|
69
|
|
|
$this->remove('Repository'); |
|
70
|
|
|
$this->remove('Resource'); |
|
71
|
|
|
$this->remove('Collection'); |
|
72
|
|
|
$this->remove('Policy'); |
|
73
|
|
|
$this->remove('Test'); |
|
74
|
|
|
|
|
75
|
|
|
RepositoryFilesRemoved::dispatch($this->basePath, $this->userPath, $this->modelName); |
|
76
|
|
|
|
|
77
|
|
|
$this->dumpAutoload(); |
|
78
|
|
|
|
|
79
|
|
|
return self::SUCCESS; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function remove($type): bool |
|
83
|
|
|
{ |
|
84
|
|
|
$fileToDelete = $this->pathResolver->outputPath($type); |
|
85
|
|
|
|
|
86
|
|
|
if (is_file($this->ungenerator->absolutePath($fileToDelete))) { |
|
87
|
|
|
$this->ungenerator->output($fileToDelete); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$pathsToDelete = count($this->userPaths); |
|
91
|
|
|
for ($i = 0; $i < $pathsToDelete; ++$i) { |
|
92
|
|
|
$pathFromBase = $this->ungenerator->absolutePath( |
|
93
|
|
|
$this->pathResolver->directionFromBase($type) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
if (is_dir($pathFromBase) && !(new FilesystemIterator($pathFromBase))->valid()) { |
|
97
|
|
|
rmdir($pathFromBase); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function dumpAutoload() |
|
105
|
|
|
{ |
|
106
|
|
|
shell_exec('composer dump-autoload'); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|