|
4 | 4 |
|
5 | 5 | namespace DragonCode\LaravelActions\Processors; |
6 | 6 |
|
| 7 | +use Closure; |
7 | 8 | use DragonCode\LaravelActions\Concerns\Anonymous; |
8 | | -use DragonCode\LaravelActions\ServiceProvider; |
9 | | -use DragonCode\Support\Facades\Filesystem\Directory; |
| 9 | +use DragonCode\LaravelActions\Constants\Names; |
10 | 10 | use DragonCode\Support\Facades\Filesystem\File; |
11 | 11 | use DragonCode\Support\Facades\Helpers\Str; |
12 | | -use DragonCode\Support\Helpers\Ables\Stringable; |
13 | 12 |
|
14 | 13 | class Upgrade extends Processor |
15 | 14 | { |
16 | 15 | use Anonymous; |
17 | 16 |
|
18 | 17 | public function handle(): void |
19 | 18 | { |
20 | | - if ($this->alreadyUpgraded()) { |
21 | | - $this->notification->info('Action upgrade already done'); |
22 | | - |
23 | | - return; |
24 | | - } |
25 | | - |
26 | 19 | $this->run(); |
27 | 20 | } |
28 | 21 |
|
| 22 | + protected function getFiles(string $path, ?Closure $filter = null): array |
| 23 | + { |
| 24 | + return $this->file->names($path, $filter, true); |
| 25 | + } |
| 26 | + |
29 | 27 | protected function run(): void |
30 | 28 | { |
31 | 29 | $this->moveFiles(); |
32 | | - $this->moveConfig(); |
33 | | - $this->callMigration(); |
34 | | - $this->clean(); |
35 | 30 | } |
36 | 31 |
|
37 | 32 | protected function moveFiles(): void |
38 | 33 | { |
39 | | - foreach ($this->getOldFiles() as $filename) { |
40 | | - $this->notification->task($filename, fn () => $this->move($filename)); |
| 34 | + foreach ($this->getProjectFiles() as $filename) { |
| 35 | + $this->notification->task($filename, fn () => $this->replace($filename)); |
41 | 36 | } |
42 | 37 | } |
43 | 38 |
|
44 | | - protected function move(string $filename): void |
| 39 | + protected function replace(string $filename): void |
45 | 40 | { |
46 | 41 | $content = $this->open($filename); |
47 | 42 |
|
48 | | - $content = $this->replaceNamespace($content); |
49 | | - $content = $this->replaceClassName($content); |
50 | | - $content = $this->replaceDeclareStrictType($content); |
51 | | - $content = $this->replaceWithInvoke($content); |
52 | | - $content = $this->replaceProperties($content); |
| 43 | + $content = $this->replaceCommandNames($content); |
53 | 44 |
|
54 | 45 | $this->store($filename, $content); |
55 | 46 | } |
56 | 47 |
|
57 | | - protected function clean(): void |
58 | | - { |
59 | | - $this->notification->task( |
60 | | - 'Delete old directory', |
61 | | - fn () => Directory::ensureDelete($this->oldPath()) |
62 | | - ); |
63 | | - } |
64 | | - |
65 | 48 | protected function open(string $filename): string |
66 | 49 | { |
67 | | - return file_get_contents($this->oldPath($filename . '.php')); |
| 50 | + return file_get_contents($this->basePath($filename)); |
68 | 51 | } |
69 | 52 |
|
70 | 53 | protected function store(string $filename, string $content): void |
71 | 54 | { |
72 | | - File::store($this->newPath($filename . '.php'), $content); |
| 55 | + File::store($this->basePath($filename), $content); |
73 | 56 | } |
74 | 57 |
|
75 | | - protected function replaceNamespace(string $content): string |
| 58 | + protected function replaceCommandNames(string $content): string |
76 | 59 | { |
77 | 60 | return Str::of($content)->replace( |
78 | | - ['DragonCode\\LaravelActions\\Support\\Actionable', 'Actionable'], |
79 | | - ['DragonCode\\LaravelActions\\Action', 'Action'] |
| 61 | + [ |
| 62 | + 'make:migration:action', |
| 63 | + 'migrate:actions', |
| 64 | + 'migrate:actions:fresh', |
| 65 | + 'migrate:actions:install', |
| 66 | + 'migrate:actions:refresh', |
| 67 | + 'migrate:actions:reset', |
| 68 | + 'migrate:actions:rollback', |
| 69 | + 'migrate:actions:status', |
| 70 | + 'migrate:actions:upgrade', |
| 71 | + ], |
| 72 | + [ |
| 73 | + Names::MAKE, |
| 74 | + Names::ACTIONS, |
| 75 | + Names::FRESH, |
| 76 | + Names::INSTALL, |
| 77 | + Names::REFRESH, |
| 78 | + Names::RESET, |
| 79 | + Names::ROLLBACK, |
| 80 | + Names::STATUS, |
| 81 | + Names::UPGRADE, |
| 82 | + ], |
80 | 83 | )->toString(); |
81 | 84 | } |
82 | 85 |
|
83 | | - protected function replaceClassName(string $content): string |
| 86 | + protected function getProjectFiles(): array |
84 | 87 | { |
85 | | - return Str::of($content) |
86 | | - ->pregReplace('/((?:return\s+new\s+class\s*\(?\s*\)?|final\s+class|class)\s*.+extends\s+Action)/', 'return new class () extends Action') |
87 | | - ->trim() |
88 | | - ->trim(';') |
89 | | - ->append(';') |
90 | | - ->append(PHP_EOL) |
91 | | - ->toString(); |
92 | | - } |
93 | | - |
94 | | - protected function replaceDeclareStrictType(string $content): string |
95 | | - { |
96 | | - return Str::of($content) |
97 | | - ->replace('(declare\s*\(\s*strict_types\s*=\s*[1|0]\);)', '') |
98 | | - ->replace("<?php\n", "<?php\n\ndeclare(strict_types=1);\n") |
99 | | - ->toString(); |
100 | | - } |
101 | | - |
102 | | - protected function replaceWithInvoke(string $content): string |
103 | | - { |
104 | | - return Str::of($content) |
105 | | - ->when(! Str::matchContains($content, '/public\s+function\s+down/'), function (Stringable $string) { |
106 | | - return $string->pregReplace('/(public\s+function\s+up)/', 'public function __invoke'); |
107 | | - })->toString(); |
108 | | - } |
109 | | - |
110 | | - protected function replaceProperties(string $content): string |
111 | | - { |
112 | | - return Str::of($content) |
113 | | - ->pregReplace('/protected\s+\$once/', 'protected bool $once') |
114 | | - ->pregReplace('/protected\s+\$transactions/', 'protected bool $transactions') |
115 | | - ->pregReplace('/protected\s+\$transaction_attempts/', 'protected int $transactionAttempts') |
116 | | - ->pregReplace('/protected\s+\$environment/', 'protected string|array|null $environment') |
117 | | - ->pregReplace('/protected\s+\$except_environment/', 'protected string|array|null $exceptEnvironment') |
118 | | - ->pregReplace('/protected\s+\$before/', 'protected bool $before') |
119 | | - ->toString(); |
120 | | - } |
121 | | - |
122 | | - protected function moveConfig(): void |
123 | | - { |
124 | | - $this->notification->task('Moving config file', function () { |
125 | | - $this->runCommand('vendor:publish', [ |
126 | | - '--provider' => ServiceProvider::class, |
127 | | - '--force' => true, |
128 | | - ]); |
129 | | - |
130 | | - $path = config_path('actions.php'); |
131 | | - |
132 | | - $table = config('database.actions', 'migration_actions'); |
133 | | - |
134 | | - $content = Str::replace(file_get_contents($path), "'table' => 'migration_actions'", "'table' => '$table'"); |
135 | | - |
136 | | - file_put_contents($path, $content); |
137 | | - }); |
138 | | - } |
139 | | - |
140 | | - protected function callMigration(): void |
141 | | - { |
142 | | - $this->notification->task('Call migration', function () { |
143 | | - $path = $this->allowAnonymousMigrations() |
144 | | - ? __DIR__ . '/../../database/migrations/anonymous/2022_08_18_180137_change_migration_actions_table.php' |
145 | | - : __DIR__ . '/../../database/migrations/named/2022_08_18_180137_change_migration_actions_table.php'; |
146 | | - |
147 | | - $this->runCommand('migrate', [ |
148 | | - '--path' => $path, |
149 | | - '--realpath' => true, |
150 | | - '--force' => true, |
151 | | - ]); |
152 | | - }); |
153 | | - } |
154 | | - |
155 | | - protected function getOldFiles(): array |
156 | | - { |
157 | | - return $this->getFiles($this->oldPath()); |
158 | | - } |
159 | | - |
160 | | - protected function oldPath(?string $filename = null): string |
161 | | - { |
162 | | - return database_path('actions/' . $filename); |
163 | | - } |
164 | | - |
165 | | - protected function newPath(?string $filename = null): string |
166 | | - { |
167 | | - return $this->options->path . '/' . $filename; |
| 88 | + return $this->getFiles( |
| 89 | + base_path(), |
| 90 | + fn (string $path) => Str::endsWith($path, '.php') |
| 91 | + && ! Str::startsWith(realpath($path), [ |
| 92 | + realpath(base_path('vendor')), |
| 93 | + realpath(base_path('node_modules')), |
| 94 | + ]) |
| 95 | + ); |
168 | 96 | } |
169 | 97 |
|
170 | | - protected function alreadyUpgraded(): bool |
| 98 | + protected function basePath(?string $filename = null): string |
171 | 99 | { |
172 | | - return Directory::exists($this->newPath()); |
| 100 | + return base_path($filename); |
173 | 101 | } |
174 | 102 | } |
0 commit comments