Skip to content

Commit

Permalink
Add slash in the end of laravel home path when use realpath
Browse files Browse the repository at this point in the history
  • Loading branch information
alustau committed Nov 13, 2023
1 parent cf8341c commit ad2c698
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/LaravelRootResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bref\LaravelBridge;

class LaravelRootResolver
{
/**
* Resolve the Laravel root path.
*
* @return string
*/
public static function resolvePath(): string
{
$laravelHome = $_SERVER['LAMBDA_TASK_ROOT'] . '/bootstrap/cache/config.php';

// If the config cache exists, we can assume that the Laravel root path is the same as the Lambda task root
if (file_exists($laravelHome)) {
return $_SERVER['LAMBDA_TASK_ROOT'];
}

// the fallback is going up 4 directories will get us from `vendor/brefphp/laravel-bridge/src` to the Laravel root folder
return realpath(__DIR__ . '/../../../../') . '/';
}
}
20 changes: 4 additions & 16 deletions src/bref-init.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

use Bref\Bref;

use Bref\LaravelBridge\LaravelRootResolver;
use Bref\LaravelBridge\HandlerResolver;
use Bref\LaravelBridge\MaintenanceMode;
use Bref\LaravelBridge\StorageDirectories;

Bref::beforeStartup(static function () {
$laravelHome = resolveBootstrapLocation();
$laravelRoot = LaravelRootResolver::resolvePath();

if (! defined('STDERR')) {
define('STDERR', fopen('php://stderr', 'wb'));
Expand All @@ -28,7 +28,7 @@
return;
}

$defaultConfigCachePath = $laravelHome . '/bootstrap/cache/config.php';
$defaultConfigCachePath = $laravelRoot . '/bootstrap/cache/config.php';

if (file_exists($defaultConfigCachePath)) {
return;
Expand All @@ -45,20 +45,8 @@
fwrite(STDERR, "Running 'php artisan config:cache' to cache the Laravel configuration\n");

// 1>&2 redirects the output to STDERR to avoid messing up HTTP responses with FPM
passthru("php {$laravelHome}artisan config:cache 1>&2");
passthru("php {$laravelRoot}artisan config:cache 1>&2");
}
});

Bref::setContainer(static fn() => new HandlerResolver);

function resolveBootstrapLocation(): string
{
$laravelHome = $_SERVER['LAMBDA_TASK_ROOT'] . '/bootstrap/cache/config.php';

if (file_exists($laravelHome)) {
return $_SERVER['LAMBDA_TASK_ROOT'];
}

// Going up 4 directories will get us from `vendor/brefphp/laravel-bridge/src` to the Laravel root folder
return realpath(__DIR__ . '/../../../../');
}

0 comments on commit ad2c698

Please sign in to comment.