Skip to content

Commit 2e2e4c5

Browse files
committed
feat: auto update route service provider
1 parent aab8efd commit 2e2e4c5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"require": {
2727
"php": "^8.0",
2828
"illuminate/support": "^9.0",
29+
"illuminate/console": "^9.0",
2930
"laracasts/flash": "^3.2",
3031
"laravelcollective/html": "^6.3"
3132
},

src/Commands/Publish/GeneratorPublishCommand.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace InfyOm\Generator\Commands\Publish;
44

5+
use Illuminate\Support\Str;
56
use InfyOm\Generator\Utils\FileUtil;
67
use Symfony\Component\Console\Input\InputOption;
78

@@ -28,6 +29,7 @@ class GeneratorPublishCommand extends PublishBaseCommand
2829
*/
2930
public function handle()
3031
{
32+
$this->updateRouteServiceProvider();
3133
$this->publishTestCases();
3234
$this->publishBaseController();
3335
$repositoryPattern = config('infyom.laravel_generator.options.repository_pattern', true);
@@ -60,6 +62,27 @@ private function fillTemplate($templateData)
6062
return $templateData;
6163
}
6264

65+
private function updateRouteServiceProvider()
66+
{
67+
$routeServiceProviderPath = app_path('Providers'.DIRECTORY_SEPARATOR."RouteServiceProvider.php");
68+
69+
if (!file_exists($routeServiceProviderPath)) {
70+
$this->error("Route Service provider not found on $routeServiceProviderPath");
71+
return 1;
72+
}
73+
74+
$fileContent = file_get_contents($routeServiceProviderPath);
75+
76+
$search = "Route::prefix('api')".PHP_EOL.str(" ")->repeat(16)."->middleware('api')";
77+
$beforeContent = str($fileContent)->before($search);
78+
$afterContent = str($fileContent)->after($search);
79+
80+
$finalContent = $beforeContent.$search.PHP_EOL.str(" ")->repeat(16)."->as('api.')".$afterContent;
81+
file_put_contents($routeServiceProviderPath, $finalContent);
82+
83+
return 0;
84+
}
85+
6386
private function publishTestCases()
6487
{
6588
$testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));

0 commit comments

Comments
 (0)