@@ -43,6 +43,12 @@ class MacrosCommand extends Command
4343 '\Illuminate\Http\UploadedFile ' ,
4444 ];
4545
46+ /** @var resource */
47+ protected $ file ;
48+
49+ /** @var int */
50+ protected $ indent = 0 ;
51+
4652 /**
4753 * Execute the console command.
4854 */
@@ -51,8 +57,8 @@ public function handle()
5157 $ classes = array_merge ($ this ->classes , config ('ide-macros.classes ' , []));
5258
5359 $ fileName = config ('ide-macros.filename ' );
54- $ file = fopen (base_path ($ fileName ), 'w ' );
55- fwrite ( $ file , "<?php " . PHP_EOL );
60+ $ this -> file = fopen (base_path ($ fileName ), 'w ' );
61+ $ this -> writeLine ( "<?php " );
5662
5763 foreach ($ classes as $ class ) {
5864 if (!class_exists ($ class )) {
@@ -72,39 +78,105 @@ public function handle()
7278 continue ;
7379 }
7480
75- fwrite ($ file , "namespace " . $ reflection ->getNamespaceName () . " { " . PHP_EOL );
76- fwrite ($ file , " class " . $ reflection ->getShortName () . " { " . PHP_EOL );
77-
78- foreach ($ macros as $ name => $ macro ) {
79- $ reflection = new \ReflectionFunction ($ macro );
80- if ($ comment = $ reflection ->getDocComment ()) {
81- fwrite ($ file , " $ comment " . PHP_EOL );
82- }
81+ $ this ->generateNamespace ($ reflection ->getNamespaceName (), function () use ($ macros , $ reflection ) {
82+ $ this ->generateClass ($ reflection ->getShortName (), function () use ($ macros ) {
83+ foreach ($ macros as $ name => $ macro ) {
84+ $ function = new \ReflectionFunction ($ macro );
85+ if ($ comment = $ function ->getDocComment ()) {
86+ $ this ->writeLine ($ comment , $ this ->indent );
8387
84- fwrite ($ file , " public static function " . $ name . "( " );
88+ if (strpos ($ comment , '@instantiated ' ) !== false ) {
89+ $ this ->generateFunction ($ name , $ function ->getParameters (), "public " );
90+ continue ;
91+ }
92+ }
8593
86- $ index = 0 ;
87- foreach ($ reflection ->getParameters () as $ parameter ) {
88- if ($ index ) {
89- fwrite ($ file , ", " );
94+ $ this ->generateFunction ($ name , $ function ->getParameters (), "public static " );
9095 }
96+ });
97+ });
98+ }
9199
92- fwrite ($ file , "$ " . $ parameter ->getName ());
93- if ($ parameter ->isOptional ()) {
94- fwrite ($ file , " = " . var_export ($ parameter ->getDefaultValue (), true ));
95- }
100+ fclose ($ this ->file );
101+
102+ $ this ->line ("$ fileName has been successfully generated. " , 'info ' );
103+ }
104+
105+ /**
106+ * @param string $name
107+ * @param null|Callable $callback
108+ */
109+ protected function generateNamespace ($ name , $ callback = null )
110+ {
111+ $ this ->writeLine ("namespace " . $ name . " { " , $ this ->indent );
112+
113+ if ($ callback ) {
114+ $ this ->indent ++;
115+ $ callback ();
116+ $ this ->indent --;
117+ }
118+
119+ $ this ->writeLine ("} " , $ this ->indent );
120+ }
121+
122+ /**
123+ * @param string $name
124+ * @param null|Callable $callback
125+ */
126+ protected function generateClass ($ name , $ callback = null )
127+ {
128+ $ this ->writeLine ("class " . $ name . " { " , $ this ->indent );
129+
130+ if ($ callback ) {
131+ $ this ->indent ++;
132+ $ callback ();
133+ $ this ->indent --;
134+ }
135+
136+ $ this ->writeLine ("} " , $ this ->indent );
137+ }
138+
139+ /**
140+ * @param string $name
141+ * @param array $parameters
142+ * @param string $type
143+ * @param null|Callable $callback
144+ */
145+ protected function generateFunction ($ name , $ parameters , $ type = '' , $ callback = null )
146+ {
147+ $ this ->write (($ type ? "$ type " : '' ) . "function $ name( " , $ this ->indent );
96148
97- $ index ++;
98- }
149+ $ index = 0 ;
150+ foreach ($ parameters as $ parameter ) {
151+ if ($ index ) {
152+ $ this ->write (", " );
153+ }
99154
100- fwrite ($ file , ") { } " . PHP_EOL );
155+ $ this ->write ("$ " . $ parameter ->getName ());
156+ if ($ parameter ->isOptional ()) {
157+ $ this ->write (" = " . var_export ($ parameter ->getDefaultValue (), true ));
101158 }
102159
103- fwrite ( $ file , " } " . PHP_EOL . " } " . PHP_EOL ) ;
160+ $ index ++ ;
104161 }
105162
106- fclose ($ file );
163+ $ this ->writeLine (") { " );
164+
165+ if ($ callback ) {
166+ $ callback ();
167+ }
168+
169+ $ this ->writeLine ();
170+ $ this ->writeLine ("} " , $ this ->indent );
171+ }
107172
108- $ this ->line ($ fileName . ' has been successfully generated. ' , 'info ' );
173+ protected function write ($ string , $ indent = 0 )
174+ {
175+ fwrite ($ this ->file , str_repeat (' ' , $ indent ) . $ string );
176+ }
177+
178+ protected function writeLine ($ line = '' , $ indent = 0 )
179+ {
180+ $ this ->write ($ line . PHP_EOL , $ indent );
109181 }
110182}
0 commit comments