Skip to content

Commit 88388d6

Browse files
authored
Merge pull request #5 from chrisdicarlo/allow-nested-config-folders
Allow nested config folders
2 parents 63ea5c6 + 9efe41b commit 88388d6

File tree

10 files changed

+103
-7
lines changed

10 files changed

+103
-7
lines changed

src/LaravelConfigCheckerServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public function configurePackage(Package $package): void
1313
$package->name('laravel-config-checker');
1414

1515
if ($this->app->runningInConsole()) {
16-
$package
17-
->hasCommand(LaravelConfigCheckerCommand::class);
16+
$package->hasCommand(LaravelConfigCheckerCommand::class);
1817
}
1918
}
2019
}

src/Support/FileChecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function check(): Collection
3131
return collect($issues);
3232
}
3333

34-
private function checkForFacadeUsage($content): array
34+
private function checkForFacadeUsage(string $content): array
3535
{
3636
$matches = [];
3737

@@ -56,7 +56,7 @@ private function checkForFacadeUsage($content): array
5656
return $issues;
5757
}
5858

59-
private function checkForHelperUsage($content): array
59+
private function checkForHelperUsage(string $content): array
6060
{
6161
$matches = [];
6262

src/Support/LoadConfigKeys.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __invoke(): array|Collection
2828
return collect($this->configKeys);
2929
}
3030

31-
private function flattenConfig($config, $prefix = '')
31+
private function flattenConfig(array $config, string $prefix = '')
3232
{
3333
foreach ($config as $key => $value) {
3434
$fullKey = $prefix ? "{$prefix}.{$key}" : $key;
@@ -48,11 +48,21 @@ private function loadConfigKeys()
4848
$finder->files()->in($configPath)->name('*.php');
4949

5050
foreach ($finder as $file) {
51-
$this->configKeys[] = basename($file->getFilename(), '.php');
51+
$relativePath = $file->getRelativePathname();
5252

5353
$config = include $file->getRealPath();
5454

55-
$this->flattenConfig($config, basename($file->getFilename(), '.php'));
55+
$key = pathinfo($relativePath, PATHINFO_FILENAME);
56+
57+
$folder = pathinfo($relativePath, PATHINFO_DIRNAME);
58+
59+
if ($folder !== '.') {
60+
$key = str_replace('/', '.', $folder).'.'.$key;
61+
}
62+
63+
$this->configKeys[] = $key;
64+
$config = include $file->getRealPath();
65+
$this->flattenConfig($config, $key);
5666
}
5767
}
5868
}

tests/LoadConfigKeysTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
'app.nested',
1111
'app.nested.key',
1212
]);
13+
14+
$this->nestedFolderConfigKeys = collect([
15+
'app',
16+
'app.valid_key',
17+
'app.nested',
18+
'app.nested.key',
19+
'settings.inside',
20+
'settings.inside.inside_subfolder_valid_key',
21+
'settings.inside.inside_subfolder_nested',
22+
'settings.inside.inside_subfolder_nested.inside_subfolder_nested_key',
23+
]);
1324
});
1425

1526
it('loads config keys from the config directory', function () {
@@ -30,6 +41,16 @@
3041
expect($configKeys->count())->toBeGreaterThan(2);
3142
});
3243

44+
it('loads config keys from the config directory with nested subfolders', function () {
45+
$loadConfigKeys = new LoadConfigKeys(realpath(__DIR__.'/fixtures/subfolders/config'));
46+
47+
$configKeys = $loadConfigKeys();
48+
49+
foreach ($this->nestedFolderConfigKeys as $configKey) {
50+
expect($configKeys)->toContain($configKey);
51+
}
52+
});
53+
3354
it('returns a collection of config keys', function () {
3455
$loadConfigKeys = new LoadConfigKeys(realpath(__DIR__.'/fixtures/base/config'));
3556

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ChrisDiCarlo\LaravelConfigChecker\Tests\fixtures\app;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class TestClassOne
8+
{
9+
public function __construct()
10+
{
11+
config('app.invalid_key');
12+
Config::get('app.invalid_key');
13+
Config::has('app.invalid_key');
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ChrisDiCarlo\LaravelConfigChecker\Tests\fixtures\app;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class TestClassTwo
8+
{
9+
public function __construct()
10+
{
11+
config('app.invalid_key');
12+
Config::get('app.invalid_key');
13+
Config::has('app.invalid_key');
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'valid_key' => 'Laravel Config Checker',
5+
'nested' => [
6+
'key' => 'value',
7+
],
8+
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'inside_subfolder_valid_key' => 'Inside Subfolder Laravel Config Checker',
5+
'inside_subfolder_nested' => [
6+
'inside_subfolder_nested_key' => 'value',
7+
],
8+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
<h1>Test View One</h1>
3+
{{ config('app.invalid_key') }}
4+
{{ Config::get('app.invalid_key') }}
5+
{{ Config::has('app.invalid_key') }}
6+
7+
{{ config('app.valid_key') }}
8+
{{ Config::get('app.valid_key') }}
9+
{{ Config::has('app.valid_key') }}
10+
</div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div>
2+
<h1>Test View Two</h1>
3+
{{ config('app.invalid_key') }}
4+
{{ Config::get('app.invalid_key') }}
5+
{{ Config::has('app.invalid_key') }}
6+
7+
{{ config('app.valid_key') }}
8+
{{ Config::get('app.valid_key') }}
9+
{{ Config::has('app.valid_key') }}
10+
</div>

0 commit comments

Comments
 (0)