Skip to content

Commit

Permalink
Avoid duplicate code for create table commands by extending new `Illu…
Browse files Browse the repository at this point in the history
…minate\Console\MigrationGeneratorCommand` (#48603)

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Apply fixes from StyleCI

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* formatting

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Oct 2, 2023
1 parent 2bfb70b commit 6b0ae71
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 466 deletions.
57 changes: 9 additions & 48 deletions src/Illuminate/Cache/Console/CacheTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Illuminate\Cache\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Console\MigrationGeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'cache:table')]
class CacheTableCommand extends Command
class CacheTableCommand extends MigrationGeneratorCommand
{
/**
* The console command name.
Expand All @@ -25,59 +23,22 @@ class CacheTableCommand extends Command
protected $description = 'Create a migration for the cache database table';

/**
* The filesystem instance.
* Get the migration table name.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* @var \Illuminate\Support\Composer
*
* @deprecated Will be removed in a future Laravel version.
*/
protected $composer;

/**
* Create a new cache table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();

$this->files = $files;
$this->composer = $composer;
}

/**
* Execute the console command.
*
* @return void
* @return string
*/
public function handle()
protected function migrationTableName()
{
$fullPath = $this->createBaseMigration();

$this->files->put($fullPath, $this->files->get(__DIR__.'/stubs/cache.stub'));

$this->components->info('Migration created successfully.');
return 'cache';
}

/**
* Create a base migration file for the table.
* Get the path to the migration stub file.
*
* @return string
*/
protected function createBaseMigration()
protected function migrationStubFile()
{
$name = 'create_cache_table';

$path = $this->laravel->databasePath().'/migrations';

return $this->laravel['migration.creator']->create($name, $path);
return __DIR__.'/stubs/cache.stub';
}
}
120 changes: 120 additions & 0 deletions src/Illuminate/Console/MigrationGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Illuminate\Console;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;

abstract class MigrationGeneratorCommand extends Command
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* The Composer instance.
*
* @var \Illuminate\Support\Composer
*
* @deprecated Will be removed in a future Laravel version.
*/
protected $composer;

/**
* Create a new migration generator command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();

$this->files = $files;
$this->composer = $composer;
}

/**
* Get the migration table name.
*
* @return string
*/
abstract protected function migrationTableName();

/**
* Get the path to the migration stub file.
*
* @return string
*/
abstract protected function migrationStubFile();

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$table = $this->migrationTableName();

if ($this->migrationExists($table)) {
$this->components->error('Migration already exists.');

return 1;
}

$this->replaceMigrationPlaceholders(
$this->createBaseMigration($table), $table
);

$this->components->info('Migration created successfully.');

return 0;
}

/**
* Create a base migration file for the table.
*
* @param string $table
* @return string
*/
protected function createBaseMigration($table)
{
return $this->laravel['migration.creator']->create(
'create_'.$table.'_table', $this->laravel->databasePath('/migrations')
);
}

/**
* Replace the placeholders in the generated migration file.
*
* @param string $path
* @param string $table
* @return void
*/
protected function replaceMigrationPlaceholders($path, $table)
{
$stub = str_replace(
'{{table}}', $table, $this->files->get($this->migrationStubFile())
);

$this->files->put($path, $stub);
}

/**
* Determine whether a migration for the table already exists.
*
* @param string $table
* @return bool
*/
protected function migrationExists($table)
{
return count($this->files->glob(
$this->laravel->joinPaths($this->laravel->databasePath('migrations'), '*_*_*_*_create_'.$table.'_table.php')
)) !== 0;
}
}
57 changes: 9 additions & 48 deletions src/Illuminate/Notifications/Console/NotificationTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Illuminate\Notifications\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Console\MigrationGeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'notifications:table')]
class NotificationTableCommand extends Command
class NotificationTableCommand extends MigrationGeneratorCommand
{
/**
* The console command name.
Expand All @@ -25,59 +23,22 @@ class NotificationTableCommand extends Command
protected $description = 'Create a migration for the notifications table';

/**
* The filesystem instance.
* Get the migration table name.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* @var \Illuminate\Support\Composer
*
* @deprecated Will be removed in a future Laravel version.
*/
protected $composer;

/**
* Create a new notifications table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();

$this->files = $files;
$this->composer = $composer;
}

/**
* Execute the console command.
*
* @return void
* @return string
*/
public function handle()
protected function migrationTableName()
{
$fullPath = $this->createBaseMigration();

$this->files->put($fullPath, $this->files->get(__DIR__.'/stubs/notifications.stub'));

$this->components->info('Migration created successfully.');
return 'notifications';
}

/**
* Create a base migration file for the notifications.
* Get the path to the migration stub file.
*
* @return string
*/
protected function createBaseMigration()
protected function migrationStubFile()
{
$name = 'create_notifications_table';

$path = $this->laravel->databasePath().'/migrations';

return $this->laravel['migration.creator']->create($name, $path);
return __DIR__.'/stubs/notifications.stub';
}
}
74 changes: 9 additions & 65 deletions src/Illuminate/Queue/Console/BatchesTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace Illuminate\Queue\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Console\MigrationGeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'queue:batches-table')]
class BatchesTableCommand extends Command
class BatchesTableCommand extends MigrationGeneratorCommand
{
/**
* The console command name.
Expand All @@ -25,76 +23,22 @@ class BatchesTableCommand extends Command
protected $description = 'Create a migration for the batches database table';

/**
* The filesystem instance.
* Get the migration table name.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* @var \Illuminate\Support\Composer
*
* @deprecated Will be removed in a future Laravel version.
*/
protected $composer;

/**
* Create a new batched queue jobs table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();

$this->files = $files;
$this->composer = $composer;
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$table = $this->laravel['config']['queue.batching.table'] ?? 'job_batches';

$this->replaceMigration(
$this->createBaseMigration($table), $table
);

$this->components->info('Migration created successfully.');
}

/**
* Create a base migration file for the table.
*
* @param string $table
* @return string
*/
protected function createBaseMigration($table = 'job_batches')
protected function migrationTableName()
{
return $this->laravel['migration.creator']->create(
'create_'.$table.'_table', $this->laravel->databasePath().'/migrations'
);
return $this->laravel['config']['queue.batching.table'] ?? 'job_batches';
}

/**
* Replace the generated migration with the batches job table stub.
* Get the path to the migration stub file.
*
* @param string $path
* @param string $table
* @return void
* @return string
*/
protected function replaceMigration($path, $table)
protected function migrationStubFile()
{
$stub = str_replace(
'{{table}}', $table, $this->files->get(__DIR__.'/stubs/batches.stub')
);

$this->files->put($path, $stub);
return __DIR__.'/stubs/batches.stub';
}
}
Loading

0 comments on commit 6b0ae71

Please sign in to comment.