Skip to content

Commit 20e6dc1

Browse files
[10.x] Config path customization (#46053)
* Config path customization Signed-off-by: Roger Vilà <[email protected]> * add tests Signed-off-by: Roger Vilà <[email protected]> * apply styleci fixes Signed-off-by: Roger Vilà <[email protected]> * formatting --------- Signed-off-by: Roger Vilà <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 47190cc commit 20e6dc1

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ class Application extends Container implements ApplicationContract, CachesConfig
117117
*/
118118
protected $appPath;
119119

120+
/**
121+
* The custom configuration path defined by the developer.
122+
*
123+
* @var string
124+
*/
125+
protected $configPath;
126+
120127
/**
121128
* The custom database path defined by the developer.
122129
*
@@ -357,9 +364,7 @@ protected function bindPathsInContainer()
357364
*/
358365
public function path($path = '')
359366
{
360-
$appPath = $this->appPath ?: $this->basePath('app');
361-
362-
return $this->joinPaths($appPath, $path);
367+
return $this->joinPaths($this->appPath ?: $this->basePath('app'), $path);
363368
}
364369

365370
/**
@@ -422,7 +427,22 @@ public function useBootstrapPath($path)
422427
*/
423428
public function configPath($path = '')
424429
{
425-
return $this->joinPaths($this->basePath('config'), $path);
430+
return $this->joinPaths($this->configPath ?: $this->basePath('config'), $path);
431+
}
432+
433+
/**
434+
* Set the configuration directory.
435+
*
436+
* @param string $path
437+
* @return $this
438+
*/
439+
public function useConfigPath($path)
440+
{
441+
$this->configPath = $path;
442+
443+
$this->instance('path.config', $path);
444+
445+
return $this;
426446
}
427447

428448
/**
@@ -433,9 +453,7 @@ public function configPath($path = '')
433453
*/
434454
public function databasePath($path = '')
435455
{
436-
$databasePath = $this->databasePath ?: $this->basePath('database');
437-
438-
return $this->joinPaths($databasePath, $path);
456+
return $this->joinPaths($this->databasePath ?: $this->basePath('database'), $path);
439457
}
440458

441459
/**
@@ -487,9 +505,7 @@ public function useLangPath($path)
487505
*/
488506
public function publicPath($path = '')
489507
{
490-
$publicPath = $this->publicPath ?: $this->basePath('public');
491-
492-
return $this->joinPaths($publicPath, $path);
508+
return $this->joinPaths($this->publicPath ?: $this->basePath('public'), $path);
493509
}
494510

495511
/**
@@ -515,9 +531,7 @@ public function usePublicPath($path)
515531
*/
516532
public function storagePath($path = '')
517533
{
518-
$storagePath = $this->storagePath ?: $this->basePath('storage');
519-
520-
return $this->joinPaths($storagePath, $path);
534+
return $this->joinPaths($this->storagePath ?: $this->basePath('storage'), $path);
521535
}
522536

523537
/**

tests/Foundation/FoundationApplicationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,16 @@ public function testMacroable(): void
516516

517517
$this->assertFalse($app->foo());
518518
}
519+
520+
/** @test */
521+
public function testUseConfigPath(): void
522+
{
523+
$app = new Application;
524+
$app->useConfigPath(__DIR__.'/fixtures/config');
525+
$app->bootstrapWith([\Illuminate\Foundation\Bootstrap\LoadConfiguration::class]);
526+
527+
$this->assertSame('bar', $app->make('config')->get('app.foo'));
528+
}
519529
}
520530

521531
class ApplicationBasicServiceProviderStub extends ServiceProvider
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'foo' => 'bar',
5+
];

0 commit comments

Comments
 (0)