Skip to content

Commit

Permalink
Merge pull request #136 from ucan-lab/topic-135
Browse files Browse the repository at this point in the history
Support Stub Customization
  • Loading branch information
ucan-lab authored Jan 14, 2022
2 parents 8a79e28 + 5e66c62 commit 405dfcf
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 7 deletions.
51 changes: 51 additions & 0 deletions src/Console/DacapoStubPublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace UcanLab\LaravelDacapo\Console;

use Illuminate\Filesystem\Filesystem;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Console\DacapoUninstallCommandUseCase;

/**
* Class DacapoStubPublishCommand
*/
class DacapoStubPublishCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dacapo:stub:publish {--force : Overwrite any existing files}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish dacapo stubs that are available for customization.';

/**
* @var DacapoUninstallCommandUseCase
*/
protected DacapoUninstallCommandUseCase $useCase;

public function handle(): void
{
if (!is_dir($stubsPath = $this->laravel->basePath('stubs'))) {
(new Filesystem)->makeDirectory($stubsPath);
}

$files = [
realpath(__DIR__ . '/../Dacapo/Infra/Adapter/Stub/dacapo.migration.create.stub') => $stubsPath . '/dacapo.migration.create.stub',
realpath(__DIR__ . '/../Dacapo/Infra/Adapter/Stub/dacapo.migration.update.stub') => $stubsPath . '/dacapo.migration.update.stub',
];

foreach ($files as $from => $to) {
if (!file_exists($to) || $this->option('force')) {
file_put_contents($to, file_get_contents($from));
}
}

$this->info('Stubs published successfully.');
}
}
22 changes: 22 additions & 0 deletions src/Dacapo/Infra/Adapter/LaravelMigrationCreateStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace UcanLab\LaravelDacapo\Dacapo\Infra\Adapter;

use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationCreateStub;

class LaravelMigrationCreateStub implements MigrationCreateStub
{
/**
* @return string
*/
public function getStub(): string
{
$filename = base_path('stubs/dacapo.migration.create.stub');

if (file_exists($filename)) {
return file_get_contents($filename);
}

return file_get_contents(__DIR__ . '/Stub/dacapo.migration.create.stub');
}
}
22 changes: 22 additions & 0 deletions src/Dacapo/Infra/Adapter/LaravelMigrationUpdateStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace UcanLab\LaravelDacapo\Dacapo\Infra\Adapter;

use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationUpdateStub;

class LaravelMigrationUpdateStub implements MigrationUpdateStub
{
/**
* @return string
*/
public function getStub(): string
{
$filename = app_path('stubs/dacapo.migration.update.stub');

if (file_exists($filename)) {
return file_get_contents($filename);
}

return file_get_contents(__DIR__ . '/Stub/dacapo.migration.update.stub');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationUpdateStub;

class SchemaToConstraintForeignKeyMigrationConverter
{
const MIGRATION_COLUMN_INDENT = ' ';
protected Schema $schema;
protected MigrationUpdateStub $migrationUpdateStub;

/**
* @param MigrationUpdateStub $migrationUpdateStub
*/
public function __construct(MigrationUpdateStub $migrationUpdateStub)
{
$this->migrationUpdateStub = $migrationUpdateStub;
}

/**
* @param SchemaList $schemaList
Expand Down Expand Up @@ -81,7 +90,7 @@ protected function makeMigrationConnection(Schema $schema): string
*/
protected function makeMigrationContents(Schema $schema): string
{
$stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.update.stub');
$stub = $this->migrationUpdateStub->getStub();
$stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub);
$stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub);
$stub = str_replace('{{ table }}', $schema->getTableName(), $stub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
use UcanLab\LaravelDacapo\Dacapo\Domain\Entity\SchemaList;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationUpdateStub;

class SchemaToCreateIndexMigrationConverter
{
const MIGRATION_COLUMN_INDENT = ' ';
protected Schema $schema;
protected MigrationUpdateStub $migrationUpdateStub;

/**
* @param MigrationUpdateStub $migrationUpdateStub
*/
public function __construct(MigrationUpdateStub $migrationUpdateStub)
{
$this->migrationUpdateStub = $migrationUpdateStub;
}

/**
* @param SchemaList $schemaList
Expand Down Expand Up @@ -63,7 +72,7 @@ protected function makeMigrationName(Schema $schema): string
*/
protected function makeMigrationContents(Schema $schema): string
{
$stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.update.stub');
$stub = $this->migrationUpdateStub->getStub();
$stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub);
$stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub);
$stub = str_replace('{{ table }}', $schema->getTableName(), $stub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFile;
use UcanLab\LaravelDacapo\Dacapo\Domain\ValueObject\Migration\MigrationFileList;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Builder\DatabaseBuilder;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationCreateStub;

class SchemaToCreateTableMigrationConverter
{
const MIGRATION_COLUMN_INDENT = ' ';

protected DatabaseBuilder $databaseBuilder;
protected MigrationCreateStub $migrationCreateStub;

public function __construct(DatabaseBuilder $databaseBuilder)
{
public function __construct(
DatabaseBuilder $databaseBuilder,
MigrationCreateStub $migrationCreateStub
) {
$this->databaseBuilder = $databaseBuilder;
$this->migrationCreateStub = $migrationCreateStub;
}

/**
Expand Down Expand Up @@ -70,7 +75,7 @@ protected function makeMigrationName(Schema $schema): string
*/
protected function makeMigrationContents(Schema $schema): string
{
$stub = file_get_contents(__DIR__ . '/../../App/Storage/stubs/migration.create.stub');
$stub = $this->migrationCreateStub->getStub();
$stub = str_replace('{{ namespace }}', $this->makeMigrationNamespace($schema), $stub);
$stub = str_replace('{{ class }}', $this->makeMigrationClassName($schema), $stub);
$stub = str_replace('{{ connection }}', $this->makeMigrationConnection($schema), $stub);
Expand Down
8 changes: 8 additions & 0 deletions src/Dacapo/UseCase/Shared/Stub/MigrationCreateStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub;

interface MigrationCreateStub
{
public function getStub(): string;
}
8 changes: 8 additions & 0 deletions src/Dacapo/UseCase/Shared/Stub/MigrationUpdateStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub;

interface MigrationUpdateStub
{
public function getStub(): string;
}
8 changes: 8 additions & 0 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use UcanLab\LaravelDacapo\Console\DacapoClearCommand;
use UcanLab\LaravelDacapo\Console\DacapoCommand;
use UcanLab\LaravelDacapo\Console\DacapoInitCommand;
use UcanLab\LaravelDacapo\Console\DacapoStubPublishCommand;
use UcanLab\LaravelDacapo\Console\DacapoUninstallCommand;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\LaravelMigrationCreateStub;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\LaravelMigrationUpdateStub;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\LocalMigrationListRepository;
use UcanLab\LaravelDacapo\Dacapo\Infra\Adapter\LocalSchemaListRepository;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Builder\DatabaseBuilder;
Expand All @@ -18,6 +21,8 @@
use UcanLab\LaravelDacapo\Dacapo\UseCase\Builder\SqlsrvDatabaseBuilder;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Port\MigrationListRepository;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Port\SchemaListRepository;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationCreateStub;
use UcanLab\LaravelDacapo\Dacapo\UseCase\Shared\Stub\MigrationUpdateStub;

/**
* Class ConsoleServiceProvider.
Expand All @@ -27,12 +32,15 @@ class ConsoleServiceProvider extends ServiceProvider implements DeferrableProvid
public array $bindings = [
SchemaListRepository::class => LocalSchemaListRepository::class,
MigrationListRepository::class => LocalMigrationListRepository::class,
MigrationCreateStub::class => LaravelMigrationCreateStub::class,
MigrationUpdateStub::class => LaravelMigrationUpdateStub::class,
];

protected array $commands = [
DacapoInitCommand::class,
DacapoCommand::class,
DacapoClearCommand::class,
DacapoStubPublishCommand::class,
DacapoUninstallCommand::class,
];

Expand Down

0 comments on commit 405dfcf

Please sign in to comment.