@@ -14,6 +14,9 @@ class SetupCommand extends Command
1414
1515 public function handle ()
1616 {
17+ $ this ->comment ('Installing Laravel API... ' );
18+ $ this ->call ('install:api ' );
19+
1720 $ this ->comment ('Publishing Restify Service Provider... ' );
1821 $ this ->callSilent ('vendor:publish ' , ['--tag ' => 'restify-provider ' ]);
1922
@@ -44,19 +47,60 @@ public function handle()
4447 }
4548
4649 /**
47- * Register the Restify service provider in the application configuration file.
50+ * Register the Restify service provider in the bootstrap/providers.php file.
4851 *
4952 * @return void
5053 */
5154 protected function registerRestifyServiceProvider ()
5255 {
5356 $ namespace = Str::replaceLast ('\\' , '' , $ this ->laravel ->getNamespace ());
57+ $ providerClass = "{$ namespace }\\Providers \\RestifyServiceProvider::class " ;
5458
55- file_put_contents (config_path ('app.php ' ), str_replace (
56- "{$ namespace }\\Providers\EventServiceProvider::class, " .PHP_EOL ,
57- "{$ namespace }\\Providers\EventServiceProvider::class, " .PHP_EOL ." {$ namespace }\Providers\RestifyServiceProvider::class, " .PHP_EOL ,
58- file_get_contents (config_path ('app.php ' ))
59- ));
59+ $ providersPath = base_path ('bootstrap/providers.php ' );
60+
61+ // Check if the providers.php file exists
62+ if (! file_exists ($ providersPath )) {
63+ $ this ->error ('bootstrap/providers.php file not found. Make sure you are using Laravel 12. ' );
64+
65+ return ;
66+ }
67+
68+ $ content = file_get_contents ($ providersPath );
69+
70+ // Check if the provider is already registered
71+ if (str_contains ($ content , $ providerClass )) {
72+ $ this ->line ('RestifyServiceProvider already registered. ' );
73+
74+ return ;
75+ }
76+
77+ // Find the return statement
78+ $ pattern = '/return\s+(\[.*?\]);/s ' ;
79+ if (preg_match ($ pattern , $ content , $ matches )) {
80+ $ providersArray = $ matches [1 ];
81+
82+ // Remove the closing bracket
83+ $ providersArrayWithoutClosing = rtrim (trim ($ providersArray ), '] ' );
84+
85+ // Add our provider and close the array
86+ $ newProvidersArray = $ providersArrayWithoutClosing ;
87+
88+ // If the array is not empty and doesn't end with a comma, add a comma
89+ if (! empty ($ providersArrayWithoutClosing ) && ! str_ends_with (trim ($ providersArrayWithoutClosing ), ', ' )) {
90+ $ newProvidersArray .= ', ' ;
91+ }
92+
93+ $ newProvidersArray .= "\n {$ providerClass }, \n] " ;
94+
95+ // Replace the old array with the new one
96+ $ newContent = preg_replace ($ pattern , "return {$ newProvidersArray }; " , $ content );
97+
98+ file_put_contents ($ providersPath , $ newContent );
99+
100+ $ this ->line ('RestifyServiceProvider registered in bootstrap/providers.php ' );
101+ } else {
102+ $ this ->error ('Could not find the providers array in bootstrap/providers.php ' );
103+ }
60104 }
61105
62106 /**
0 commit comments