Skip to content

Commit e719b1d

Browse files
authored
[10.x] Rework Application public path methods (#46015)
* Add usePublicPath helper to Application to match other path helpers * Use new usePublicPath method in tests * Sort paths alphabetically in bindPathsInContainer
1 parent 03bff92 commit e719b1d

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class Application extends Container implements ApplicationContract, CachesConfig
131131
*/
132132
protected $langPath;
133133

134+
/**
135+
* The custom public / web path defined by the developer.
136+
*
137+
* @var string
138+
*/
139+
protected $publicPath;
140+
134141
/**
135142
* The custom storage path defined by the developer.
136143
*
@@ -324,11 +331,10 @@ protected function bindPathsInContainer()
324331
$this->instance('path', $this->path());
325332
$this->instance('path.base', $this->basePath());
326333
$this->instance('path.config', $this->configPath());
327-
$this->instance('path.public', $this->publicPath());
328-
$this->instance('path.storage', $this->storagePath());
329334
$this->instance('path.database', $this->databasePath());
335+
$this->instance('path.public', $this->publicPath());
330336
$this->instance('path.resources', $this->resourcePath());
331-
$this->instance('path.bootstrap', $this->bootstrapPath());
337+
$this->instance('path.storage', $this->storagePath());
332338

333339
$this->useBootstrapPath(value(function () {
334340
return is_dir($directory = $this->basePath('.laravel'))
@@ -481,11 +487,26 @@ public function useLangPath($path)
481487
*/
482488
public function publicPath($path = '')
483489
{
484-
$publicPath = $this->bound('path.public') ? $this->make('path.public') : $this->basePath('public');
490+
$publicPath = $this->publicPath ?: $this->basePath('public');
485491

486492
return $this->joinPaths($publicPath, $path);
487493
}
488494

495+
/**
496+
* Set the public / web directory.
497+
*
498+
* @param string $path
499+
* @return $this
500+
*/
501+
public function usePublicPath($path)
502+
{
503+
$this->publicPath = $path;
504+
505+
$this->instance('path.public', $path);
506+
507+
return $this;
508+
}
509+
489510
/**
490511
* Get the path to the storage directory.
491512
*

tests/Foundation/FoundationHelpersTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ public function testMixHotModuleReloadingWithManifestDirectoryUsesLocalhostIfNoH
205205

206206
protected function makeHotModuleReloadFile($url, $directory = '')
207207
{
208-
app()->singleton('path.public', function () {
209-
return __DIR__;
210-
});
208+
app()->usePublicPath(__DIR__);
211209

212210
$path = public_path(Str::finish($directory, '/').'hot');
213211

@@ -220,9 +218,7 @@ protected function makeHotModuleReloadFile($url, $directory = '')
220218

221219
protected function makeManifest($directory = '')
222220
{
223-
app()->singleton('path.public', function () {
224-
return __DIR__;
225-
});
221+
app()->usePublicPath(__DIR__);
226222

227223
$path = public_path(Str::finish($directory, '/').'mix-manifest.json');
228224

tests/Foundation/FoundationViteTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ public function testCrossoriginAttributeIsInheritedByPreloadTags()
11081108
public function testItCanConfigureTheManifestFilename()
11091109
{
11101110
$buildDir = Str::random();
1111-
app()->singleton('path.public', fn () => __DIR__);
1111+
app()->usePublicPath(__DIR__);
11121112
if (! file_exists(public_path($buildDir))) {
11131113
mkdir(public_path($buildDir));
11141114
}
@@ -1186,7 +1186,7 @@ public function testItOnlyOutputsUniquePreloadTags()
11861186

11871187
protected function makeViteManifest($contents = null, $path = 'build')
11881188
{
1189-
app()->singleton('path.public', fn () => __DIR__);
1189+
app()->usePublicPath(__DIR__);
11901190

11911191
if (! file_exists(public_path($path))) {
11921192
mkdir(public_path($path));
@@ -1247,7 +1247,7 @@ protected function cleanViteManifest($path = 'build')
12471247

12481248
protected function makeViteHotFile($path = null)
12491249
{
1250-
app()->singleton('path.public', fn () => __DIR__);
1250+
app()->usePublicPath(__DIR__);
12511251

12521252
$path ??= public_path('hot');
12531253

tests/Integration/Foundation/FoundationHelpersTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ public function testMixOnlyThrowsAndReportsOneExceptionWhenAssetIsMissingFromMan
117117

118118
protected function makeManifest($directory = '')
119119
{
120-
$this->app->singleton('path.public', function () {
121-
return __DIR__;
122-
});
120+
app()->usePublicPath(__DIR__);
123121

124122
$path = public_path(Str::finish($directory, '/').'mix-manifest.json');
125123

0 commit comments

Comments
 (0)