Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to run Laravel under custom vendor location #131

Merged
merged 22 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/HandlerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,7 @@ private function laravel(): Application
return $this->laravel;
}

$bootstrapFile = getcwd() . '/bootstrap/app.php';

if (! file_exists($bootstrapFile)) {
throw new RuntimeException(
"Unable to locate `{$bootstrapFile}`: Bref tried to load that file to retrieve the Laravel app"
);
}
$bootstrapFile = $this->resolveBootstrapLocation();

$this->laravel = require $bootstrapFile;

Expand All @@ -96,4 +90,23 @@ private function laravel(): Application

return $this->laravel;
}

private function resolveBootstrapLocation(): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems to duplicate the other resolveBootstrapLocation() function, but with a slightly different implementation (but same name), any reason for that?

Can we merge both functions into one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just addressed this

{
$bootstrapFile = getcwd() . '/bootstrap/app.php';

if (file_exists($bootstrapFile)) {
return $bootstrapFile;
}

// Going up 4 directories will get us from `vendor/brefphp/laravel-bridge/src`
// to the Laravel root folder so we can navigate to `bootstrap/app.php`
if (file_exists(__DIR__ . '/../../../../bootstrap/app.php')) {
tillkruss marked this conversation as resolved.
Show resolved Hide resolved
return realpath(__DIR__ . '/../../../../bootstrap/app.php');
}

throw new RuntimeException(
"Unable to locate `{$bootstrapFile}`: Bref tried to load that file to retrieve the Laravel app"
);
}
}
18 changes: 16 additions & 2 deletions src/bref-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Bref\LaravelBridge\StorageDirectories;

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

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

$defaultConfigCachePath = $_SERVER['LAMBDA_TASK_ROOT'] . '/bootstrap/cache/config.php';
$defaultConfigCachePath = $laravelHome . '/bootstrap/cache/config.php';

if (file_exists($defaultConfigCachePath)) {
return;
Expand All @@ -43,8 +45,20 @@
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 artisan config:cache 1>&2');
passthru("php {$laravelHome}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__ . '/../../../../');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @alustau this is breaking the invocation of php artisan config:cache because realpath() returns the folder without the last /. We need to concatenate / at the end of the string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inline comment is not marked as "obsolete", is this resolved? I didn't see any new commits related to this, not sure what to do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had forgotten to merge the PR that was addressing this 😅

}