Skip to content

Commit 70a57d6

Browse files
committed
Add a way to register additional migrators
1 parent c402961 commit 70a57d6

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/Database/Commands/MigrateCommand.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CraftCms\Cms\Database\Commands;
66

77
use CraftCms\Cms\Console\CraftCommand;
8+
use CraftCms\Cms\Database\Events\RegisterMigrators;
89
use CraftCms\Cms\Database\Migrator;
910
use CraftCms\Cms\Plugin\Plugins;
1011
use CraftCms\Cms\Support\Str;
@@ -14,6 +15,7 @@
1415
use Illuminate\Contracts\Console\Isolatable;
1516
use Illuminate\Foundation\Console\DownCommand;
1617
use Illuminate\Foundation\Console\UpCommand;
18+
use Illuminate\Support\Facades\Event;
1719
use Laravel\Prompts\Concerns\Colors;
1820
use Laravel\Prompts\Themes\Default\Concerns\DrawsBoxes;
1921
use Throwable;
@@ -121,10 +123,11 @@ function () use (&$migrationsByTrack, &$plugins) {
121123
foreach ($migrationsByTrack as $track => $migrations) {
122124
$n = count($migrations);
123125

124-
$which = match ($track) {
125-
'craft' => 'Craft',
126-
'content' => 'content',
127-
default => $plugins[substr((string) $track, 7)]->name,
126+
$which = match (true) {
127+
$track === 'craft' => 'Craft',
128+
$track === 'content' => 'content',
129+
str_starts_with((string) $track, 'plugin') => $plugins[substr((string) $track, 7)]->name,
130+
default => $track,
128131
};
129132

130133
$this->box(
@@ -196,6 +199,28 @@ private function gatherMigrationsByTrack(array &$migrationsByTrack, array &$plug
196199
$migrationsByTrack['content'] = $contentMigrations;
197200
}
198201
}
202+
203+
if (Event::hasListeners(RegisterMigrators::class)) {
204+
Event::dispatch($event = new RegisterMigrators([]));
205+
206+
foreach ($event->migrators as $migrator) {
207+
if (! $migrator instanceof Migrator) {
208+
$this->components->warn($migrator::class.' is not an instance of '.Migrator::class);
209+
210+
continue;
211+
}
212+
213+
if (! $track = $migrator->getTrack()) {
214+
$this->components->warn('A migrator was registered without a track.');
215+
216+
continue;
217+
}
218+
219+
if (! empty($migrations = $migrator->getPendingMigrations())) {
220+
$migrationsByTrack[$track] = $migrations;
221+
}
222+
}
223+
}
199224
}
200225

201226
/**
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 CraftCms\Cms\Database\Events;
6+
7+
final class RegisterMigrators
8+
{
9+
public function __construct(
10+
/** @var \CraftCms\Cms\Database\Migrator[] */
11+
public array $migrators,
12+
) {}
13+
}

src/Database/Migrator.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CraftCms\Cms\Database;
66

77
use CraftCms\Aliases\Aliases;
8+
use Override;
89

910
/**
1011
* @internal
@@ -33,14 +34,19 @@ public function track(string $track): self
3334
return $this;
3435
}
3536

37+
public function getTrack(): ?string
38+
{
39+
return $this->repository->getTrack();
40+
}
41+
3642
public function setPaths(array $paths): self
3743
{
3844
$this->paths = $paths;
3945

4046
return $this;
4147
}
4248

43-
#[\Override]
49+
#[Override]
4450
public function run($paths = [], array $options = []): array
4551
{
4652
if (empty($paths)) {
@@ -56,13 +62,13 @@ public function run($paths = [], array $options = []): array
5662
return parent::run($paths, $options);
5763
}
5864

59-
#[\Override]
65+
#[Override]
6066
public function runMigration($migration, $method): void
6167
{
6268
parent::runMigration($migration, $method);
6369
}
6470

65-
#[\Override]
71+
#[Override]
6672
public function resetMigrations(array $migrations, array $paths, $pretend = false): array
6773
{
6874
return parent::resetMigrations($migrations, $paths, $pretend);

0 commit comments

Comments
 (0)