22
33namespace Aiiro \Factory \Commands ;
44
5+ use Aiiro \Factory \Connections \DatabaseContract ;
6+ use Aiiro \Factory \Connections \MySql ;
7+ use Aiiro \Factory \Connections \Sqlite ;
58use Aiiro \Factory \Exceptions \UnknownConnectionException ;
69use Illuminate \Config \Repository ;
710use 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 /**
0 commit comments