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 all 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
8 changes: 1 addition & 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 = LaravelPathFinder::app();

$this->laravel = require $bootstrapFile;

Expand Down
43 changes: 43 additions & 0 deletions src/LaravelPathFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Bref\LaravelBridge;

use RuntimeException;

final class LaravelPathFinder
{
/**
* Resolve the Laravel root path. Path is always returned without a leading slash.
*
* @return string
*/
public static function root(): string
{
$app = $_SERVER['LAMBDA_TASK_ROOT'] . '/bootstrap/app.php';

// If the config cache exists, we can assume that the Laravel root path is the same as the Lambda task root.
// This may not be needed as the fallback "should" work in all cases, but we're keeping
// this as it's safer to keep 100% compatibility with the original implementation.
if (file_exists($app)) {
return $_SERVER['LAMBDA_TASK_ROOT'];
}

// If Laravel is installed on a sub-folder, we can navigate from where we are
// (inside composer) to the root of Laravel.
// We will go up 4 directories, represented by `vendor/brefphp/laravel-bridge/src`.
return realpath(__DIR__ . '/../../../../');
}

public static function app(): string
{
$bootstrapFile = self::root() . '/bootstrap/app.php';

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

throw new RuntimeException(
"Unable to locate `{$bootstrapFile}`: Bref tried to load that file to retrieve the Laravel app"
);
}
}
8 changes: 5 additions & 3 deletions src/bref-init.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Bref\Bref;

use Bref\LaravelBridge\LaravelPathFinder;
use Bref\LaravelBridge\HandlerResolver;
use Bref\LaravelBridge\MaintenanceMode;
use Bref\LaravelBridge\StorageDirectories;
Expand All @@ -11,6 +11,8 @@
define('STDERR', fopen('php://stderr', 'wb'));
}

$laravelRoot = LaravelPathFinder::root();

StorageDirectories::create();

MaintenanceMode::setUp();
Expand All @@ -26,7 +28,7 @@
return;
}

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

if (file_exists($defaultConfigCachePath)) {
return;
Expand All @@ -47,7 +49,7 @@
$outputDestination = '1>&2';
}

passthru("php artisan config:cache {$outputDestination}");
passthru("php $laravelRoot/artisan config:cache {$outputDestination}");
}
});

Expand Down
Loading