Skip to content

Commit 747a392

Browse files
Fixed invokable calling
1 parent 2e38fe6 commit 747a392

File tree

12 files changed

+264
-43
lines changed

12 files changed

+264
-43
lines changed

README.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Github Workflow Status][badge_build]][link_build]
99
[![License][badge_license]][link_license]
1010

11-
> Actions are like version control for your migration process, allowing your team to modify and share the application's actionable schema. If you have ever had to tell a teammate
11+
> Actions are like version control for your migration process, allowing your team to modify and share the application's Action schema. If you have ever had to tell a teammate
1212
> to manually perform any action on a producton server, you've come across an issue that actions solves.
1313
1414
## Installation
@@ -132,7 +132,7 @@ php artisan make:migration:action
132132

133133
```php
134134
/* 2022_01_28_184116_branch_2x_1643384476.php */
135-
class Branch2x1643384476 extends Actionable { }
135+
class Branch2x1643384476 extends Action { }
136136
```
137137

138138
### Running actions
@@ -159,9 +159,9 @@ In some cases, you need to call the code every time you deploy the application.
159159
To do this, override the `$once` variable in the action file:
160160

161161
```php
162-
use DragonCode\LaravelActions\Services\Actionable;
162+
use DragonCode\LaravelActions\Action;
163163

164-
return new class extends Actionable
164+
return new class extends Action
165165
{
166166
protected $once = false;
167167

@@ -187,9 +187,9 @@ In some cases, it becomes necessary to execute an action in a specific environme
187187
For this you can use the `$environment` parameter:
188188

189189
```php
190-
use DragonCode\LaravelActions\Services\Actionable;
190+
use DragonCode\LaravelActions\Action;
191191

192-
return new class extends Actionable
192+
return new class extends Action
193193
{
194194
/** @var string|array|null */
195195
protected $environment = 'production';
@@ -204,9 +204,9 @@ return new class extends Actionable
204204
You can also specify multiple environment names:
205205

206206
```php
207-
use DragonCode\LaravelActions\Services\Actionable;
207+
use DragonCode\LaravelActions\Action;
208208

209-
return new class extends Actionable
209+
return new class extends Action
210210
{
211211
/** @var string|array|null */
212212
protected $environment = ['testing', 'staging'];
@@ -227,9 +227,9 @@ In some cases, it becomes necessary to execute an action excluding certain envir
227227
For this you can use the `$except_environment` parameter:
228228

229229
```php
230-
use DragonCode\LaravelActions\Services\Actionable;
230+
use DragonCode\LaravelActions\Action;
231231

232-
return new class extends Actionable
232+
return new class extends Action
233233
{
234234
/** @var string|array|null */
235235
protected $except_environment = 'production';
@@ -244,9 +244,9 @@ return new class extends Actionable
244244
You can also specify multiple environment names:
245245

246246
```php
247-
use DragonCode\LaravelActions\Services\Actionable;
247+
use DragonCode\LaravelActions\Action;
248248

249-
return new class extends Actionable
249+
return new class extends Action
250250
{
251251
/** @var string|array|null */
252252
protected $except_environment = ['testing', 'staging'];
@@ -275,9 +275,9 @@ When calling the `migrate:actions` command with the `before` parameter, the scri
275275
For backwards compatibility, the `before` parameter is set to `true` by default, but actions will only be executed if the option is explicitly passed.
276276

277277
```php
278-
use DragonCode\LaravelActions\Services\Actionable;
278+
use DragonCode\LaravelActions\Action;
279279

280-
return new class extends Actionable
280+
return new class extends Action
281281
{
282282
protected $before = false;
283283

@@ -320,9 +320,9 @@ By setting the `$transactions = true` parameter, you will ensure that your code
320320
will reduce the time it takes to create the action.
321321

322322
```php
323-
use DragonCode\LaravelActions\Services\Actionable;
323+
use DragonCode\LaravelActions\Action;
324324

325-
return new class extends Actionable
325+
return new class extends Action
326326
{
327327
protected $transactions = true;
328328

@@ -400,9 +400,9 @@ You can also override the `success` and `failed` methods, which are called on su
400400
#### If Success
401401

402402
```php
403-
use DragonCode\LaravelActions\Services\Actionable;use Illuminate\Support\Facade\Log;
403+
use DragonCode\LaravelActions\Action;use Illuminate\Support\Facade\Log;
404404

405-
return new class extends Actionable
405+
return new class extends Action
406406
{
407407
public function up(): void
408408
{
@@ -433,9 +433,11 @@ The log file will contain two `success` entries.
433433
#### If Failed
434434

435435
```php
436-
use DragonCode\LaravelActions\Services\Actionable;use Exeption;use Illuminate\Support\Facade\Log;
436+
use DragonCode\LaravelActions\Action;
437+
use Exeption;
438+
use Illuminate\Support\Facade\Log;
437439

438-
return new class extends Actionable
440+
return new class extends Action
439441
{
440442
public function up(): void
441443
{
@@ -468,9 +470,9 @@ The log file will contain two `failed` entries.
468470
Quite often, when working with actions, it becomes necessary to run one or another console command, and each time you have to write the following code:
469471

470472
```php
471-
use DragonCode\LaravelActions\Services\Actionable;use Illuminate\Support\Facades\Artisan;
473+
use DragonCode\LaravelActions\Action;use Illuminate\Support\Facades\Artisan;
472474

473-
return new class extends Actionable
475+
return new class extends Action
474476
{
475477
public function up()
476478
{
@@ -484,9 +486,9 @@ return new class extends Actionable
484486
Since version [`2.3`](https://github.com/TheDragonCode/laravel-migration-actions/releases/tag/v2.3.0) we have added a method call. Now calling commands has become much easier:
485487

486488
```php
487-
use DragonCode\LaravelActions\Services\Actionable;
489+
use DragonCode\LaravelActions\Action;
488490

489-
return new class extends Actionable
491+
return new class extends Action
490492
{
491493
public function up()
492494
{

docs/getting-started/how-to-use/creating.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,71 @@ php artisan make:migration:action foo/bar/QweRty
6565
php artisan make:migration:action foo/bar/QweRty.php
6666
# actions/foo/bar/2022_10_11_225734_qwe_rty.php
6767
```
68+
69+
## Invokable Method
70+
71+
By default, the new action class will contain the `__invoke` method, but you can easily replace it with public `up` name.
72+
73+
```php
74+
use DragonCode\LaravelActions\Action;
75+
76+
return new class () extends Action
77+
{
78+
public function __invoke(): void
79+
{
80+
// some code
81+
}
82+
};
83+
```
84+
85+
> Note that the `__invoke` method has been added as a single call.
86+
> This means that when the action is running, it will be called, but not when it is rolled back.
87+
>
88+
> You should also pay attention to the fact that if there is an `__invoke` method in the class, the `down` method will not be called.
89+
90+
```php
91+
use DragonCode\LaravelActions\Action;
92+
93+
return new class () extends Action
94+
{
95+
public function __invoke(): void {} // called when `php artisan migrate:actions` running
96+
97+
public function down(): void {} // doesn't call when `php artisan migrate:rollback` running
98+
// and any other commands to revert the action.
99+
};
100+
```
101+
102+
## Dependency Injection
103+
104+
You can also use the dependency injection with `__invoke`, `up` and `down` methods:
105+
106+
```php
107+
use DragonCode\LaravelActions\Action;
108+
use Tests\Concerns\Some;
109+
110+
return new class () extends Action
111+
{
112+
public function __invoke(Some $some): void
113+
{
114+
$value = $some->get('qwerty');
115+
}
116+
};
117+
```
118+
119+
```php
120+
use DragonCode\LaravelActions\Action;
121+
use Tests\Concerns\Some;
122+
123+
return new class () extends Action
124+
{
125+
public function up(Some $some): void
126+
{
127+
$value = $some->get('qwerty');
128+
}
129+
130+
public function down(Some $some): void
131+
{
132+
$value = $some->get('qwerty');
133+
}
134+
};
135+
```

src/Action.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,6 @@ abstract class Action extends Migration
5757
*/
5858
protected bool $before = true;
5959

60-
/**
61-
* Run the actions.
62-
*/
63-
public function up(): void
64-
{
65-
}
66-
67-
/**
68-
* Reverse the actions.
69-
*/
70-
public function down(): void
71-
{
72-
}
73-
7460
/**
7561
* Determines the type of launch of the action.
7662
*

src/Services/Migrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function runDown(string $file): void
6363
$action = $this->resolvePath($file);
6464
$name = $this->resolveActionName($file);
6565

66-
if ($this->hasAction($action, 'down')) {
66+
if (! $this->hasAction($action, '__invoke') && $this->hasAction($action, 'down')) {
6767
$this->runAction($action, $name, 'down');
6868
}
6969

tests/Commands/MigrateTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,32 @@ public function testMixedBefore()
490490
$this->assertDatabaseMigrationHas($this->table, 'test_before_enabled');
491491
$this->assertDatabaseMigrationHas($this->table, 'test_before_disabled');
492492
}
493+
494+
public function testDI(): void
495+
{
496+
$this->copyDI();
497+
498+
$table = 'test';
499+
500+
$this->artisan(Names::INSTALL)->assertExitCode(0);
501+
502+
$this->assertDatabaseCount($table, 0);
503+
$this->assertDatabaseCount($this->table, 0);
504+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke');
505+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke_down');
506+
$this->assertDatabaseMigrationDoesntLike($this->table, 'up_down');
507+
$this->assertDatabaseMigrationDoesntLike($table, 'up_down', column: 'value');
508+
$this->assertDatabaseMigrationDoesntLike($table, 'invoke_down', column: 'value');
509+
$this->assertDatabaseMigrationDoesntLike($table, 'invoke', column: 'value');
510+
$this->artisan(Names::MIGRATE)->assertExitCode(0);
511+
512+
$this->assertDatabaseCount($table, 3);
513+
$this->assertDatabaseCount($this->table, 3);
514+
$this->assertDatabaseMigrationHas($this->table, 'invoke');
515+
$this->assertDatabaseMigrationHas($this->table, 'invoke_down');
516+
$this->assertDatabaseMigrationHas($this->table, 'up_down');
517+
$this->assertDatabaseMigrationHas($table, 'up_down', column: 'value');
518+
$this->assertDatabaseMigrationHas($table, 'invoke_down', column: 'value');
519+
$this->assertDatabaseMigrationHas($table, 'invoke', column: 'value');
520+
}
493521
}

tests/Commands/RollbackTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,43 @@ public function testEnabledBefore()
264264
$this->assertDatabaseMigrationDoesntLike($this->table, 'test_before_enabled');
265265
$this->assertDatabaseMigrationDoesntLike($this->table, 'test_before_disabled');
266266
}
267+
268+
public function testDI(): void
269+
{
270+
$this->copyDI();
271+
272+
$table = 'test';
273+
274+
$this->artisan(Names::INSTALL)->assertExitCode(0);
275+
276+
$this->assertDatabaseCount($table, 0);
277+
$this->assertDatabaseCount($this->table, 0);
278+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke');
279+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke_down');
280+
$this->assertDatabaseMigrationDoesntLike($this->table, 'up_down');
281+
$this->assertDatabaseMigrationDoesntLike($table, 'up_down', column: 'value');
282+
$this->assertDatabaseMigrationDoesntLike($table, 'invoke_down', column: 'value');
283+
$this->assertDatabaseMigrationDoesntLike($table, 'invoke', column: 'value');
284+
$this->artisan(Names::MIGRATE)->assertExitCode(0);
285+
286+
$this->assertDatabaseCount($table, 3);
287+
$this->assertDatabaseCount($this->table, 3);
288+
$this->assertDatabaseMigrationHas($this->table, 'invoke');
289+
$this->assertDatabaseMigrationHas($this->table, 'invoke_down');
290+
$this->assertDatabaseMigrationHas($this->table, 'up_down');
291+
$this->assertDatabaseMigrationHas($table, 'up_down', column: 'value');
292+
$this->assertDatabaseMigrationHas($table, 'invoke_down', column: 'value');
293+
$this->assertDatabaseMigrationHas($table, 'invoke', column: 'value');
294+
295+
$this->artisan(Names::ROLLBACK)->assertExitCode(0);
296+
297+
$this->assertDatabaseCount($table, 2);
298+
$this->assertDatabaseCount($this->table, 0);
299+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke');
300+
$this->assertDatabaseMigrationDoesntLike($this->table, 'invoke_down');
301+
$this->assertDatabaseMigrationDoesntLike($this->table, 'up_down');
302+
$this->assertDatabaseMigrationDoesntLike($table, 'up_down', column: 'value');
303+
$this->assertDatabaseMigrationHas($table, 'invoke_down', column: 'value');
304+
$this->assertDatabaseMigrationHas($table, 'invoke', column: 'value');
305+
}
267306
}

tests/Concerns/AssertDatabase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ protected function assertDatabaseDoesntTable(string $table): void
2222
);
2323
}
2424

25-
protected function assertDatabaseMigrationHas(string $table, $value, $connection = null): void
25+
protected function assertDatabaseMigrationHas(string $table, $value, $connection = null, string $column = 'action'): void
2626
{
27-
$this->assertDatabaseHasLike($table, 'action', $value, $connection);
27+
$this->assertDatabaseHasLike($table, $column, $value, $connection);
2828
}
2929

3030
protected function assertDatabaseHasLike(string $table, string $column, $value, $connection = null): void
@@ -37,9 +37,9 @@ protected function assertDatabaseHasLike(string $table, string $column, $value,
3737
$this->assertTrue($exists);
3838
}
3939

40-
protected function assertDatabaseMigrationDoesntLike(string $table, $value, $connection = null): void
40+
protected function assertDatabaseMigrationDoesntLike(string $table, $value, $connection = null, string $column = 'action'): void
4141
{
42-
$this->assertDatabaseDoesntLike($table, 'action', $value, $connection);
42+
$this->assertDatabaseDoesntLike($table, $column, $value, $connection);
4343
}
4444

4545
protected function assertDatabaseDoesntLike(string $table, string $column, $value, $connection = null): void

tests/Concerns/Files.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ protected function copyFiles(): void
2222
);
2323
}
2424

25+
protected function copyDI(): void
26+
{
27+
File::copyDirectory(
28+
__DIR__ . '/../fixtures/app/di',
29+
$this->targetDirectory()
30+
);
31+
}
32+
2533
protected function copySuccessFailureMethod(): void
2634
{
2735
File::copy(

tests/Concerns/Some.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Concerns;
6+
7+
class Some
8+
{
9+
public function get(string $value): string
10+
{
11+
return $value;
12+
}
13+
}

0 commit comments

Comments
 (0)