Skip to content

Commit d4c7127

Browse files
Merge pull request #112 from TheDragonCode/4.x-1
Refactored `actions:upgrade` console command
2 parents 3b08a8e + 93e9754 commit d4c7127

File tree

2 files changed

+48
-120
lines changed

2 files changed

+48
-120
lines changed

src/Console/Upgrade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Upgrade extends Command
1212
{
1313
protected $name = Names::UPGRADE;
1414

15-
protected $description = 'Action structure upgrade from version 2 to 3';
15+
protected $description = 'Action project upgrade from version 3 to 4';
1616

1717
protected Processor|string $processor = UpgradeProcessor::class;
1818
}

src/Processors/Upgrade.php

Lines changed: 47 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -4,171 +4,99 @@
44

55
namespace DragonCode\LaravelActions\Processors;
66

7+
use Closure;
78
use DragonCode\LaravelActions\Concerns\Anonymous;
8-
use DragonCode\LaravelActions\ServiceProvider;
9-
use DragonCode\Support\Facades\Filesystem\Directory;
9+
use DragonCode\LaravelActions\Constants\Names;
1010
use DragonCode\Support\Facades\Filesystem\File;
1111
use DragonCode\Support\Facades\Helpers\Str;
12-
use DragonCode\Support\Helpers\Ables\Stringable;
1312

1413
class Upgrade extends Processor
1514
{
1615
use Anonymous;
1716

1817
public function handle(): void
1918
{
20-
if ($this->alreadyUpgraded()) {
21-
$this->notification->info('Action upgrade already done');
22-
23-
return;
24-
}
25-
2619
$this->run();
2720
}
2821

22+
protected function getFiles(string $path, ?Closure $filter = null): array
23+
{
24+
return $this->file->names($path, $filter, true);
25+
}
26+
2927
protected function run(): void
3028
{
3129
$this->moveFiles();
32-
$this->moveConfig();
33-
$this->callMigration();
34-
$this->clean();
3530
}
3631

3732
protected function moveFiles(): void
3833
{
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));
4136
}
4237
}
4338

44-
protected function move(string $filename): void
39+
protected function replace(string $filename): void
4540
{
4641
$content = $this->open($filename);
4742

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);
5344

5445
$this->store($filename, $content);
5546
}
5647

57-
protected function clean(): void
58-
{
59-
$this->notification->task(
60-
'Delete old directory',
61-
fn () => Directory::ensureDelete($this->oldPath())
62-
);
63-
}
64-
6548
protected function open(string $filename): string
6649
{
67-
return file_get_contents($this->oldPath($filename . '.php'));
50+
return file_get_contents($this->basePath($filename));
6851
}
6952

7053
protected function store(string $filename, string $content): void
7154
{
72-
File::store($this->newPath($filename . '.php'), $content);
55+
File::store($this->basePath($filename), $content);
7356
}
7457

75-
protected function replaceNamespace(string $content): string
58+
protected function replaceCommandNames(string $content): string
7659
{
7760
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+
],
8083
)->toString();
8184
}
8285

83-
protected function replaceClassName(string $content): string
86+
protected function getProjectFiles(): array
8487
{
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+
);
16896
}
16997

170-
protected function alreadyUpgraded(): bool
98+
protected function basePath(?string $filename = null): string
17199
{
172-
return Directory::exists($this->newPath());
100+
return base_path($filename);
173101
}
174102
}

0 commit comments

Comments
 (0)