Skip to content

Commit 168dbfa

Browse files
committed
fix class resolver and create event
1 parent 397deee commit 168dbfa

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

src/Events/PatchEnded.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Dentro\Patcher\Events;
4+
5+
class PatchEnded extends PatchEvent
6+
{
7+
}

src/Events/PatchEvent.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Dentro\Patcher\Events;
4+
5+
use Dentro\Patcher\Patch;
6+
7+
abstract class PatchEvent
8+
{
9+
public function __construct(
10+
public Patch $patch
11+
) {}
12+
}

src/Events/PatchStarted.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Dentro\Patcher\Events;
4+
5+
class PatchStarted extends PatchEvent
6+
{
7+
}

src/Patcher.php

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Dentro\Patcher;
44

5+
use Dentro\Patcher\Events\PatchEnded;
6+
use Dentro\Patcher\Events\PatchStarted;
57
use Illuminate\Database\Migrations\Migrator;
68

79
class Patcher extends Migrator
@@ -50,23 +52,17 @@ public function runPending(array $migrations, array $options = []): void
5052
*/
5153
protected function patch(string $file, int $batch, bool $pretend): void
5254
{
53-
$migration = $this->resolve(
54-
$name = $this->getMigrationName($file)
55-
);
56-
57-
$migration->setContainer(app())->setCommand(app('command.patcher'));
55+
$migration = $this->resolvePath($file);
5856

59-
if ($pretend) {
60-
$this->pretendToRun($migration, 'patch');
57+
$name = $this->getMigrationName($file);
6158

62-
return;
63-
}
59+
$migration->setContainer(app())->setCommand(app('command.patcher'));
6460

6561
$this->note("<comment>Patching:</comment> {$name}");
6662

6763
$startTime = microtime(true);
6864

69-
$this->runPatch($migration, 'patch');
65+
$this->runPatch($migration);
7066

7167
$runTime = round(microtime(true) - $startTime, 2);
7268

@@ -78,26 +74,36 @@ protected function patch(string $file, int $batch, bool $pretend): void
7874
/**
7975
* Run a migration inside a transaction if the database supports it.
8076
*
81-
* @param object $migration
82-
* @param string $method
83-
*
77+
* @param object $patch
8478
* @return void
8579
* @throws \Throwable
8680
*/
87-
protected function runPatch(object $migration, string $method): void
81+
protected function runPatch(object $patch): void
8882
{
8983
$connection = $this->resolveConnection(
90-
$migration->getConnection()
84+
$patch->getConnection()
9185
);
9286

93-
$callback = static function () use ($migration, $method) {
94-
if (method_exists($migration, $method)) {
95-
$migration->{$method}();
87+
$dispatchEvent = function (object $event) {
88+
$this->events->dispatch($event);
89+
};
90+
91+
$callback = static function () use ($patch, $dispatchEvent) {
92+
if (method_exists($patch, 'patch')) {
93+
if ($patch instanceof Patch) {
94+
$dispatchEvent(new PatchStarted($patch));
95+
}
96+
97+
$patch->patch();
98+
99+
if ($patch instanceof Patch) {
100+
$dispatchEvent(new PatchEnded($patch));
101+
}
96102
}
97103
};
98104

99105
$this->getSchemaGrammar($connection)->supportsSchemaTransactions()
100-
&& $migration->withinTransaction
106+
&& $patch->withinTransaction
101107
? $connection->transaction($callback)
102108
: $callback();
103109
}

stubs/blank.stub

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ return new class extends Patch
99
*
1010
* @return void
1111
*/
12-
public function patch()
12+
public function patch(): void
1313
{
1414
//
1515
}
16-
}
16+
};

tests/Command/stubs/schema.sql

Whitespace-only changes.

0 commit comments

Comments
 (0)