Skip to content

Commit

Permalink
[migrator] Package will not redefine make:migrations anymore, `lara…
Browse files Browse the repository at this point in the history
…-asp-migrator:raw-migration` should be used instead (#3).
  • Loading branch information
LastDragon-ru committed May 9, 2021
1 parent a171723 commit 3ebb2d4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/migrator/src/Commands/RawMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Migrator\Commands;

use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
use Illuminate\Support\Composer;
use LastDragon_ru\LaraASP\Migrator\Extenders\RawMigrationCreator;
use LastDragon_ru\LaraASP\Migrator\Package;

use function str_replace;

class RawMigration extends MigrateMakeCommand {
public const Name = Package::Name.':raw-migration';

public function __construct(RawMigrationCreator $creator, Composer $composer) {
$this->signature = str_replace('make:migration', static::Name, $this->signature);

parent::__construct($creator, $composer);
}
}
43 changes: 43 additions & 0 deletions packages/migrator/src/Commands/RawMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Migrator\Commands;

use LastDragon_ru\LaraASP\Migrator\Package;
use LastDragon_ru\LaraASP\Migrator\Testing\Package\TestCase;
use Symfony\Component\Finder\Finder;

use function array_keys;
use function iterator_to_array;
use function mkdir;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

/**
* @internal
* @coversDefaultClass \LastDragon_ru\LaraASP\Migrator\Commands\RawMigration
*/
class RawMigrationTest extends TestCase {
/**
* @covers ::handle
*/
public function testHandle(): void {
$pkg = Package::Name;
$path = tempnam(sys_get_temp_dir(), $pkg);

unlink($path);
mkdir($path);

$finder = Finder::create()->in($path);

$this->assertCount(0, $finder->files());

$this->artisan("{$pkg}:raw-migration", [
'name' => 'RawMigration',
'--path' => $path,
'--realpath' => true,
]);

$this->assertCount(3, $finder->files());
}
}
7 changes: 7 additions & 0 deletions packages/migrator/src/Package.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Migrator;

final class Package {
public const Name = 'lara-asp-migrator';
}
16 changes: 13 additions & 3 deletions packages/migrator/src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\ServiceProvider;
use LastDragon_ru\LaraASP\Core\Concerns\ProviderWithCommands;
use LastDragon_ru\LaraASP\Migrator\Commands\RawMigration;
use LastDragon_ru\LaraASP\Migrator\Extenders\RawMigrationCreator;
use LastDragon_ru\LaraASP\Migrator\Extenders\RawSeederMakeCommand;
use LastDragon_ru\LaraASP\Migrator\Extenders\SmartMigrator;

class Provider extends ServiceProvider implements DeferrableProvider {
use ProviderWithCommands;

// <editor-fold desc="\Illuminate\Support\ServiceProvider">
// =========================================================================
/**
Expand All @@ -25,6 +29,12 @@ public function register() {
$this->registerMigrationCreator();
$this->registerSeederMakeCommand();
}

public function boot(): void {
$this->bootCommands(
RawMigration::class,
);
}
// </editor-fold>

// <editor-fold desc="\Illuminate\Contracts\Support\DeferrableProvider">
Expand All @@ -34,20 +44,20 @@ public function register() {
* @noinspection PhpMissingReturnTypeInspection
*/
public function provides() {
return [...parent::provides(), 'migrator', 'migration.creator', 'command.seeder.make'];
return [...parent::provides(), 'migrator', 'command.seeder.make', RawMigrationCreator::class];
}
// </editor-fold>

// <editor-fold desc="Functions">
// =========================================================================
protected function registerMigrator(): void {
$this->app->singleton('migrator', static function (Application $app): Migrator {
return new SmartMigrator($app['migration.repository'], $app['db'], $app['files'], $app['events'], $app);
return new SmartMigrator($app['migration.repository'], $app['db'], $app['files'], $app['events']);
});
}

protected function registerMigrationCreator(): void {
$this->app->singleton('migration.creator', static function (Application $app): MigrationCreator {
$this->app->bind(RawMigrationCreator::class, static function (Application $app): MigrationCreator {
return new RawMigrationCreator($app['files'], $app->basePath('stubs'));
});
}
Expand Down

0 comments on commit 3ebb2d4

Please sign in to comment.