Skip to content

Commit

Permalink
Commands for one table #58 (#59)
Browse files Browse the repository at this point in the history
* Update the attach() contract of `ProviderInterface` to generate specific table

* Add `--table` option to `bin/console` all commands and testing

* Use canonicalized absolute pathname

* Restrict the columns to `Table` instance

* Update the command to include specific table functionality

* Update additional command options in the README
  • Loading branch information
cable8mm authored Mar 28, 2024
1 parent 2f9a86e commit 2c17211
Show file tree
Hide file tree
Showing 27 changed files with 224 additions and 52 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The Xeed is to generate new model, seed, database seed, factory and migration fi
> [!TIP]
> It can function as both `php artisan xeed:*` commands for Laravel and `bin/console *` commands for Standalone, providing 100% identical functionality. Therefore, you can use it within your own Laravel project or as a standalone application.
We have provided the API Documentation on the web. For more information, please visit https://www.palgle.com/xeed/ ❤️
We have provided the API Documentation on the web. For more information, please visit <https://www.palgle.com/xeed/> ❤️

### Features

Expand Down Expand Up @@ -76,23 +76,35 @@ composer create-project cable8mm/xeed
```shell tab=Laravel
php artisan xeed:models
# Generate all models from database in `app/Models` folder

php artisan xeed:models -f -t xeeds
# Force to generate a model from `xeeds` table in `app/Models` folder
```

```shell tab=Standalone
bin/console models
# Generate all models from database in `dist/app/Models` folder

bin/console models -f -t xeeds
# Force to generate a model from `xeeds` table in `app/Models` folder
```

### Generate `Seeders`

```shell tab=Laravel
php artisan xeed:seeders
# Generate all seeds from database in `database/seeders` folder

php artisan xeed:seeders -f -t xeeds
# Force to generate a seeder from `xeeds` table in `database/seeders` folder
```

```shell tab=Standalone
bin/console seeders
# Generate all seeds from database in `dist/database/seeders` folder

bin/console seeders -f -t xeeds
# Force to generate a seeder from `xeeds` table in `dist/database/seeders` folder
```

### Generate `DatabaseSeeder`
Expand All @@ -112,23 +124,35 @@ bin/console database
```shell tab=Laravel
php artisan xeed:factories
# Generate all factories from database in `database/factories' folder

php artisan xeed:factories -f -t xeeds
# Force to generate a factory from `xeeds` table in `database/factories' folder
```

```shell tab=Standalone
bin/console factories
# Generate all factories from database in `dist/database/factories' folder

bin/console factories -f -t xeeds
# Force to generate a factory from `xeeds` table in `database/factories' folder
```

### Generate `Migrations`

```shell tab=Laravel
php artisan xeed:migrations
# Generate all migrations from database in `database/migrations' folder

php artisan xeed:migrations -f -t xeeds
# Force to generate a migration from `xeeds` table in `database/migrations' folder
```

```shell tab=Standalone
bin/console migrations
# Generate all migrations from database in `dist/database/migrations' folder

bin/console migrations -f -t xeeds
# Force to generate a migration from `xeeds` table in `database/migrations' folder
```

The generated files are stored in the same folder as your Laravel project. Please check the `dist` folder.
Expand Down
14 changes: 12 additions & 2 deletions src/Command/GenerateFactoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name: 'generate-factories',
description: 'Generate factories. run `bin/console generate-factories` or `bin/console factories`',
hidden: false,
aliases: ['factories']
aliases: ['factories', 'factory']
)]
class GenerateFactoriesCommand extends Command
{
Expand All @@ -39,6 +39,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Are files forcibly deleted even if they exist?',
false
)->addOption(
'table',
't',
InputOption::VALUE_OPTIONAL,
'Are you generating the specific table with the factory?',
null
);
}

Expand All @@ -49,7 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $input->getOption('force') ?? true;

$tables = Xeed::getInstance()->attach()->getTables();
$table = $input->getOption('table');

$tables = is_null($table)
? Xeed::getInstance()->attach()->getTables()
: Xeed::getInstance()->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
14 changes: 12 additions & 2 deletions src/Command/GenerateMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
name: 'generate-migrations',
description: 'Generate migrations. run `bin/console generate-migrations` or `bin/console migrations`',
hidden: false,
aliases: ['migrations']
aliases: ['migrations', 'migration']
)]
class GenerateMigrationsCommand extends Command
{
Expand All @@ -40,6 +40,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Are files forcibly deleted even if they exist?',
false
)->addOption(
'table',
't',
InputOption::VALUE_OPTIONAL,
'Are you generating the specific table with the migration?',
null
);
}

Expand All @@ -50,7 +56,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $input->getOption('force') ?? true;

$tables = Xeed::getInstance()->attach()->getTables();
$table = $input->getOption('table');

$tables = is_null($table)
? Xeed::getInstance()->attach()->getTables()
: Xeed::getInstance()->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
14 changes: 12 additions & 2 deletions src/Command/GenerateModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name: 'generate-models',
description: 'Generate models. run `bin/console generate-models` or `bin/console models`',
hidden: false,
aliases: ['models']
aliases: ['models', 'model']
)]
class GenerateModelsCommand extends Command
{
Expand All @@ -39,6 +39,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Are files forcibly deleted even if they exist?',
false
)->addOption(
'table',
't',
InputOption::VALUE_OPTIONAL,
'Are you generating the specific table with the model?',
null
);
}

Expand All @@ -49,7 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $input->getOption('force') ?? true;

$tables = Xeed::getInstance()->attach()->getTables();
$table = $input->getOption('table');

$tables = is_null($table)
? Xeed::getInstance()->attach()->getTables()
: Xeed::getInstance()->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
14 changes: 12 additions & 2 deletions src/Command/GenerateSeedersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
name: 'generate-seeders',
description: 'Generate seeders. run `bin/console generate-seeders` or `bin/console seeders`',
hidden: false,
aliases: ['seeders']
aliases: ['seeders', 'seeder']
)]
class GenerateSeedersCommand extends Command
{
Expand All @@ -39,6 +39,12 @@ protected function configure(): void
InputOption::VALUE_OPTIONAL,
'Are files forcibly deleted even if they exist?',
false
)->addOption(
'table',
't',
InputOption::VALUE_OPTIONAL,
'Are you generating the specific table with the seed?',
null
);
}

Expand All @@ -49,7 +55,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$force = $input->getOption('force') ?? true;

$tables = Xeed::getInstance()->attach()->getTables();
$table = $input->getOption('table');

$tables = is_null($table)
? Xeed::getInstance()->attach()->getTables()
: Xeed::getInstance()->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ interface ProviderInterface
* Attach child provider to DB instance.
*
* @param \Cable8mm\Xeed\Xeed $xeed Xeed instance
* @param string $table The table name to attach child provider
*/
public function attach(Xeed $xeed): void;
public function attach(Xeed $xeed, ?string $table = null): void;

/**
* Do mapping method between another fields for Database.
Expand Down
12 changes: 8 additions & 4 deletions src/Laravel/Commands/GenerateFactoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class GenerateFactoriesCommand extends Command
*
* @var string
*/
protected $signature = 'xeed:factories {force=false}';
protected $signature = 'xeed:factories
{force=false : Are files forcibly deleted even if they exist?}
{--t|table= : Are you generating the specific table with the factory?}';

/**
* The console command description.
Expand All @@ -30,9 +32,11 @@ public function handle(Xeed $xeed)
{
$force = $this->argument('force');

$tables = $xeed->addPdo(
DB::connection()->getPDO()
)->attach()->getTables();
$table = $this->option('table');

$tables = is_null($table)
? $xeed->addPdo(DB::connection()->getPDO())->attach()->getTables()
: $xeed->addPdo(DB::connection()->getPDO())->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
12 changes: 8 additions & 4 deletions src/Laravel/Commands/GenerateMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class GenerateMigrationsCommand extends Command
*
* @var string
*/
protected $signature = 'xeed:migrations {force=false}';
protected $signature = 'xeed:migrations
{force=false : Are files forcibly deleted even if they exist?}
{--t|table= : Are you generating the specific table with the migration?}';

/**
* The console command description.
Expand All @@ -31,9 +33,11 @@ public function handle(Xeed $xeed)
{
$force = $this->argument('force');

$tables = $xeed->addPdo(
DB::connection()->getPDO()
)->attach()->getTables();
$table = $this->option('table');

$tables = is_null($table)
? $xeed->addPdo(DB::connection()->getPDO())->attach()->getTables()
: $xeed->addPdo(DB::connection()->getPDO())->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
12 changes: 8 additions & 4 deletions src/Laravel/Commands/GenerateModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class GenerateModelsCommand extends Command
*
* @var string
*/
protected $signature = 'xeed:models {force=false}';
protected $signature = 'xeed:models
{force=false : Are files forcibly deleted even if they exist?}
{--t|table= : Are you generating the specific table with the model?}';

/**
* The console command description.
Expand All @@ -30,9 +32,11 @@ public function handle(Xeed $xeed)
{
$force = $this->argument('force');

$tables = $xeed->addPdo(
DB::connection()->getPDO()
)->attach()->getTables();
$table = $this->option('table');

$tables = is_null($table)
? $xeed->addPdo(DB::connection()->getPDO())->attach()->getTables()
: $xeed->addPdo(DB::connection()->getPDO())->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
12 changes: 8 additions & 4 deletions src/Laravel/Commands/GenerateSeedersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class GenerateSeedersCommand extends Command
*
* @var string
*/
protected $signature = 'xeed:seeders {force=false}';
protected $signature = 'xeed:seeders
{force=false : Are files forcibly deleted even if they exist?}
{--t|table= : Are you generating the specific table with the seeder?}';

/**
* The console command description.
Expand All @@ -30,9 +32,11 @@ public function handle(Xeed $xeed)
{
$force = $this->argument('force');

$tables = $xeed->addPdo(
DB::connection()->getPDO()
)->attach()->getTables();
$table = $this->option('table');

$tables = is_null($table)
? $xeed->addPdo(DB::connection()->getPDO())->attach()->getTables()
: $xeed->addPdo(DB::connection()->getPDO())->attach($table)->getTables();

foreach ($tables as $table) {
try {
Expand Down
6 changes: 4 additions & 2 deletions src/Provider/MysqlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ final class MysqlProvider implements ProviderInterface
/**
* {@inheritDoc}
*/
public function attach(Xeed $xeed): void
public function attach(Xeed $xeed, ?string $table = null): void
{
$tables = $xeed->pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
$tables = is_null($table)
? $xeed->pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN)
: [$table];

foreach ($tables as $table) {
$columns = $xeed->pdo->query('SHOW COLUMNS FROM '.$table)->fetchAll(PDO::FETCH_ASSOC);
Expand Down
6 changes: 4 additions & 2 deletions src/Provider/PgsqlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ final class PgsqlProvider implements ProviderInterface
/**
* {@inheritDoc}
*/
public function attach(Xeed $xeed): void
public function attach(Xeed $xeed, ?string $table = null): void
{
$tables = $xeed->pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\' ORDER BY table_name')->fetchAll(PDO::FETCH_COLUMN);
$tables = is_null($table)
? $xeed->pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema = \'public\' ORDER BY table_name')->fetchAll(PDO::FETCH_COLUMN)
: [$table];

$tables = array_diff($tables, [
'geography_columns',
Expand Down
14 changes: 10 additions & 4 deletions src/Provider/SqliteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ final class SqliteProvider implements ProviderInterface
/**
* {@inheritDoc}
*/
public function attach(Xeed $xeed): void
public function attach(Xeed $xeed, ?string $table = null): void
{
$query = $xeed->pdo->query("SELECT name FROM sqlite_master WHERE type='table';");
if (is_null($table)) {
$query = $xeed->pdo->query("SELECT name FROM sqlite_master WHERE type='table';");

$tables = array_filter($query->fetchAll(), fn ($item) => $item['name'] !== 'sqlite_sequence');
$tables = array_filter($query->fetchAll(), fn ($item) => $item['name'] !== 'sqlite_sequence');

$tables = array_flatten($tables);
$tables = array_flatten($tables);
} else {
$tables = [$table];
}

foreach ($tables as $table) {
$columns = $xeed->pdo->query('SELECT * FROM PRAGMA_TABLE_INFO("'.$table.'");')->fetchAll();

$columnObject = [];

foreach ($columns as $column) {
$columnObject[] = new Column(...self::map($column, $table, $xeed));
}
Expand Down
Loading

0 comments on commit 2c17211

Please sign in to comment.