Skip to content

Commit

Permalink
Add: Logic for registering hidden files in specified environments
Browse files Browse the repository at this point in the history
  • Loading branch information
antoninmasek committed Jun 14, 2024
1 parent 9bfc066 commit 02ecd0e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
11 changes: 11 additions & 0 deletions config/route-directory-macro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
/*
* List of environments in which hidden routes (route files with `.` prefix) should be registered.
*
* This is useful for working on new routes in certain environments (usually local), while
* ensuring they are not registered in any other environment (usually production).
*/
'register_hidden_routes_in_environments' => [],
];
12 changes: 10 additions & 2 deletions src/LaravelRouteDirectoryMacroServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AntoninMasek\LaravelRouteDirectoryMacro;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -12,7 +13,9 @@ class LaravelRouteDirectoryMacroServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package->name('laravel-route-directory-macro');
$package
->name('laravel-route-directory-macro')
->hasConfigFile();
}

public function packageRegistered(): void
Expand All @@ -33,8 +36,13 @@ public function packageRegistered(): void
? base_path($path)
: $path;

$allowHiddenFilesInEnvironments = config('route-directory-macro.register_hidden_routes_in_environments');
$shouldLoadHiddenFiles = ! empty($allowHiddenFilesInEnvironments)
? App::environment($allowHiddenFilesInEnvironments)
: false;

$files = File::exists($path)
? File::allFiles($path, false)
? File::allFiles($path, $shouldLoadHiddenFiles)
: [];

collect($files)
Expand Down
59 changes: 52 additions & 7 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@
->toEqual(['web', 'auth']);
});

it('does not load hidden files', function () {
Route::loadFromDirectory('routes/app');

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->toBeNull('It loaded hidden file');
});

it('only loads php files', function () {
Route::loadFromDirectory('routes/app');

Expand Down Expand Up @@ -140,3 +133,55 @@
Route::loadFromDirectory('routes/public');
expect(RoutesInspector::getRoutesCount())->toEqual(1);
});

it('does not load hidden files by default', function () {
Route::loadFromDirectory('routes/app');
config()->set('route-directory-macro.register_hidden_routes_in_environments', null);

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->toBeNull('It loaded hidden file');
});

it('does not load hidden files when config value is empty', function () {
Route::loadFromDirectory('routes/app');

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->toBeNull('It loaded hidden file');
});

it('does not load hidden files when config value does not contain current environment', function () {
$currentEnvironment = \Illuminate\Support\Facades\App::environment();
config()->set('route-directory-macro.register_hidden_routes_in_environments', [$currentEnvironment.'_test']);

Route::loadFromDirectory('routes/app');

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->toBeNull('It loaded hidden file');
});

it('does load hidden files in specified environment', function () {
$currentEnvironment = \Illuminate\Support\Facades\App::environment();
config()->set('route-directory-macro.register_hidden_routes_in_environments', [$currentEnvironment]);

Route::loadFromDirectory('routes/app');

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->not()->toBeNull(
'It did not load hidden file when explicitly allowed via config',
);
});

it('does load hidden files in specified environment even when more environments are specified', function () {
$currentEnvironment = \Illuminate\Support\Facades\App::environment();
config()->set('route-directory-macro.register_hidden_routes_in_environments', [
'local',
$currentEnvironment,
]);

Route::loadFromDirectory('routes/app');

$secretRoute = RoutesInspector::getRoute('secret.index');
expect($secretRoute)->not()->toBeNull(
'It did not load hidden file when explicitly allowed via config',
);
});

0 comments on commit 02ecd0e

Please sign in to comment.