44
55namespace DragonCode \LaravelHttpMacros \Commands ;
66
7+ use Closure ;
78use DragonCode \LaravelHttpMacros \Macros \Macro ;
89use DragonCode \Support \Facades \Filesystem \Directory ;
910use DragonCode \Support \Facades \Filesystem \File ;
1011use Illuminate \Console \Command ;
12+ use Illuminate \Support \Collection ;
1113use Illuminate \Support \Str ;
14+ use ReflectionFunction ;
15+ use ReflectionNamedType ;
16+ use ReflectionParameter ;
17+ use ReflectionUnionType ;
1218
13- use function array_map ;
1419use function base_path ;
20+ use function class_exists ;
1521use function collect ;
1622use function config ;
1723use function file_get_contents ;
18- use function implode ;
19- use function is_string ;
24+ use function is_numeric ;
25+ use function Laravel \Prompts \intro ;
26+ use function Laravel \Prompts \outro ;
2027use function sprintf ;
21-
22- use const PHP_EOL ;
28+ use function var_export ;
2329
2430class GenerateHelperCommand extends Command
2531{
@@ -29,71 +35,124 @@ class GenerateHelperCommand extends Command
2935
3036 public function handle (): void
3137 {
32- $ names = $ this ->names ();
38+ $ this ->sections ()->map (function (array $ macros , string $ section ) {
39+ intro ($ section );
40+
41+ return $ this ->macros ($ macros );
42+ })
43+ ->tap (fn () => outro ('storing ' ))
44+ ->tap (fn () => $ this ->cleanUp ())
45+ ->each (fn (Collection $ blocks , string $ section ) => $ this ->store (
46+ $ section ,
47+ $ this ->compileBlocks ($ section , $ blocks ->flatten ())
48+ ));
49+ }
50+
51+ protected function cleanUp (): void
52+ {
53+ $ this ->components ->task ('clean up ' , fn () => Directory::ensureDelete ($ this ->directory ()));
54+ }
55+
56+ protected function macros (array $ macros ): Collection
57+ {
58+ return collect ($ macros )->map (function (Macro |string $ macro , int |string $ name ) {
59+ $ name = $ this ->resolveName ($ macro , $ name );
3360
34- $ static = $ this ->make ($ names , true );
35- $ dynamic = $ this ->make ($ names );
61+ $ this ->components ->task ($ name , function () use ($ macro , $ name , &$ result ) {
62+ $ result = $ this ->prepare ($ name , $ this ->reflectionCallback ($ macro ::callback ())->getParameters ());
63+ });
3664
37- $ this ->cleanUp ();
38- $ this ->store ($ static , true );
39- $ this ->store ($ dynamic );
65+ return $ result ;
66+ });
4067 }
4168
42- protected function make ( array $ names , bool $ isStatic = false ): array
69+ protected function store ( string $ section , string $ content ): void
4370 {
44- return array_map (
45- fn (string $ name ) => sprintf (
46- ' * @method %s $this %s(\Closure|string $class, int|string|null $key = null) ' ,
47- $ isStatic ? 'static ' : '' ,
48- $ name
49- ),
50- $ names
51- );
71+ $ this ->components ->task ($ section , fn () => File::store ($ this ->helperPath ($ section ), $ content ));
5272 }
5373
54- protected function store ( array $ methods , bool $ isStatic = false ): void
74+ protected function compileBlocks ( string $ section , Collection $ blocks ): string
5575 {
56- File::store (
57- $ this ->path ($ this ->filename ($ isStatic )),
58- $ this ->makeDocBlock ($ methods )
76+ return Str::replace (
77+ ['{class} ' , '{methods} ' ],
78+ [
79+ Str::studly ($ section ),
80+ $ blocks ->implode ("\n" ),
81+ ],
82+ $ this ->stub ()
5983 );
6084 }
6185
62- protected function makeDocBlock ( array $ methods ): string
86+ protected function prepare ( string $ name , array $ functions ): array
6387 {
64- return Str:: replace ( ' {methods} ' , implode ( PHP_EOL , $ methods ) , $ this ->template ( ));
88+ return $ this -> docBlock ( $ name , $ this ->docBlockParameters ( $ functions ));
6589 }
6690
67- protected function names ( ): array
91+ protected function docBlock ( string $ name , string $ parameters ): array
6892 {
69- return collect ($ this ->macros ())->map (
70- fn (Macro |string $ macro , int |string $ name ) => is_string ($ name ) ? $ name : $ macro ::name ()
71- )->all ();
93+ return [
94+ sprintf (' * @method $this %s(%s) ' , $ name , $ parameters ),
95+ sprintf (' * @method static $this %s(%s) ' , $ name , $ parameters ),
96+ ];
7297 }
7398
74- protected function path (?string $ filename = null ): string
99+ /**
100+ * @param array<ReflectionParameter> $functions
101+ *
102+ * @return Collection
103+ */
104+ protected function docBlockParameters (array $ functions ): string
75105 {
76- return base_path ('vendor/_http_macros/ ' . $ filename );
106+ return collect ($ functions )->map (function (ReflectionParameter $ parameter ) {
107+ $ result = $ parameter ->hasType () ? $ this ->compactTypes ($ parameter ->getType ()) : 'mixed ' ;
108+
109+ $ result .= ' $ ' . $ parameter ->getName ();
110+
111+ if ($ parameter ->isDefaultValueAvailable ()) {
112+ $ result .= ' = ' . var_export ($ parameter ->getDefaultValue (), true );
113+ }
114+
115+ return $ result ;
116+ })->implode (', ' );
77117 }
78118
79- protected function filename ( bool $ isStatic ): string
119+ protected function compactTypes ( ReflectionNamedType | ReflectionUnionType $ type ): string
80120 {
81- return $ isStatic
82- ? '_ide_helper_macro_static.php '
83- : '_ide_helper_macro.php ' ;
121+ if ($ type instanceof ReflectionNamedType) {
122+ return class_exists ($ type ->getName ()) ? '\\' . $ type ->getName () : $ type ->getName ();
123+ }
124+
125+ return collect ($ type ->getTypes ())->map (
126+ fn (ReflectionNamedType $ type ) => $ this ->compactTypes ($ type )
127+ )->implode ('| ' );
84128 }
85129
86- protected function cleanUp (): void
130+ protected function reflectionCallback (Closure $ callback ): ReflectionFunction
131+ {
132+ return new ReflectionFunction ($ callback );
133+ }
134+
135+ protected function resolveName (Macro |string $ macro , int |string $ name ): string
136+ {
137+ return is_numeric ($ name ) ? $ macro ::name () : $ name ;
138+ }
139+
140+ protected function sections (): Collection
141+ {
142+ return collect (config ('http.macros ' , []));
143+ }
144+
145+ protected function helperPath (string $ name ): string
87146 {
88- Directory:: ensureDelete ( $ this ->path ()) ;
147+ return $ this ->directory () . " /_ide_helper_macro_ $ name .php " ;
89148 }
90149
91- protected function macros (): array
150+ protected function directory (): string
92151 {
93- return config ( ' http.macros.response ' , [] );
152+ return base_path ( ' vendor/_http_macros ' );
94153 }
95154
96- protected function template (): string
155+ protected function stub (): string
97156 {
98157 return file_get_contents (__DIR__ . '/../../stubs/helper.stub ' );
99158 }
0 commit comments