Skip to content

Commit 8946ce4

Browse files
committed
improve code readiness
1 parent 90f93be commit 8946ce4

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

src/Patch.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ abstract class Patch extends Migration
3737
*/
3838
public $withinTransaction = false;
3939

40-
/**
41-
* Patch constructor.
42-
*/
43-
public function __construct()
44-
{
45-
$this->logger = app('log')->driver(PatcherServiceProvider::$LOG_CHANNEL);
46-
}
47-
4840
/**
4941
* Run patch script.
5042
*
@@ -77,4 +69,14 @@ public function setContainer(Container $container): Patch
7769

7870
return $this;
7971
}
72+
73+
/**
74+
* Set Logger instance.
75+
*
76+
* @param \Illuminate\Log\Logger $logger
77+
*/
78+
public function setLogger(Logger $logger): void
79+
{
80+
$this->logger = $logger;
81+
}
8082
}

src/Patcher.php

+25-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Dentro\Patcher\Events\PatchEnded;
66
use Dentro\Patcher\Events\PatchStarted;
7-
use Illuminate\Database\Migrations\Migration;
87
use Illuminate\Database\Migrations\Migrator;
98

109
class Patcher extends Migrator
@@ -28,12 +27,10 @@ public function runPending(array $migrations, array $options = []): void
2827

2928
$batch = $this->repository->getNextBatchNumber();
3029

31-
$pretend = $options['pretend'] ?? false;
32-
3330
$step = $options['step'] ?? false;
3431

3532
foreach ($migrations as $file) {
36-
$this->patch($file, $batch, $pretend);
33+
$this->patch($file, $batch);
3734

3835
if ($step) {
3936
$batch++;
@@ -46,45 +43,47 @@ public function runPending(array $migrations, array $options = []): void
4643
*
4744
* @param string $file
4845
* @param int $batch
49-
* @param bool $pretend
50-
*
5146
* @return void
5247
* @throws \Throwable
5348
*/
54-
protected function patch(string $file, int $batch, bool $pretend): void
49+
protected function patch(string $file, int $batch): void
5550
{
56-
$migration = $this->resolvePath($file);
51+
$patch = $this->resolvePath($file);
5752

5853
$name = $this->getMigrationName($file);
5954

60-
$migration->setContainer(app())->setCommand(app('command.patcher'));
61-
62-
$this->note("<comment>Patching:</comment> {$name}");
55+
$this->note("<comment>Patching:</comment> $name");
6356

6457
$startTime = microtime(true);
6558

66-
if ($this->isEligible()) {
67-
$this->runPatch($migration);
59+
if ($patch instanceof Patch && $this->isEligible($patch)) {
60+
$patch
61+
->setContainer(app())
62+
->setCommand(app('command.patcher'))
63+
->setLogger(app('log')->driver(PatcherServiceProvider::$LOG_CHANNEL));
64+
65+
$this->runPatch($patch);
6866

6967
$runTime = round(microtime(true) - $startTime, 2);
7068

7169
$this->repository->log($name, $batch);
7270

73-
$this->note("<info>Patched:</info> {$name} ({$runTime} seconds).");
71+
$this->note("<info>Patched:</info> $name ($runTime seconds).");
7472
} else {
75-
$this->note("<comment>Skipped:</comment> {$name} is not eligible to run in current condition.");
73+
$this->note("<comment>Skipped:</comment> $name is not eligible to run in current condition.");
7674
}
7775
}
7876

7977
/**
8078
* Determine if patcher should run.
8179
*
80+
* @param \Dentro\Patcher\Patch $patch
8281
* @return bool
8382
*/
84-
public function isEligible(): bool
83+
public function isEligible(Patch $patch): bool
8584
{
86-
if (method_exists($this, 'eligible')) {
87-
return $this->eligible();
85+
if (method_exists($patch, 'eligible')) {
86+
return $patch->eligible();
8887
}
8988

9089
return true;
@@ -93,11 +92,11 @@ public function isEligible(): bool
9392
/**
9493
* Run a migration inside a transaction if the database supports it.
9594
*
96-
* @param object $patch
95+
* @param \Dentro\Patcher\Patch $patch
9796
* @return void
9897
* @throws \Throwable
9998
*/
100-
protected function runPatch(object $patch): void
99+
protected function runPatch(Patch $patch): void
101100
{
102101
$connection = $this->resolveConnection(
103102
$patch->getConnection()
@@ -121,9 +120,11 @@ protected function runPatch(object $patch): void
121120
}
122121
};
123122

124-
$this->getSchemaGrammar($connection)->supportsSchemaTransactions()
125-
&& $patch->withinTransaction
126-
? $connection->transaction($callback)
127-
: $callback();
123+
if ($patch->withinTransaction && $this->getSchemaGrammar($connection)->supportsSchemaTransactions()) {
124+
$connection->transaction($callback);
125+
return;
126+
}
127+
128+
$callback();
128129
}
129130
}

0 commit comments

Comments
 (0)