Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
19 changes: 16 additions & 3 deletions src/Commands/Make/RouteProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -88,23 +91,33 @@ 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);
}

/**
* @return mixed
*/
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');
}

/**
* @return mixed
*/
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
Expand Down
12 changes: 6 additions & 6 deletions src/Commands/stubs/route-provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ 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.
*/
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.
*/
protected function mapApiRoutes(): void
{
Route::middleware('api')->prefix('api')->name('api.')->group(module_path($this->name, '$API_ROUTES_PATH$'));
}
}%END_API_ROUTES%
}
48 changes: 42 additions & 6 deletions src/Support/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [];
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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.
*/
Expand Down