File tree Expand file tree Collapse file tree 2 files changed +62
-3
lines changed
src/Illuminate/Foundation Expand file tree Collapse file tree 2 files changed +62
-3
lines changed Original file line number Diff line number Diff line change 3
3
namespace Illuminate \Foundation ;
4
4
5
5
use Closure ;
6
+ use Composer \Autoload \ClassLoader ;
6
7
use Illuminate \Container \Container ;
7
8
use Illuminate \Contracts \Console \Kernel as ConsoleKernelContract ;
8
9
use Illuminate \Contracts \Foundation \Application as ApplicationContract ;
@@ -224,16 +225,30 @@ public function __construct($basePath = null)
224
225
*/
225
226
public static function configure (string $ baseDirectory = null )
226
227
{
227
- $ baseDirectory = $ ENV ['APP_BASE_PATH ' ] ?? ($ baseDirectory ?: dirname (dirname (
228
- debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 1 )[0 ]['file ' ]
229
- )));
228
+ $ baseDirectory = match (true ) {
229
+ is_string ($ baseDirectory ) => $ baseDirectory ,
230
+ default => static ::inferBaseDirectory (),
231
+ };
230
232
231
233
return (new Configuration \ApplicationBuilder (new static ($ baseDirectory )))
232
234
->withKernels ()
233
235
->withEvents ()
234
236
->withCommands ();
235
237
}
236
238
239
+ /**
240
+ * Infer the application's base directory from the environment.
241
+ *
242
+ * @return string
243
+ */
244
+ public static function inferBaseDirectory ()
245
+ {
246
+ return match (true ) {
247
+ isset ($ _ENV ['APP_BASE_PATH ' ]) => $ _ENV ['APP_BASE_PATH ' ],
248
+ default => dirname (array_keys (ClassLoader::getRegisteredLoaders ())[0 ]),
249
+ };
250
+ }
251
+
237
252
/**
238
253
* Get the version number of the application.
239
254
*
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Illuminate \Tests \Foundation ;
4
+
5
+ use Illuminate \Foundation \Application ;
6
+ use Mockery as m ;
7
+ use PHPUnit \Framework \TestCase ;
8
+
9
+ class FoundationApplicationBuilderTest extends TestCase
10
+ {
11
+ protected function tearDown (): void
12
+ {
13
+ m::close ();
14
+
15
+ unset($ _ENV ['APP_BASE_PATH ' ]);
16
+
17
+ parent ::tearDown ();
18
+ }
19
+
20
+ public function testBaseDirectoryWithArg ()
21
+ {
22
+ $ _ENV ['APP_BASE_PATH ' ] = __DIR__ .'/as-env ' ;
23
+
24
+ $ app = Application::configure (__DIR__ .'/as-arg ' )->create ();
25
+
26
+ $ this ->assertSame (__DIR__ .'/as-arg ' , $ app ->basePath ());
27
+ }
28
+
29
+ public function testBaseDirectoryWithEnv ()
30
+ {
31
+ $ _ENV ['APP_BASE_PATH ' ] = __DIR__ .'/as-env ' ;
32
+
33
+ $ app = Application::configure ()->create ();
34
+
35
+ $ this ->assertSame (__DIR__ .'/as-env ' , $ app ->basePath ());
36
+ }
37
+
38
+ public function testBaseDirectoryWithComposer ()
39
+ {
40
+ $ app = Application::configure ()->create ();
41
+
42
+ $ this ->assertSame (dirname (__DIR__ , 2 ), $ app ->basePath ());
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments