1
+ <?php
2
+
3
+ namespace CodeOfDigital \CacheRepository \Commands ;
4
+
5
+ use Illuminate \Support \Facades \Artisan ;
6
+ use Illuminate \Support \Pluralizer ;
7
+
8
+ class RepositoryCommand extends BaseCommand
9
+ {
10
+ /**
11
+ * The name of the command
12
+ *
13
+ * @var string
14
+ */
15
+ protected $ signature = 'make:repository {model} {--cache : Whether to use caching in your repository} ' ;
16
+
17
+ /**
18
+ * The description of command
19
+ *
20
+ * @var string
21
+ */
22
+ protected $ description = 'Create a new repository ' ;
23
+
24
+ /**
25
+ * Stub paths
26
+ *
27
+ * @var array|string[]
28
+ */
29
+ protected array $ stubs = [
30
+ 'interface ' => __DIR__ . '/stubs/repository-interface.stub ' ,
31
+ 'repository ' => __DIR__ . '/stubs/repository.stub '
32
+ ];
33
+
34
+ public function __construct ()
35
+ {
36
+ parent ::__construct ();
37
+ }
38
+
39
+ public function handle ()
40
+ {
41
+ $ this ->checkModel ();
42
+ list ($ interface , $ interfaceName ) = $ this ->createInterface ();
43
+ $ this ->createRepository ($ interface , $ interfaceName );
44
+ }
45
+
46
+ protected function checkModel ()
47
+ {
48
+ $ model = $ this ->appNamespace .$ this ->getSingularName ($ this ->argument ('model ' ));
49
+
50
+ $ this ->model = str_replace ('/ ' , '\\' , $ model );
51
+
52
+ if ($ this ->laravel ->runningInConsole ()) {
53
+ if (!class_exists ($ this ->model )) {
54
+ $ response = $ this ->ask ("Model [ {$ this ->model }] does not exist. Would you like to create it? " , 'Yes ' );
55
+
56
+ if ($ this ->isResponsePositive ($ response )) {
57
+ Artisan::call ('make:model ' , [
58
+ 'name ' => $ this ->model
59
+ ]);
60
+
61
+ $ this ->info ("Model [ {$ this ->model }] has been successfully created. " );
62
+ } else {
63
+ $ this ->info ("Model [ {$ this ->model }] will be skipped. " );
64
+ }
65
+ }
66
+ }
67
+
68
+ $ modelInfo = explode ('\\' , $ this ->model );
69
+ $ this ->modelName = $ modelInfo [array_key_last ($ modelInfo )];
70
+ }
71
+
72
+ /**
73
+ * Create a new repository interface
74
+ *
75
+ * @return string[]|void
76
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
77
+ * @throws \Psr\Container\ContainerExceptionInterface
78
+ * @throws \Psr\Container\NotFoundExceptionInterface
79
+ */
80
+ protected function createInterface ()
81
+ {
82
+ $ content = $ this ->fileManager ->get ($ this ->stubs ['interface ' ]);
83
+
84
+ $ replacements = [
85
+ '{{ namespace }} ' => "{$ this ->appNamespace }\Repository\{ $ this ->modelName } " ,
86
+ '{{ model }} ' => $ this ->modelName
87
+ ];
88
+
89
+ $ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
90
+
91
+ $ fileName = "{$ this ->modelName }RepositoryInterface " ;
92
+ $ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
93
+ $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
94
+
95
+ if (!$ this ->fileManager ->exists ($ fileDirectory ))
96
+ $ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
97
+
98
+ if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ fileDirectory )) {
99
+ $ response = $ this ->ask ("The interface [ {$ fileName }] has already exists. Do you want to overwrite it? " , 'Yes ' );
100
+
101
+ if (!$ this ->isResponsePositive ($ response )) {
102
+ $ this ->line ("The interface [ {$ fileName }] will not be overwritten. " );
103
+ return ;
104
+ }
105
+ }
106
+
107
+ $ this ->fileManager ->put ($ filePath , $ content );
108
+
109
+ $ this ->info ("The interface [ {$ fileName }] has been created " );
110
+
111
+ return ["{$ this ->appNamespace }\Repository\{ $ this ->modelName }\{ $ fileName} " , $ fileName ];
112
+ }
113
+
114
+ /**
115
+ * Create a new repository
116
+ *
117
+ * @param string $interface
118
+ * @param string $fileName
119
+ * @return void
120
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
121
+ * @throws \Psr\Container\ContainerExceptionInterface
122
+ * @throws \Psr\Container\NotFoundExceptionInterface
123
+ */
124
+ protected function createRepository (string $ interface , string $ fileName )
125
+ {
126
+ $ content = $ this ->fileManager ->get ($ this ->stubs ['repository ' ]);
127
+
128
+ $ replacements = [
129
+ '{{ interfaceNamespace }} ' => "{$ this ->appNamespace }{$ interface }" ,
130
+ '{{ interface }} ' => $ fileName ,
131
+ '{{ model }} ' => $ this ->model ,
132
+ '{{ modelName }} ' => $ this ->modelName ,
133
+ '{{ namespace }} ' => "{$ this ->appNamespace }\Repository\{ $ this ->modelName } "
134
+ ];
135
+
136
+ $ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
137
+
138
+ $ fileName = "{$ this ->modelName }Repository " ;
139
+ $ fileDirectory = app ()->basePath () . "/App/Repository/ {$ this ->modelName }" ;
140
+ $ filePath = "{$ fileDirectory }{$ fileName }.php " ;
141
+
142
+ if (!$ this ->fileManager ->exists ($ fileDirectory ))
143
+ $ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
144
+
145
+ if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ filePath )) {
146
+ $ response = $ this ->ask ("The repository [ {$ fileName }] already exists. Do you want to overwrite it? " , 'Yes ' );
147
+
148
+ if (!$ this ->isResponsePositive ($ response )) {
149
+ $ this ->info ("The repository [ {$ fileName }] will not be overwritten. " );
150
+ return ;
151
+ }
152
+ }
153
+
154
+ $ this ->fileManager ->put ($ filePath , $ content );
155
+
156
+ $ this ->info ("The repository [ {$ filePath }] has been created. " );
157
+ }
158
+
159
+
160
+ public function getSingularName ($ model ): string
161
+ {
162
+ return empty ($ model ) ? '' : ucwords (Pluralizer::singular ($ model ));
163
+ }
164
+ }
0 commit comments