Skip to content

Commit

Permalink
feat: auto update route service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulgolakiya committed Feb 22, 2022
1 parent aab8efd commit 2e2e4c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require": {
"php": "^8.0",
"illuminate/support": "^9.0",
"illuminate/console": "^9.0",
"laracasts/flash": "^3.2",
"laravelcollective/html": "^6.3"
},
Expand Down
23 changes: 23 additions & 0 deletions src/Commands/Publish/GeneratorPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace InfyOm\Generator\Commands\Publish;

use Illuminate\Support\Str;
use InfyOm\Generator\Utils\FileUtil;
use Symfony\Component\Console\Input\InputOption;

Expand All @@ -28,6 +29,7 @@ class GeneratorPublishCommand extends PublishBaseCommand
*/
public function handle()
{
$this->updateRouteServiceProvider();
$this->publishTestCases();
$this->publishBaseController();
$repositoryPattern = config('infyom.laravel_generator.options.repository_pattern', true);
Expand Down Expand Up @@ -60,6 +62,27 @@ private function fillTemplate($templateData)
return $templateData;
}

private function updateRouteServiceProvider()
{
$routeServiceProviderPath = app_path('Providers'.DIRECTORY_SEPARATOR."RouteServiceProvider.php");

if (!file_exists($routeServiceProviderPath)) {
$this->error("Route Service provider not found on $routeServiceProviderPath");
return 1;
}

$fileContent = file_get_contents($routeServiceProviderPath);

$search = "Route::prefix('api')".PHP_EOL.str(" ")->repeat(16)."->middleware('api')";
$beforeContent = str($fileContent)->before($search);
$afterContent = str($fileContent)->after($search);

$finalContent = $beforeContent.$search.PHP_EOL.str(" ")->repeat(16)."->as('api.')".$afterContent;
file_put_contents($routeServiceProviderPath, $finalContent);

return 0;
}

private function publishTestCases()
{
$testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));
Expand Down

0 comments on commit 2e2e4c5

Please sign in to comment.