diff --git a/config/config.php b/config/config.php index 32f69c3c3..ca26c999a 100644 --- a/config/config.php +++ b/config/config.php @@ -176,7 +176,7 @@ 'views' => ['path' => 'resources/views', 'generate' => true], // routes/ - 'routes' => ['path' => 'routes', 'generate' => true], + 'routes' => ['path' => 'routes', 'generate' => true, 'web' => true, 'api' => true], // tests/ 'test-feature' => ['path' => 'tests/Feature', 'generate' => true], diff --git a/src/Commands/Make/RouteProviderMakeCommand.php b/src/Commands/Make/RouteProviderMakeCommand.php index 1ed3b901f..3a96e5eb6 100644 --- a/src/Commands/Make/RouteProviderMakeCommand.php +++ b/src/Commands/Make/RouteProviderMakeCommand.php @@ -66,6 +66,9 @@ protected function getTemplateContents() 'API_ROUTES_PATH' => $this->getApiRoutesPath(), 'LOWER_NAME' => $module->getLowerName(), 'KEBAB_NAME' => $module->getKebabName(), + ]))->setRemovalTags(array_filter([ + !$this->shouldGenerateWebRoutes() ? 'WEB_ROUTES' : null, + !$this->shouldGenerateApiRoutes() ? 'API_ROUTES' : null, ]))->render(); } @@ -88,7 +91,17 @@ protected function getDestinationFilePath() $generatorPath = GenerateConfigReader::read('provider'); - return $path.$generatorPath->getPath().'/'.$this->getFileName().'.php'; + return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; + } + + protected function shouldGenerateWebRoutes(): bool + { + return config('modules.paths.generator.routes.web', true); + } + + protected function shouldGenerateApiRoutes(): bool + { + return config('modules.paths.generator.routes.api', true); } /** @@ -96,7 +109,7 @@ protected function getDestinationFilePath() */ protected function getWebRoutesPath() { - return '/'.$this->laravel['modules']->config('stubs.files.routes/web', 'Routes/web.php'); + return '/' . $this->laravel['modules']->config('stubs.files.routes/web', 'Routes/web.php'); } /** @@ -104,7 +117,7 @@ protected function getWebRoutesPath() */ protected function getApiRoutesPath() { - return '/'.$this->laravel['modules']->config('stubs.files.routes/api', 'Routes/api.php'); + return '/' . $this->laravel['modules']->config('stubs.files.routes/api', 'Routes/api.php'); } public function getDefaultNamespace(): string diff --git a/src/Commands/stubs/route-provider.stub b/src/Commands/stubs/route-provider.stub index 937037af7..809e8d142 100644 --- a/src/Commands/stubs/route-provider.stub +++ b/src/Commands/stubs/route-provider.stub @@ -24,11 +24,11 @@ class $CLASS$ extends ServiceProvider */ public function map(): void { - $this->mapApiRoutes(); - $this->mapWebRoutes(); + %START_API_ROUTES%$this->mapApiRoutes();%END_API_ROUTES% + %START_WEB_ROUTES%$this->mapWebRoutes();%END_WEB_ROUTES% } - /** + %START_WEB_ROUTES%/** * Define the "web" routes for the application. * * These routes all receive session state, CSRF protection, etc. @@ -36,9 +36,9 @@ class $CLASS$ extends ServiceProvider protected function mapWebRoutes(): void { Route::middleware('web')->group(module_path($this->name, '$WEB_ROUTES_PATH$')); - } + }%END_WEB_ROUTES% - /** + %START_API_ROUTES%/** * Define the "api" routes for the application. * * These routes are typically stateless. @@ -46,5 +46,5 @@ class $CLASS$ extends ServiceProvider protected function mapApiRoutes(): void { Route::middleware('api')->prefix('api')->name('api.')->group(module_path($this->name, '$API_ROUTES_PATH$')); - } + }%END_API_ROUTES% } diff --git a/src/Support/Stub.php b/src/Support/Stub.php index 6c1f443de..fcb30866c 100644 --- a/src/Support/Stub.php +++ b/src/Support/Stub.php @@ -19,13 +19,19 @@ class Stub */ protected array $replaces = []; + /** + * The removal tags array. + */ + protected array $removalTags = []; + /** * The contructor. */ - public function __construct(string $path, array $replaces = []) + public function __construct(string $path, array $replaces = [], ?array $removalTags = null) { $this->path = $path; $this->replaces = $replaces; + $this->removalTags = $removalTags ?? []; } /** @@ -51,9 +57,9 @@ public function setPath(string $path): self */ public function getPath(): string { - $path = static::getBasePath().$this->path; + $path = static::getBasePath() . $this->path; - return file_exists($path) ? $path : __DIR__.'/../Commands/stubs'.$this->path; + return file_exists($path) ? $path : __DIR__ . '/../Commands/stubs' . $this->path; } /** @@ -80,10 +86,30 @@ public function getContents(): string $contents = file_get_contents($this->getPath()); foreach ($this->replaces as $search => $replace) { - $contents = str_replace('$'.strtoupper($search).'$', $replace, $contents); + $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents); } - return $contents; + foreach ($this->removalTags as $removalTag) { + $contents = $this->removeContentsBetweenTagMarkers($removalTag, $contents); + } + + return $this->cleanUpTagMarkers($contents); + } + + /** + * Remove content between %START_TAG% and %END_TAG% + */ + private function removeContentsBetweenTagMarkers(string $tag, string $contents): string + { + return preg_replace('/%START_' . $tag . '%.*?%END_' . $tag . '%/s', '', $contents); + } + + /** + * Remove leftover %TAG% markers + */ + private function cleanUpTagMarkers(string $contents): string + { + return preg_replace('/%[A-Z0-9_]+%/i', '', $contents); } /** @@ -99,7 +125,7 @@ public function render(): string */ public function saveTo(string $path, string $filename): bool { - return file_put_contents($path.'/'.$filename, $this->getContents()); + return file_put_contents($path . '/' . $filename, $this->getContents()); } /** @@ -112,6 +138,16 @@ public function replace(array $replaces = []): self return $this; } + /** + * Set removal tags array. + */ + public function setRemovalTags(array $tagsArray): self + { + $this->removalTags = $tagsArray; + + return $this; + } + /** * Get replacements. */