99
1010use Carbon \Carbon ;
1111
12- class ArdorMakeClass extends Command
12+ use Illuminate \Console \GeneratorCommand ;
13+
14+ class ArdorMakeClass extends GeneratorCommand
1315{
16+
17+ private String $ classType ;
18+
19+ private array $ allowed = ['bundler ' ];
20+
1421 /**
1522 * The name and signature of the console command.
1623 *
1724 * @var string
1825 */
19- protected $ signature = 'ardor:make {name} {--type= : bundler|contract } ' ;
26+ protected $ signature = 'ardor:make {name} {--type= : bundler} ' ;
2027
2128 /**
2229 * The console command description.
@@ -26,23 +33,127 @@ class ArdorMakeClass extends Command
2633 protected $ description = 'This command will run all your bundler classes. ' ;
2734
2835 /**
29- * Create a new command instance .
36+ * Execute the console command.
3037 *
31- * @return void
38+ * @return mixed
3239 */
33- public function __construct ()
40+ public function handle ()
3441 {
35- parent ::__construct ();
42+
43+ $ name = $ this ->qualifyClass ($ this ->getNameInput ());
44+
45+ if (! $ this ->hasOption ('type ' ) || $ this ->option ('type ' ) === null || !in_array ($ this ->option ('type ' ), $ this ->allowed )) {
46+ $ this ->error ("A valid type must be provied! " );
47+ return ;
48+ }
49+
50+ $ this ->classType = $ this ->option ('type ' );
51+ $ path = $ this ->getPath ($ name );
52+
53+ if ((! $ this ->hasOption ('force ' ) ||
54+ ! $ this ->option ('force ' )) &&
55+ $ this ->alreadyExists ($ this ->getNameInput ())) {
56+ $ this ->error ($ this ->type .' already exists! ' );
57+
58+ return false ;
59+ }
60+
61+ $ this ->makeDirectory ($ path );
62+
63+ $ this ->files ->put ($ path , $ this ->sortImports ($ this ->buildClassCustom ($ name , $ this ->classType )));
64+
65+ $ this ->info ($ this ->type .' created successfully (data class and blade file). ' );
66+
3667 }
3768
3869 /**
39- * Execute the console command .
70+ * Build the class with the given name .
4071 *
41- * @return mixed
72+ * @param string $name
73+ * @return string
74+ *
75+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4276 */
43- public function handle ()
77+ protected function buildClassCustom (String $ name , String $ stubname )
78+ {
79+ $ stub = $ this ->files ->get ($ this ->getStubFilePath ($ stubname ));
80+
81+ return $ this ->replaceNamespace ($ stub , $ name )->replaceClass ($ stub , $ name );
82+ }
83+
84+ /**
85+ * Get the root namespace for the class.
86+ *
87+ * @return string
88+ */
89+ protected function rootNamespace ():String
90+ {
91+ return $ this ->laravel ->getNamespace ()."Bundlers " ;
92+ }
93+
94+ /**
95+ * Returns the path to the stubs folder
96+ */
97+ protected function getStub (): String {
98+ return __DIR__ ."/../../Stubs/ " ;
99+ }
100+
101+ /**
102+ * Get the stub file for the generator.
103+ *
104+ * @return string
105+ */
106+ protected function getStubFilePath (String $ stubname ):String
44107 {
108+ return $ this ->getStub ()."$ {stubname}.stub " ;
109+ }
45110
111+ /**
112+ * Returns the path for the document class
113+ *
114+ * @param mixed $name
115+ * @return String
116+ */
117+ protected function getPath ($ name ):String {
118+ $ name = Str::replaceFirst ($ this ->rootNamespace (), '' , $ name );
119+ $ type = ucfirst (Str::plural ($ this ->classType ));
120+ return $ this ->getPathFolder ($ name , "app/ $ {type}" );
121+ }
122+
123+
124+
125+ /**
126+ * Returns the base path for the file
127+ *
128+ * @param mixed $name
129+ * @param mixed $folder
130+ * @return String
131+ */
132+ protected function getPathFolder (String $ name , String $ folder = '' ): String {
133+ $ name = Str::replaceFirst ($ this ->rootNamespace (), '' , $ name );
134+ $ type = ucfirst ($ this ->classType );
135+ return base_path ($ folder .str_replace ('\\' , '/ ' , $ name ."$ {type}" ).".php " );
136+ }
137+
138+ /**
139+ * Replace the namespace for the given stub.
140+ *
141+ * @param string $stub
142+ * @param string $name
143+ * @return $this
144+ */
145+ protected function replaceNamespace (&$ stub , $ name )
146+ {
147+
148+ $ bladename = strtolower (str_replace ("\\" , ". " , str_replace ($ this ->rootNamespace ()."\\" , "printables. " , $ name )));
149+
150+ $ stub = str_replace (
151+ ['DummyNamespace ' , 'DummyRootNamespace ' , 'NamespacedDummyUserModel ' , 'DummyBladeName ' ],
152+ [$ this ->getNamespace ($ name ), $ this ->rootNamespace (), $ this ->userProviderModel ()],
153+ $ stub
154+ );
155+
156+ return $ this ;
46157 }
47158
48159}
0 commit comments