@@ -94,7 +94,7 @@ public function add_class(string $path, string $class) {
9494 $ provider_content = $ this ->filesystem ->read ( $ provider_path );
9595
9696 $ full_name = $ this ->class_generator ->get_fullname ( $ class );
97- $ id = $ this ->class_generator ->create_id ( $ full_name );
97+ $ id = $ this ->class_generator ->get_fullname ( $ full_name ) . ' ::class ' ;
9898 if (! preg_match ('/\n(?<indents>\s*)(protected\s)?\$provides = \[(?<content>[^]]*[^ ]*) *];/ ' , $ provider_content , $ content ) ) {
9999 return ;
100100 }
@@ -105,7 +105,7 @@ public function add_class(string $path, string $class) {
105105 }
106106
107107 $ indents = $ content ['indents ' ];
108- $ content = $ content ['content ' ] . "$ indents ' " . $ id . "' , \n" ;
108+ $ content = $ content ['content ' ] . "$ indents " . $ id . ", \n" ;
109109 $ provider_content = preg_replace ('/\$provides = \[(?<content>[^\]])*];/ ' , "\$provides = [ $ content$ indents]; " , $ provider_content );
110110
111111 preg_match ( '/\n(?<indents> *)public function register\(\)\s*{ *\n(?<content>[^}]*)}/ ' ,
@@ -119,7 +119,7 @@ public function add_class(string $path, string $class) {
119119 $ content ['content ' ] = rtrim ($ content ['content ' ], " \n" ) . "\n" ;
120120 }
121121
122- $ content = $ content ['content ' ] . "$ indents \$this->getContainer()->share(' " . $ id . "' , $ full_name::class); \n" ;
122+ $ content = $ content ['content ' ] . "$ indents \$this->getContainer()->share( " . $ id . ", $ full_name::class); \n" ;
123123 $ provider_content = preg_replace ( '/public function register\(\)[^}]*{ *\n(?<content>[^}]*)}/ ' , "public function register() \n$ indents{ \n$ content$ indents} " , $ provider_content );
124124
125125 $ this ->filesystem ->update ( $ provider_path , $ provider_content );
@@ -164,18 +164,36 @@ public function register_subscriber(string $id, string $path, SubscriberType $ty
164164 */
165165 protected function add_to_subscriber_method (string $ id , string $ method , string $ content ): string {
166166 if ( ! preg_match ('/\n(?<indents> *)public function ' . $ method . '\(\)[^{]*{(?<content>[^}]*)}/ ' , $ content , $ results ) ) {
167- preg_match ('/(?<content>\$provides = \[[^]]*];)/ ' , $ content , $ results );
168- $ new_content = $ this ->renderer ->apply_template ('serviceprovider/_partials/ ' . $ method . '.php.tpl ' , [
169- 'ids ' => "' $ id', "
170- ]);
171- $ results = $ results ['content ' ] . $ new_content ;
172- return preg_replace ('/(?<content>\$provides = \[[^]]*];) *\n/ ' , $ results , $ content );
167+ if ($ this ->is_autoresolver ($ content )) {
168+ return $ this ->add_method_autoresolve ($ content , $ method , $ id );
169+ }
170+ return $ this ->add_method_vanilla ($ content , $ method , $ id );
173171 }
174172 $ indents = $ results ['indents ' ];
175- $ results = $ results ['content ' ] . " ' " . $ id . "' , \n" ;
173+ $ results = $ results ['content ' ] . " " . $ id . ", \n" ;
176174 return preg_replace ('/public function ' . $ method . '\(\)[^}]*{(?<content>[^}]*)}/ ' , "public function $ method() \n$ indents{ \n$ results$ indents} " , $ content );
177175 }
178176
177+ protected function add_method_autoresolve (string $ content , string $ method , string $ id ) {
178+ if (! preg_match ('/class(?<content>\s*[^{]*{\h*\n)/ ' , $ content , $ results )) {
179+ return $ content ;
180+ }
181+ $ new_content = $ this ->renderer ->apply_template ('serviceprovider/_partials/ ' . $ method . '.php.tpl ' , [
182+ 'ids ' => "$ id, "
183+ ]);
184+ $ results = $ results ['content ' ] . $ new_content ;
185+ return preg_replace ('/class(?<content>\s*[^{]*{\h*\n)/ ' , $ results , $ content );
186+ }
187+
188+ protected function add_method_vanilla (string $ content , string $ method , string $ id ){
189+ preg_match ('/(?<content>\$provides = \[[^]]*];)/ ' , $ content , $ results );
190+ $ new_content = $ this ->renderer ->apply_template ('serviceprovider/_partials/ ' . $ method . '.php.tpl ' , [
191+ 'ids ' => "$ id, "
192+ ]);
193+ $ results = $ results ['content ' ] . $ new_content ;
194+ return preg_replace ('/(?<content>\$provides = \[[^]]*];) *\n/ ' , $ results , $ content );
195+ }
196+
179197 /**
180198 * Instantiate a class inside the service provider.
181199 *
@@ -190,15 +208,20 @@ public function instantiate(string $path, string $class) {
190208 $ provider_content = $ this ->filesystem ->read ( $ provider_path );
191209
192210 $ full_name = $ this ->class_generator ->get_fullname ( $ class );
193- $ id = $ this -> class_generator -> create_id ( $ full_name ) ;
211+ $ id = $ full_name . ' ::class ' ;
194212
195213 preg_match ( '/\n(?<indents> *)public function register\(\)[^}]*\s*{(?<content>[^}]*)}/ ' , $ provider_content , $ content );
196214
197215 $ indents = $ content ['indents ' ];
198- $ content = $ content ['content ' ] . "$ indents \$this->getContainer()->get(' " . $ id . "' ); \n" ;
216+ $ content = $ content ['content ' ] . "$ indents \$this->getContainer()->get( " . $ id . "); \n" ;
199217
200218 $ provider_content = preg_replace ( '/public function register\(\)[^}]*{(?<content>[^}]*)}/ ' , "public function register() \n$ indents{ " ."$ content$ indents} " , $ provider_content );
201219
202220 $ this ->filesystem ->update ( $ provider_path , $ provider_content );
203221 }
222+
223+
224+ public function is_autoresolver (string $ content ) {
225+ return (bool ) preg_match ('/Dependencies \\\\RocketLauncherAutoresolver \\\\ServiceProvider/ ' , $ content );
226+ }
204227}
0 commit comments