@@ -19,113 +19,128 @@ class MakeDTOCommand extends Command
1919 /**
2020 * Name and signiture of Command.
2121 * name
22+ *
2223 * @var string
2324 */
2425 protected $ name = 'make:dto ' ;
2526
2627 /**
2728 * command description.
2829 * description
30+ *
2931 * @var string
3032 */
3133 protected $ description = 'create a new DTO ' ;
3234
3335 /**
34- * Get Command argumant EX : HasAuth
35- * getArguments
36+ * __construct
3637 *
37- * @return array
38+ * @return void
3839 */
39- protected function getArguments ()
40+ public function __construct ()
4041 {
41- return [
42- ['dto ' , InputArgument::REQUIRED , 'The name of the DTO ' ],
43- ];
42+ parent ::__construct ();
4443 }
4544
46-
4745 /**
48- * __construct
49- *
50- * @return void
46+ * getClassNamespace
5147 */
52- public function __construct ()
48+ public function getDefaultNamespace (): string
5349 {
54- parent :: __construct () ;
50+ return ' App \\ Http \\ DTOs ' ;
5551 }
5652
5753 /**
58- * getDTOName
54+ * Return a vaid class name
55+ * getClass
5956 *
6057 * @return string
6158 */
62- private function getDTOName ()
59+ public function getClass ()
6360 {
64- $ dto = Str::studly ($ this ->argument ('dto ' ));
65- return $ dto ;
61+ return class_basename ($ this ->argument ($ this ->argumentName ));
6662 }
6763
6864 /**
69- * getDestinationFilePath
65+ * Generate class namespace dinamacally
66+ * getClassNamespace
7067 *
7168 * @return string
7269 */
73- protected function getDestinationFilePath ()
70+ public function getClassNamespace ()
7471 {
75- return app_path () . "/Http/DTOs " . '/ ' . $ this ->getDTOName () . '.php ' ;
72+ $ extra = str_replace ($ this ->getClass (), '' , $ this ->argument ($ this ->argumentName ));
73+
74+ $ extra = str_replace ('/ ' , '\\' , $ extra );
75+
76+ $ namespace = $ this ->getDefaultNamespace ();
77+
78+ $ namespace .= '\\' . $ extra ;
79+
80+ $ namespace = str_replace ('/ ' , '\\' , $ namespace );
81+
82+ return trim ($ namespace , '\\' );
7683 }
7784
7885 /**
79- * getDTONameWithoutNamespace
80- *
81- * @return string
86+ * Create view directory if not exists.
8287 */
83- private function getDTONameWithoutNamespace ( )
88+ public function createDir ( $ path )
8489 {
85- return class_basename ($ this ->getDTOName ());
90+ $ dir = dirname ($ path );
91+
92+ if (! file_exists ($ dir )) {
93+ mkdir ($ dir , 0777 , true );
94+ }
8695 }
8796
8897 /**
89- * getClassNamespace
98+ * Execute the console command.
9099 *
91- * @return string
100+ * @return int
92101 */
93- public function getDefaultNamespace (): string
102+ public function handle ()
94103 {
95- return "App \\Http \\DTOs " ;
104+ $ path = str_replace ('\\' , '/ ' , $ this ->getDestinationFilePath ());
105+
106+ $ fileContents = $ this ->getTemplateContents ();
107+
108+ $ this ->createDir ($ path );
109+
110+ if (File::exists ($ path )) {
111+ $ this ->error ("File {$ path } already exists! " );
112+
113+ return 1 ;
114+ }
115+
116+ File::put ($ path , $ fileContents );
117+ $ this ->info ("DTO generated successfully! path : {$ path }" );
118+
119+ return 0 ;
120+
96121 }
97122
98123 /**
99- * Return a vaid class name
100- * getClass
124+ * Get Command argumant EX : HasAuth
125+ * getArguments
101126 *
102- * @return string
127+ * @return array
103128 */
104- public function getClass ()
129+ protected function getArguments ()
105130 {
106- return class_basename ($ this ->argument ($ this ->argumentName ));
131+ return [
132+ ['dto ' , InputArgument::REQUIRED , 'The name of the DTO ' ],
133+ ];
107134 }
108135
109-
110136 /**
111- * Generate class namespace dinamacally
112- * getClassNamespace
137+ * getDestinationFilePath
113138 *
114139 * @return string
115140 */
116- public function getClassNamespace ()
141+ protected function getDestinationFilePath ()
117142 {
118- $ extra = str_replace ($ this ->getClass (), '' , $ this ->argument ($ this ->argumentName ));
119-
120- $ extra = str_replace ('/ ' , '\\' , $ extra );
121-
122- $ namespace = $ this ->getDefaultNamespace ();
123-
124- $ namespace .= '\\' . $ extra ;
125-
126- $ namespace = str_replace ('/ ' , '\\' , $ namespace );
127-
128- return trim ($ namespace , '\\' );
143+ return app_path () . '/Http/DTOs ' . '/ ' . $ this ->getDTOName () . '.php ' ;
129144 }
130145
131146 /**
@@ -136,6 +151,7 @@ public function getClassNamespace()
136151 protected function getStubFilePath ()
137152 {
138153 $ stub = '/stubs/dto.stub ' ;
154+
139155 return $ stub ;
140156 }
141157
@@ -149,8 +165,8 @@ protected function getTemplateContents()
149165 $ fileTemplate = file_get_contents (__DIR__ . $ this ->getStubFilePath ());
150166
151167 $ replaceOptions = [
152- 'CLASS_NAMESPACE ' => $ this ->getClassNamespace (),
153- 'CLASS ' => $ this ->getDTONameWithoutNamespace ()
168+ 'CLASS_NAMESPACE ' => $ this ->getClassNamespace (),
169+ 'CLASS ' => $ this ->getDTONameWithoutNamespace (),
154170 ];
155171
156172 foreach ($ replaceOptions as $ search => $ replace ) {
@@ -161,40 +177,24 @@ protected function getTemplateContents()
161177 }
162178
163179 /**
164- * Create view directory if not exists.
180+ * getDTOName
165181 *
166- * @param $path
182+ * @return string
167183 */
168- public function createDir ( $ path )
184+ private function getDTOName ( )
169185 {
170- $ dir = dirname ( $ path );
186+ $ dto = Str:: studly ( $ this -> argument ( ' dto ' ) );
171187
172- if (!file_exists ($ dir )) {
173- mkdir ($ dir , 0777 , true );
174- }
188+ return $ dto ;
175189 }
176190
177191 /**
178- * Execute the console command.
192+ * getDTONameWithoutNamespace
179193 *
180- * @return int
194+ * @return string
181195 */
182- public function handle ()
196+ private function getDTONameWithoutNamespace ()
183197 {
184- $ path = str_replace ('\\' , '/ ' , $ this ->getDestinationFilePath ());
185-
186- $ fileContents = $ this ->getTemplateContents ();
187-
188- $ this ->createDir ($ path );
189-
190- if (File::exists ($ path )) {
191- $ this ->error ("File {$ path } already exists! " );
192- return 1 ;
193- }
194-
195- File::put ($ path , $ fileContents );
196- $ this ->info ("DTO generated successfully! path : {$ path }" );
197- return 0 ;
198-
198+ return class_basename ($ this ->getDTOName ());
199199 }
200200}
0 commit comments