Skip to content

Commit 87111ac

Browse files
committed
add docs and improve functional
1 parent 8ada7b7 commit 87111ac

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

README.md

+35-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ composer require dentro/laravel-patcher
1919
```json
2020
{
2121
"require": {
22-
...
2322
"dentro/laravel-patcher": "^1.0"
2423
}
2524
}
@@ -43,16 +42,10 @@ Those file will be like:
4342
```php
4443
<?php
4544

46-
use Jalameta\Patcher\Patch;
45+
use Dentro\Patcher\Patch;
4746

4847
class WhatDoYouWantToPatch extends Patch
4948
{
50-
/**
51-
* Run patch script.
52-
*
53-
* @return void
54-
* @throws \Exception
55-
*/
5649
public function patch()
5750
{
5851
//
@@ -116,3 +109,37 @@ Patching: 2020_10_09_124616_add_attachment_beep
116109
Patched: 2020_10_09_124616_add_attachment_beep (0.06 seconds)
117110
```
118111

112+
#### SKIPPING THE PATCH
113+
You might need to skip single patch when run ```php artisan patcher:run```.
114+
Due to patch is unnecessary or patch is not eligible to run in your environment.
115+
Here you can add the ```eligible``` method to your patch class to evaluate the condition
116+
before running the ```patch``` method.
117+
118+
```php
119+
<?php
120+
121+
use Dentro\Patcher\Patch;
122+
use App\Models\User;
123+
124+
class WhatDoYouWantToPatch extends Patch
125+
{
126+
public function eligible(): bool
127+
{
128+
return User::query()->where('id', 331)->exists();
129+
}
130+
131+
public function patch()
132+
{
133+
$user = User::query()->find(331);
134+
// do something with user.
135+
}
136+
}
137+
```
138+
then the output of ```php artisan patcher:run``` will be:
139+
```shell script
140+
➜ php artisan patcher:run
141+
Patching: 2020_09_29_190531_fix_double_sections
142+
Skipped: 2020_09_29_190531_fix_double_sections is not eligible to run in current condition.
143+
Patching: 2020_10_09_124616_add_attachment_beep
144+
Patched: 2020_10_09_124616_add_attachment_beep (0.06 seconds)
145+
```

src/Patcher.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,28 @@ protected function patch(string $file, int $batch, bool $pretend): void
6363

6464
$startTime = microtime(true);
6565

66-
if (method_exists($migration, 'eligible') && $migration->eligible())
67-
{
66+
if ($this->isEligible()) {
6867
$this->runPatch($migration);
69-
}
7068

71-
$runTime = round(microtime(true) - $startTime, 2);
69+
$runTime = round(microtime(true) - $startTime, 2);
7270

73-
$this->repository->log($name, $batch);
71+
$this->repository->log($name, $batch);
7472

75-
$this->note("<info>Patched:</info> {$name} ({$runTime} seconds)");
73+
$this->note("<info>Patched:</info> {$name} ({$runTime} seconds).");
74+
} else {
75+
$this->note("<comment>Skipped:</comment> {$name} is not eligible to run in current condition.");
76+
}
7677
}
7778

7879
/**
7980
* Determine if patcher should run.
8081
*
81-
* @param \Illuminate\Database\Migrations\Migration $migration
8282
* @return bool
8383
*/
84-
public function isEligible(Migration $migration): bool
84+
public function isEligible(): bool
8585
{
86-
if (method_exists($migration, 'eligible')) {
87-
return $migration->eligible();
86+
if (method_exists($this, 'eligible')) {
87+
return $this->eligible();
8888
}
8989

9090
return true;

0 commit comments

Comments
 (0)