1+ <?php
2+
3+ namespace Arrilot \Api \Generator ;
4+
5+ use Illuminate \Console \AppNamespaceDetectorTrait ;
6+ use Illuminate \Console \Command ;
7+ use Illuminate \Filesystem \Filesystem ;
8+ use Symfony \Component \Console \Input \InputArgument ;
9+
10+ class ApiMakeCommand extends Command
11+ {
12+ use AppNamespaceDetectorTrait;
13+
14+ /**
15+ * The filesystem instance.
16+ *
17+ * @var \Illuminate\Filesystem\Filesystem
18+ */
19+ protected $ files ;
20+
21+ /**
22+ * The console command name.
23+ *
24+ * @var string
25+ */
26+ protected $ name = 'make:api ' ;
27+
28+ /**
29+ * The console command description.
30+ *
31+ * @var string
32+ */
33+ protected $ description = 'Create api controller, transformer and api routes for a given model (arrilot/laravel-api-generator) ' ;
34+
35+ /**
36+ * The application namespace.
37+ *
38+ * @var string
39+ */
40+ protected $ appNamespace ;
41+
42+ /**
43+ * The array of variables available in stubs.
44+ *
45+ * @var array
46+ */
47+ protected $ stubVariables = [];
48+
49+ protected $ modelsBaseNamespace ;
50+
51+ /**
52+ * Create a new controller creator command instance.
53+ *
54+ * @param \Illuminate\Filesystem\Filesystem $files
55+ */
56+ public function __construct (Filesystem $ files )
57+ {
58+ parent ::__construct ();
59+
60+ $ this ->files = $ files ;
61+ }
62+
63+ /**
64+ * Execute the console command.
65+ *
66+ * @return void
67+ */
68+ public function fire ()
69+ {
70+ $ this ->prepareVariablesForStubs ($ this ->argument ('name ' ));
71+
72+ //create controller
73+
74+ //create transformer
75+
76+ //create routes
77+ }
78+
79+ /**
80+ * Prepare names, paths and namespaces for stubs.
81+ *
82+ * @param $name
83+ */
84+ protected function prepareVariablesForStubs ($ name )
85+ {
86+ $ this ->appNamespace = $ this ->getAppNamespace ();
87+
88+ $ baseDir = config ('laravel-api-generator.models_base_dir ' );
89+
90+ $ this ->modelsBaseNamespace = $ baseDir ? trim ($ baseDir , '\\' ).'\\' : '' ;
91+
92+ $ this ->setModelData ($ name )
93+ ->setControllerData ()
94+ ->setTransformerData ()
95+ ->setEndpoint ();
96+
97+ var_dump ($ this ->stubVariables );
98+ }
99+
100+ /**
101+ * Set the model name and namespace.
102+ *
103+ * @return $this
104+ */
105+ protected function setModelData ($ name )
106+ {
107+ if (str_contains ($ name , '/ ' ))
108+ {
109+ $ name = $ this ->convertSlashes ($ name );
110+ }
111+
112+ $ name = trim ($ name , '\\' );
113+
114+ $ this ->stubVariables ['modelFullNameWithoutRoot ' ] = $ name ;
115+ $ this ->stubVariables ['modelFullName ' ] = $ this ->appNamespace .$ this ->modelsBaseNamespace .$ name ;
116+
117+ $ exploded = explode ('\\' , $ this ->stubVariables ['modelFullName ' ]);
118+
119+ $ this ->stubVariables ['modelName ' ] = array_pop ($ exploded );
120+ $ this ->stubVariables ['modelNamespace ' ] = implode ('\\' , $ exploded );
121+
122+ return $ this ;
123+ }
124+
125+ /**
126+ * Set the controller name and namespace.
127+ *
128+ * @return string
129+ */
130+ protected function setControllerData ()
131+ {
132+ // $this->stubVariables['transformerName'] = $this->stubVariables['modelName'].'Transformer';
133+ // $this->stubVariables['transformerNamespace'] = $this->appNamespace.$this->convertSlashes(config('laravel-api-generator.transformers_path'));
134+ // $this->stubVariables['transformerFullName'] = trim($this->stubVariables['transformerNamespace'].'\\'.$this->stubVariables['transformerName'], '\\');
135+
136+ return $ this ;
137+ }
138+
139+ /**
140+ * Set the transformer name and namespace.
141+ *
142+ * @return string
143+ */
144+ protected function setTransformerData ()
145+ {
146+ $ this ->stubVariables ['transformerName ' ] = $ this ->stubVariables ['modelName ' ].'Transformer ' ;
147+ $ this ->stubVariables ['transformerNamespace ' ] = $ this ->appNamespace .$ this ->convertSlashes (config ('laravel-api-generator.transformers_path ' ));
148+ $ this ->stubVariables ['transformerFullName ' ] = trim ($ this ->stubVariables ['transformerNamespace ' ].'\\' .$ this ->stubVariables ['transformerName ' ], '\\' );
149+
150+ return $ this ;
151+ }
152+
153+ /**
154+ * Set endpoint for a given model.
155+ * "Profile\Payer" -> "profile_payers"
156+ *
157+ * @return string
158+ */
159+ protected function setEndpoint ()
160+ {
161+ $ endpoint = str_replace ('\\' , '' , $ this ->stubVariables ['modelFullNameWithoutRoot ' ]);
162+ $ endpoint = snake_case ($ endpoint );
163+
164+ $ this ->stubVariables ['endpoint ' ] = str_plural ($ endpoint );
165+
166+ return $ this ;
167+ }
168+
169+ /**
170+ * Get the console command arguments.
171+ *
172+ * @return array
173+ */
174+ protected function getArguments ()
175+ {
176+ return [
177+ ['name ' , InputArgument::REQUIRED , 'The name of the model ' ],
178+ ];
179+ }
180+
181+ /**
182+ * Convert "/" to "\".
183+ *
184+ * @param $string
185+ *
186+ * @return string
187+ */
188+ protected function convertSlashes ($ string )
189+ {
190+ return str_replace ('/ ' , '\\' , $ string );
191+
192+ }
193+ }
0 commit comments