Skip to content

Commit 56a5610

Browse files
committed
Add all option to generate the factories of all tables in database.
1 parent eb41e88 commit 56a5610

File tree

5 files changed

+153
-31
lines changed

5 files changed

+153
-31
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@ After installing and Configuration, you can generate the factory file by running
4343

4444
Please pass the table name to `generate:factory` command as the argument.
4545

46-
**NOTE**
47-
This command connects to the database to retrieve the columns from table, so make sure that the database is configured.
4846
``` shell
4947
php artisan generate:factory some_samples
5048
```
5149

50+
**NOTE**
51+
This command connects to the database to retrieve the columns from table, so make sure that the database is configured.
52+
53+
#### To generate factories of all tables in database.
54+
55+
Use `--all` option without table name, to generate factories of all tables in database.
56+
57+
If a factory of table exists, it will be skipped and continue to generate factories of other tables.
58+
59+
```bash
60+
php artisan generate:factory --all
61+
```
62+
5263
## License
5364
This project is released under MIT License. See [MIT License](LICENSE)
5465
for the detail.

src/Commands/GenerateFactory.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Aiiro\Factory\Commands;
44

5+
use Aiiro\Factory\Connections\DatabaseContract;
6+
use Aiiro\Factory\Connections\MySql;
7+
use Aiiro\Factory\Connections\Sqlite;
58
use Aiiro\Factory\Exceptions\UnknownConnectionException;
69
use Illuminate\Config\Repository;
710
use Illuminate\Console\GeneratorCommand;
@@ -22,11 +25,18 @@ class GenerateFactory extends GeneratorCommand
2225
/**
2326
* @var string
2427
*/
25-
protected $name = 'generate:factory';
28+
protected $signature = 'generate:factory {name?} {--all}';
2629

27-
/** @var \Illuminate\Config\Repository */
30+
/**
31+
* @var \Illuminate\Config\Repository
32+
*/
2833
protected $config;
2934

35+
/**
36+
* @var DatabaseContract
37+
*/
38+
protected $database;
39+
3040
/**
3141
* GenerateFactory constructor.
3242
*
@@ -40,47 +50,53 @@ public function __construct(Filesystem $files, Repository $config)
4050
}
4151

4252
/**
43-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
53+
* @return bool|null|void
4454
* @throws \Aiiro\Factory\Exceptions\UnknownConnectionException
4555
*/
4656
public function handle()
4757
{
58+
/** @var Connection $conn */
59+
$conn = \DB::connection();
60+
/** @var \PDO $pdo */
61+
$pdo = \DB::connection()->getPdo();
62+
63+
if ($conn instanceof MySqlConnection) {
64+
$this->database = new MySql($pdo);
65+
} elseif ($conn instanceof SQLiteConnection) {
66+
$this->database = new Sqlite($pdo);
67+
} else {
68+
throw new UnknownConnectionException('Unknown connection is set.');
69+
}
70+
71+
if ($this->hasOption('all') && $this->option('all')) {
72+
$tables = $this->database->fetchTables();
73+
foreach ($tables as $tableRecord) {
74+
$this->runFactoryBuilder($tableRecord[0]);
75+
}
76+
} else {
77+
$table = $this->getNameInput();
78+
$this->runFactoryBuilder($table);
79+
}
80+
}
4881

49-
$table = $this->getNameInput();
82+
/**
83+
* @param $table
84+
* @return bool
85+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
86+
*/
87+
protected function runFactoryBuilder($table)
88+
{
5089
$model = Str::ucfirst(Str::camel($table));
5190
$factory = Str::singular($model) . 'Factory';
5291

5392
$path = $this->getPath($factory);
5493

5594
if ($this->alreadyExists($path)) {
5695
$this->error($factory . ' already exists!');
57-
5896
return false;
5997
}
6098

61-
/** @var Connection $conn */
62-
$conn = \DB::connection();
63-
/** @var \PDO $pdo */
64-
$pdo = \DB::connection()->getPdo();
65-
66-
$stmt = null;
67-
$columns = [];
68-
69-
if ($conn instanceof MySqlConnection) {
70-
$stmt = $pdo->prepare("DESCRIBE $table");
71-
$stmt->execute();
72-
foreach ($stmt->fetchAll() as $column) {
73-
$columns[] = $column['Field'];
74-
}
75-
} elseif ($conn instanceof SQLiteConnection) {
76-
$stmt = $pdo->prepare("PRAGMA table_info($table)");
77-
$stmt->execute();
78-
foreach ($stmt->fetchAll() as $column) {
79-
$columns[] = $column['name'];
80-
}
81-
} else {
82-
throw new UnknownConnectionException('Unknown connection is set.');
83-
}
99+
$columns = $this->database->fetchColumns($table);
84100

85101
$factoryContent = $this->buildFactory(
86102
$this->config->get('factory-generator.namespace.model'),
@@ -91,7 +107,6 @@ public function handle()
91107
$this->createFile($path, $factoryContent);
92108

93109
$this->info($factory . ' created successfully.');
94-
95110
}
96111

97112
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Aiiro\Factory\Connections;
4+
5+
interface DatabaseContract
6+
{
7+
public function fetchColumns($table);
8+
}

src/Connections/MySql.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Aiiro\Factory\Connections;
4+
5+
class MySql implements DatabaseContract
6+
{
7+
8+
/**
9+
* @var \PDO
10+
*/
11+
protected $pdo;
12+
13+
/**
14+
* MySql constructor.
15+
*
16+
* @param $pdo
17+
*/
18+
public function __construct($pdo)
19+
{
20+
$this->pdo = $pdo;
21+
}
22+
23+
public function fetchTables()
24+
{
25+
$stmt = $this->pdo->prepare("SHOW TABLES");
26+
$stmt->execute();
27+
28+
return $stmt->fetchAll();
29+
}
30+
31+
32+
public function fetchColumns($table)
33+
{
34+
$columns = [];
35+
36+
$stmt = $this->pdo->prepare("DESCRIBE $table");
37+
$stmt->execute();
38+
39+
foreach ($stmt->fetchAll() as $column) {
40+
$columns[] = $column['Field'];
41+
}
42+
43+
return $columns;
44+
}
45+
}

src/Connections/Sqlite.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Aiiro\Factory\Connections;
4+
5+
class Sqlite implements DatabaseContract
6+
{
7+
8+
/**
9+
* @var \PDO
10+
*/
11+
protected $pdo;
12+
13+
/**
14+
* Sqlite constructor.
15+
*
16+
* @param $pdo
17+
*/
18+
public function __construct($pdo)
19+
{
20+
$this->pdo = $pdo;
21+
}
22+
23+
public function fetchTables()
24+
{
25+
$stmt = $this->pdo->prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT IN ('migrations', 'sqlite_sequence');");
26+
$stmt->execute();
27+
28+
return $stmt->fetchAll();
29+
}
30+
31+
public function fetchColumns($table)
32+
{
33+
$columns = [];
34+
35+
$stmt = $this->pdo->prepare("PRAGMA table_info($table)");
36+
$stmt->execute();
37+
foreach ($stmt->fetchAll() as $column) {
38+
$columns[] = $column['name'];
39+
}
40+
41+
return $columns;
42+
}
43+
}

0 commit comments

Comments
 (0)