Skip to content

Commit c968d9c

Browse files
Merge pull request #74 from TheDragonCode/3.x
Fixed paths
2 parents f51c192 + 8603eb9 commit c968d9c

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

docs/prologue/upgrade.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- PHP 7.3 and 7.4 was dropped
1111
- Laravel 6.0 was dropped
1212
- Dragon Code: Contracts (`dragon-code/contracts`) was dropped
13+
- Added property typing
1314

1415
## Medium Impact Changes
1516

@@ -91,3 +92,16 @@ If your class does not contain a `down` method, then you can replace the `up` me
9192
### Changed Migration Repository
9293

9394
Just call the `php artisan migrate` command to make changes to the action repository table.
95+
96+
### Added property typing
97+
98+
Make sure all overridden properties are typed:
99+
100+
```
101+
protected $once >> protected bool $once
102+
protected $transactions >> protected bool $transactions
103+
protected $transactionAttempts >> protected int $transactionAttempts
104+
protected $environment >> protected string|array|null $environment
105+
protected $exceptEnvironment >> protected string|array|null $exceptEnvironment
106+
protected $before >> protected bool $before
107+
```

src/Processors/Install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function create(): void
3535
protected function ensureDirectory(): void
3636
{
3737
Directory::ensureDirectory(
38-
$this->getActionsPath()
38+
$this->getActionsPath(realpath: $this->options->realpath)
3939
);
4040
}
4141
}

src/Processors/Make.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function handle(): void
2222
protected function run(): void
2323
{
2424
$name = $this->getName();
25-
$path = $this->getActionsPath($name);
25+
$path = $this->getActionsPath($name, realpath: $this->options->realpath);
2626

2727
$this->create($path);
2828
}

src/Processors/Migrate.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ protected function getNewFiles(): array
6767
{
6868
$completed = $this->repository->getCompleted()->pluck('action')->toArray();
6969

70-
return $this->getFiles(fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed), $this->options->path);
70+
return $this->getFiles(
71+
filter : fn (string $file) => ! Str::of($file)->replace('\\', '/')->contains($completed),
72+
path : $this->options->path,
73+
fullpath: true
74+
);
7175
}
7276

7377
protected function getBatch(): int

src/Processors/Processor.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use DragonCode\LaravelActions\Repositories\ActionRepository;
1313
use DragonCode\LaravelActions\Services\Migrator;
1414
use DragonCode\LaravelActions\Values\Options;
15+
use DragonCode\Support\Facades\Helpers\Arr;
1516
use DragonCode\Support\Facades\Helpers\Str;
1617
use DragonCode\Support\Filesystem\File;
18+
use DragonCode\Support\Helpers\Ables\Arrayable;
1719
use Illuminate\Console\OutputStyle;
1820
use Illuminate\Contracts\Events\Dispatcher;
1921
use Symfony\Component\Console\Input\InputInterface;
@@ -41,16 +43,22 @@ public function __construct(
4143
$this->migrator->setConnection($this->options->connection)->setOutput($this->output);
4244
}
4345

44-
protected function getFiles(?Closure $filter = null, ?string $path = null): array
46+
protected function getFiles(?Closure $filter = null, ?string $path = null, bool $realpath = false, bool $fullpath = false): array
4547
{
46-
$path = $this->getActionsPath($path);
48+
$path = $this->getActionsPath($path, $realpath);
4749

48-
return $this->file->exists($path) ? [$path] : $this->file->allPaths($path, $filter, true);
50+
$names = $this->file->exists($path) ? [$path] : $this->file->allPaths($path, $filter, true);
51+
52+
return Arr::of($names)
53+
->when(! $fullpath, fn (Arrayable $array) => $array
54+
->map(fn (string $value) => Str::of(realpath($value))->after(realpath($path))->ltrim('\\/')->toString())
55+
)
56+
->toArray();
4957
}
5058

51-
protected function getActionsPath(?string $path = null): string
59+
protected function getActionsPath(?string $path = null, bool $realpath = false): string
5260
{
53-
$path = $this->options->realpath ? $path : $this->config->path($path);
61+
$path = $realpath ? $path : $this->config->path($path);
5462

5563
if (! is_dir($path) && ! Str::endsWith($path, '.php')) {
5664
return $this->file->exists($path . '.php') ? $path . '.php' : $path;

src/Processors/Rollback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected function getActions(?int $step): array
5151
protected function rollbackAction(string $action): void
5252
{
5353
$this->migrator->runDown(
54-
$this->getActionsPath($action)
54+
$this->getActionsPath($action, realpath: $this->options->realpath)
5555
);
5656
}
5757

src/Processors/Upgrade.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ protected function move(string $filename): void
4545
$content = $this->replaceClassName($content);
4646
$content = $this->replaceDeclareStrictType($content);
4747
$content = $this->replaceWithInvoke($content);
48+
$content = $this->replaceProperties($content);
4849

4950
$this->store($filename, $content);
50-
$this->delete($filename);
5151
}
5252

5353
protected function clean(): void
@@ -57,19 +57,19 @@ protected function clean(): void
5757
));
5858
}
5959

60-
protected function open(string $path): string
60+
protected function open(string $filename): string
6161
{
62-
return file_get_contents(base_path('database/actions/' . $path));
62+
return file_get_contents(base_path('database/actions/' . $filename));
6363
}
6464

65-
protected function store(string $path, string $content): void
65+
protected function store(string $filename, string $content): void
6666
{
67-
file_put_contents($this->config->path($path), $content);
67+
File::store($this->config->path($filename), $content);
6868
}
6969

70-
protected function delete(string $path): void
70+
protected function delete(string $filename): void
7171
{
72-
File::ensureDelete($this->config->path($path));
72+
File::ensureDelete($filename);
7373
}
7474

7575
protected function replaceNamespace(string $content): string
@@ -83,8 +83,9 @@ protected function replaceNamespace(string $content): string
8383
protected function replaceClassName(string $content): string
8484
{
8585
return Str::of($content)
86-
->pregReplace('/^([final\s|class]+.+extends\sAction)$/', 'return new class () extends Action')
86+
->pregReplace('/((?:return\s+new\s+class\s*\(?\s*\)?|final\s+class|class)\s*.+extends\s+Action)/', 'return new class () extends Action')
8787
->trim()
88+
->trim(';')
8889
->append(';')
8990
->append(PHP_EOL)
9091
->toString();
@@ -106,6 +107,18 @@ protected function replaceWithInvoke(string $content): string
106107
})->toString();
107108
}
108109

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+
109122
protected function moveConfig(): void
110123
{
111124
$this->notification->task('Moving config file', function () {
@@ -126,7 +139,7 @@ protected function moveConfig(): void
126139

127140
protected function getOldFiles(): array
128141
{
129-
return $this->getFiles(path: database_path('actions'));
142+
return $this->getFiles(path: database_path('actions'), realpath: true);
130143
}
131144

132145
protected function alreadyUpgraded(): bool

0 commit comments

Comments
 (0)