1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Actions\Migration; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Mtolhuys\LaravelSchematics\Http\Requests\DeleteRelationRequest; |
8
|
|
|
use Mtolhuys\LaravelSchematics\Actions\Migration\Traits\DeletesMigrations; |
9
|
|
|
|
10
|
|
|
class DeleteMigrationAction |
11
|
|
|
{ |
12
|
|
|
use DeletesMigrations; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param $request |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function execute($request) |
19
|
|
|
{ |
20
|
|
|
$migrations = scandir($this->path); |
21
|
|
|
|
22
|
|
|
foreach ($migrations as $migration) { |
23
|
|
|
if (str_contains($migration, ".php") && $this->isRelatedMigration($migration, $request)) { |
24
|
|
|
if ($this->autoMigrate) { |
25
|
|
|
$this->down($migration); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
File::delete("$this->path/$migration"); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $migration |
35
|
|
|
* @param $request |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
private function isRelatedMigration($migration, $request): bool |
39
|
|
|
{ |
40
|
|
|
$content = file_get_contents("$this->path/$migration"); |
41
|
|
|
|
42
|
|
|
if ($request instanceof DeleteRelationRequest) { |
43
|
|
|
$this->autoMigrate = true; |
44
|
|
|
|
45
|
|
|
return strpos($content, "laravel-schematics-{$request['table']}-relation") !== false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$table = Str::plural(Str::snake( |
49
|
|
|
substr($request['name'], strrpos($request['name'], '\\') + 1) |
50
|
|
|
)); |
51
|
|
|
|
52
|
|
|
return strpos($content, "laravel-schematics-$table-model") !== false |
53
|
|
|
|| strpos($content, "laravel-schematics-$table-relation") !== false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param SplFileInfo $file |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
private function readablePhp(SplFileInfo $file): bool |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return |
63
|
|
|
$file->isReadable() |
64
|
|
|
&& $file->isFile() |
65
|
|
|
&& mb_strtolower($file->getExtension()) === 'php'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|