2
2
3
3
namespace Eghamat24 \DatabaseRepository \Commands ;
4
4
5
- use Illuminate \Console \Command ;
6
- use Illuminate \Support \Str ;
7
5
use Eghamat24 \DatabaseRepository \Creators \BaseCreator ;
8
6
use Eghamat24 \DatabaseRepository \Creators \CreatorResource ;
9
7
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
8
+ use Illuminate \Support \Collection ;
10
9
11
10
class MakeResource extends BaseCommand
12
11
{
12
+ use CustomMySqlQueries;
13
+
14
+ private const OBJECT_NAME = 'Resource ' ;
15
+
13
16
/**
14
17
* The name and signature of the console command.
15
18
*
@@ -28,30 +31,59 @@ class MakeResource extends BaseCommand
28
31
*/
29
32
protected $ description = 'Create new resource ' ;
30
33
31
- use CustomMySqlQueries;
32
-
33
- /**
34
- * Execute the console command.
35
- *
36
- * @return int
37
- */
38
34
public function handle (): void
39
35
{
40
36
$ this ->setArguments ();
41
- $ resourceName = $ this ->entityName . " Resource " ;
37
+ $ resourceName = $ this ->entityName . self :: OBJECT_NAME ;
42
38
$ resourceNamespace = config ('repository.path.namespace.resources ' );
43
39
$ relativeResourcesPath = config ('repository.path.relative.resources ' );
44
- $ resourceStubsPath = __DIR__ . ' /../../ ' . config ( ' repository.path.stub.resources ' );
40
+
45
41
$ filenameWithPath = $ relativeResourcesPath . $ resourceName . '.php ' ;
46
42
47
- $ this ->checkDelete ($ filenameWithPath , $ resourceName , "Resource " );
43
+ $ this ->checkAndPrepare ($ filenameWithPath , $ resourceName , $ relativeResourcesPath , $ resourceNamespace );
44
+
45
+ $ RepoCreator = $ this ->getResourceCreator ($ resourceNamespace , $ resourceName );
46
+ $ baseContent = $ this ->generateBaseContent ($ RepoCreator , $ filenameWithPath );
47
+
48
+ $ this ->finalized ($ filenameWithPath , $ resourceName , $ baseContent );
49
+ }
50
+
51
+ /**
52
+ * @param string $filenameWithPath
53
+ * @param string $resourceName
54
+ * @param mixed $relativeResourcesPath
55
+ * @param mixed $resourceNamespace
56
+ * @return void
57
+ */
58
+ private function checkAndPrepare (string $ filenameWithPath , string $ resourceName , mixed $ relativeResourcesPath , mixed $ resourceNamespace ): void
59
+ {
60
+ $ this ->checkDelete ($ filenameWithPath , $ resourceName , self ::OBJECT_NAME );
48
61
$ this ->checkDirectory ($ relativeResourcesPath );
49
- $ this ->checkClassExist ($ resourceNamespace , $ resourceName , "Resource " );
62
+ $ this ->checkClassExist ($ resourceNamespace , $ resourceName , self ::OBJECT_NAME );
63
+ }
64
+
65
+ /**
66
+ * @param string $tableName
67
+ * @return Collection
68
+ */
69
+ private function getColumnsOf (string $ tableName ): Collection
70
+ {
71
+ $ columns = $ this ->getAllColumnsInTable ($ tableName );
72
+ $ this ->checkEmpty ($ columns , $ tableName );
50
73
51
- $ columns = $ this ->getAllColumnsInTable ($ this ->tableName );
52
- $ this ->checkEmpty ($ columns , $ this ->tableName );
74
+ return $ columns ;
75
+ }
76
+
77
+ /**
78
+ * @param mixed $resourceNamespace
79
+ * @param string $resourceName
80
+ * @return CreatorResource
81
+ */
82
+ private function getResourceCreator (mixed $ resourceNamespace , string $ resourceName ): CreatorResource
83
+ {
84
+ $ resourceStubsPath = __DIR__ . '/../../ ' . config ('repository.path.stub.resources ' );
53
85
54
- $ RepoCreator = new CreatorResource ($ columns ,
86
+ return new CreatorResource ($ this -> getColumnsOf ( $ this -> tableName ) ,
55
87
$ this ->tableName ,
56
88
$ this ->entityName ,
57
89
$ this ->entityNamespace ,
@@ -60,10 +92,17 @@ public function handle(): void
60
92
$ resourceStubsPath ,
61
93
$ this ->detectForeignKeys ,
62
94
$ this ->entityVariableName );
63
- $ creator = new BaseCreator ($ RepoCreator );
64
- $ baseContent = $ creator ->createClass ($ filenameWithPath , $ this );
65
- $ this ->finalized ($ filenameWithPath , $ resourceName , $ baseContent );
95
+ }
66
96
97
+ /**
98
+ * @param CreatorResource $RepoCreator
99
+ * @param string $filenameWithPath
100
+ * @return string
101
+ */
102
+ private function generateBaseContent (CreatorResource $ RepoCreator , string $ filenameWithPath ): string
103
+ {
104
+ $ creator = new BaseCreator ($ RepoCreator );
105
+ return $ creator ->createClass ($ filenameWithPath , $ this );
67
106
}
68
107
69
108
}
0 commit comments