Skip to content

Commit 97ebfe8

Browse files
committed
feat: make sure to publish laravel 12 proper files
1 parent d0a670c commit 97ebfe8

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

src/Commands/SetupCommand.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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,58 @@ 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+
return;
65+
}
66+
67+
$content = file_get_contents($providersPath);
68+
69+
// Check if the provider is already registered
70+
if (str_contains($content, $providerClass)) {
71+
$this->line('RestifyServiceProvider already registered.');
72+
return;
73+
}
74+
75+
// Find the return statement
76+
$pattern = '/return\s+(\[.*?\]);/s';
77+
if (preg_match($pattern, $content, $matches)) {
78+
$providersArray = $matches[1];
79+
80+
// Remove the closing bracket
81+
$providersArrayWithoutClosing = rtrim(trim($providersArray), ']');
82+
83+
// Add our provider and close the array
84+
$newProvidersArray = $providersArrayWithoutClosing;
85+
86+
// If the array is not empty and doesn't end with a comma, add a comma
87+
if (!empty($providersArrayWithoutClosing) && !str_ends_with(trim($providersArrayWithoutClosing), ',')) {
88+
$newProvidersArray .= ',';
89+
}
90+
91+
$newProvidersArray .= "\n {$providerClass},\n]";
92+
93+
// Replace the old array with the new one
94+
$newContent = preg_replace($pattern, "return {$newProvidersArray};", $content);
95+
96+
file_put_contents($providersPath, $newContent);
97+
98+
$this->line('RestifyServiceProvider registered in bootstrap/providers.php');
99+
} else {
100+
$this->error('Could not find the providers array in bootstrap/providers.php');
101+
}
60102
}
61103

62104
/**

0 commit comments

Comments
 (0)