Skip to content

Commit

Permalink
Merge pull request #131 from brefphp/custom-vendor-placement
Browse files Browse the repository at this point in the history
Allow to run Laravel under custom vendor location
  • Loading branch information
mnapoli authored Mar 3, 2024
2 parents b77b272 + d27c72b commit 9f18f59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
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

0 comments on commit 9f18f59

Please sign in to comment.