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

[2.x] Adds conditional log wrapper #152

Merged
merged 11 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Install Octane
run: |
composer require laravel/octane --dev
composer require laravel/octane --with-all-dependencies --dev

- name: Execute type checking
run: vendor/bin/phpstan
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
"autoload": {
"psr-4": {
"Laravel\\Vapor\\": "src"
}
},
"files": [
"src/debug.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
11 changes: 5 additions & 6 deletions src/Runtime/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Environment
/**
* Create a new environment manager instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct(Application $app)
Expand Down Expand Up @@ -94,7 +93,7 @@ public function decryptEnvironment()

$this->loadEnvironment();
} catch (Throwable $e) {
fwrite(STDERR, $e->getMessage().PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug($e->getMessage());
}
}

Expand All @@ -110,13 +109,13 @@ public function canBeDecrypted()
}

if (version_compare($this->app->version(), '9.37.0', '<')) {
fwrite(STDERR, 'Decrypt command not available.'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Decrypt command not available.');

return false;
}

if (! file_exists($this->app->basePath($this->encryptedFile))) {
fwrite(STDERR, 'Encrypted environment file not found.'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Encrypted environment file not found.');

return false;
}
Expand Down Expand Up @@ -144,7 +143,7 @@ public function copyEncryptedFile()
*/
public function decryptFile()
{
fwrite(STDERR, 'Decrypting environment variables.'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Decrypting environment variables.');

$this->console()->call('env:decrypt', ['--env' => $this->environment, '--path' => $this->writePath]);
}
Expand All @@ -156,7 +155,7 @@ public function decryptFile()
*/
public function loadEnvironment()
{
fwrite(STDERR, 'Loading decrypted environment variables.'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Loading decrypted environment variables.');

Dotenv::createMutable($this->writePath, $this->environmentFile)->load();
}
Expand Down
17 changes: 8 additions & 9 deletions src/Runtime/Fpm/Fpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
class Fpm
{
public const SOCKET = '/tmp/.vapor/php-fpm.sock';

public const CONFIG = '/tmp/.vapor/php-fpm.conf';

public const PID_FILE = '/tmp/.vapor/php-fpm.pid';

/**
Expand Down Expand Up @@ -61,8 +63,6 @@ class Fpm
*
* @param \Hoa\Socket\Client $handler
* @param \Hoa\FastCGI\SocketConnections\UnixDomainSocket $socketConnection
* @param string $handler
* @param array $serverVariables
* @return void
*/
public function __construct(Client $client, UnixDomainSocket $socketConnection, string $handler, array $serverVariables = [])
Expand All @@ -77,7 +77,6 @@ public function __construct(Client $client, UnixDomainSocket $socketConnection,
* Boot FPM with the given handler.
*
* @param string $handler
* @param array $serverVariables
* @return static
*/
public static function boot($handler, array $serverVariables = [])
Expand Down Expand Up @@ -112,7 +111,7 @@ public function start()
$this->killExistingFpm();
}

fwrite(STDERR, 'Ensuring ready to start FPM'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Ensuring ready to start FPM');

$this->ensureReadyToStart();

Expand All @@ -124,12 +123,12 @@ public function start()
self::CONFIG,
]);

fwrite(STDERR, 'Starting FPM Process...'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Starting FPM Process...');

$this->fpm->disableOutput()
->setTimeout(null)
->start(function ($type, $output) {
fwrite(STDERR, $output.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug($output);
});

$this->ensureFpmHasStarted();
Expand Down Expand Up @@ -163,7 +162,7 @@ protected function ensureReadyToStart()
public function handle($request)
{
return (new FpmApplication($this->client, $this->socketConnection))
->handle($request);
->handle($request);
}

/**
Expand Down Expand Up @@ -216,7 +215,7 @@ public function ensureRunning()
throw new Exception('PHP-FPM has stopped unexpectedly.');
}
} catch (Throwable $e) {
echo $e->getMessage().PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug($e->getMessage());

exit(1);
}
Expand All @@ -241,7 +240,7 @@ public function stop()
*/
protected function killExistingFpm()
{
fwrite(STDERR, 'Killing existing FPM'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Killing existing FPM');

if (! file_exists(static::PID_FILE)) {
return unlink(static::SOCKET);
Expand Down
3 changes: 1 addition & 2 deletions src/Runtime/Handlers/CliHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CliHandler implements LambdaEventHandler
/**
* Handle an incoming Lambda event.
*
* @param array $event
* @return \Laravel\Vapor\Contracts\LambdaResponse
*/
public function handle(array $event)
Expand All @@ -32,7 +31,7 @@ public function handle(array $event)
if (! Str::containsAll($line, ['{"message":', '"level":'])) {
$output .= $line;
} else {
echo $line.PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug($line);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/LambdaContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function terminateIfInvocationLimitHasBeenReached($invocations, $i
Octane::terminate();
}

echo 'Killing container. Container has processed '.$invocationLimit.' invocations. ('.$_ENV['AWS_REQUEST_ID'].')'.PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug('Killing container. Container has processed '.$invocationLimit.' invocations. ('.$_ENV['AWS_REQUEST_ID'].')');

exit(0);
}
Expand Down
7 changes: 2 additions & 5 deletions src/Runtime/LambdaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public static function fromEnvironmentVariable()
/**
* Handle the next Lambda invocation.
*
* @param callable $callback
* @return void
*/
public function nextInvocation(callable $callback)
Expand All @@ -61,8 +60,6 @@ public function nextInvocation(callable $callback)
/**
* Inform Lambda of an invocation failure.
*
* @param string $invocationId
* @param \Throwable $error
* @return void
*/
public function handleException(string $invocationId, Throwable $error)
Expand All @@ -71,8 +68,8 @@ public function handleException(string $invocationId, Throwable $error)
? 'Uncaught '.get_class($error).': '.$error->getMessage()
: $error->getMessage();

fwrite(STDERR, sprintf(
"Fatal error: %s in %s:%d\nStack trace:\n%s".PHP_EOL,
function_exists('__vapor_debug') && __vapor_debug(sprintf(
"Fatal error: %s in %s:%d\nStack trace:\n%s",
$errorMessage,
$error->getFile(),
$error->getLine(),
Expand Down
17 changes: 2 additions & 15 deletions src/Runtime/Octane/Octane.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,6 @@ public static function terminate()

/**
* Marshal the given Octane request context into an Laravel foundation request.
*
* @param \Laravel\Octane\RequestContext $context
* @return array
*/
public function marshalRequest(RequestContext $context): array
{
Expand All @@ -241,10 +238,6 @@ public function marshalRequest(RequestContext $context): array

/**
* Stores the response in the instance.
*
* @param \Laravel\Octane\RequestContext $context
* @param \Laravel\Octane\OctaneResponse $response
* @return void
*/
public function respond(RequestContext $context, OctaneResponse $response): void
{
Expand All @@ -253,12 +246,6 @@ public function respond(RequestContext $context, OctaneResponse $response): void

/**
* Send an error message to the server.
*
* @param \Throwable $e
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Http\Request $request
* @param \Laravel\Octane\RequestContext $context
* @return void
*/
public function error(Throwable $e, Application $app, Request $request, RequestContext $context): void
{
Expand All @@ -267,8 +254,8 @@ public function error(Throwable $e, Application $app, Request $request, RequestC
$app[ExceptionHandler::class]->render($request, $e)
);
} catch (Throwable $throwable) {
fwrite(STDERR, $throwable->getMessage());
fwrite(STDERR, $e->getMessage());
function_exists('__vapor_debug') && __vapor_debug($throwable->getMessage());
function_exists('__vapor_debug') && __vapor_debug($e->getMessage());

static::$response = new OctaneResponse(
new \Illuminate\Http\Response('', 500)
Expand Down
4 changes: 0 additions & 4 deletions src/Runtime/Secrets.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public static function addToEnvironment($path, $parameters, $file)

return tap(static::all($path, (array) $parameters), function ($variables) {
foreach ($variables as $key => $value) {
echo "Injecting secret [{$key}] into runtime.".PHP_EOL;

$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
Expand All @@ -34,7 +32,6 @@ public static function addToEnvironment($path, $parameters, $file)
* Get all of the secret parameters (AWS SSM) at the given path.
*
* @param string $path
* @param array $parameters
* @return array
*/
public static function all($path, array $parameters = [])
Expand Down Expand Up @@ -65,7 +62,6 @@ public static function all($path, array $parameters = [])
/**
* Parse the secret names and values into an array.
*
* @param array $secrets
* @return array
*/
protected static function parseSecrets(array $secrets)
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/StorageDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function create()

foreach ($directories as $directory) {
if (! is_dir($directory)) {
echo "Creating storage directory: $directory".PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug("Creating storage directory: $directory");

mkdir($directory, 0755, true);
}
Expand Down
10 changes: 10 additions & 0 deletions src/debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

if (! function_exists('__vapor_debug')) {
function __vapor_debug($message)
{
if (isset($_ENV['VAPOR_DEBUG']) && $_ENV['VAPOR_DEBUG'] === 'true') {
fwrite(STDERR, $message.PHP_EOL);
}
}
}
8 changes: 4 additions & 4 deletions stubs/cliRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@
file_put_contents($app->storagePath().'/framework/down', '[]');
}

echo 'Caching Laravel configuration'.PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug('Caching Laravel configuration');

try {
$app->make(ConsoleKernelContract::class)->call('config:cache');
} catch (Throwable $e) {
echo 'Failing caching Laravel configuration: '.$e->getMessage().PHP_EOL;
function_exists('__vapor_debug') && __vapor_debug('Failing caching Laravel configuration: '.$e->getMessage());
}

/*
Expand All @@ -86,8 +86,8 @@
while (true) {
$lambdaRuntime->nextInvocation(function ($invocationId, $event) {
return CliHandlerFactory::make($event)
->handle($event)
->toApiGatewayFormat();
->handle($event)
->toApiGatewayFormat();
});

LambdaContainer::terminateIfInvocationLimitHasBeenReached(
Expand Down
10 changes: 5 additions & 5 deletions stubs/fpmRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
|
*/

fwrite(STDERR, 'Preparing to add secrets to runtime'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Preparing to add secrets to runtime');

Secrets::addToEnvironment(
$_ENV['VAPOR_SSM_PATH'],
Expand Down Expand Up @@ -58,7 +58,7 @@

$app->useStoragePath(StorageDirectories::PATH);

fwrite(STDERR, 'Caching Laravel configuration'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Caching Laravel configuration');

$app->make(ConsoleKernelContract::class)->call('config:cache');

Expand All @@ -73,7 +73,7 @@
|
*/

fwrite(STDERR, 'Preparing to boot FPM'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Preparing to boot FPM');

$fpm = Fpm::boot(
__DIR__.'/httpHandler.php'
Expand All @@ -97,8 +97,8 @@
while (true) {
$lambdaRuntime->nextInvocation(function ($invocationId, $event) {
return FpmHttpHandlerFactory::make($event)
->handle($event)
->toApiGatewayFormat();
->handle($event)
->toApiGatewayFormat();
});

$fpm->ensureRunning();
Expand Down
10 changes: 5 additions & 5 deletions stubs/octaneRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
|
*/

fwrite(STDERR, 'Preparing to add secrets to runtime'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Preparing to add secrets to runtime');

Secrets::addToEnvironment(
$_ENV['VAPOR_SSM_PATH'],
Expand Down Expand Up @@ -58,7 +58,7 @@

$app->useStoragePath(StorageDirectories::PATH);

fwrite(STDERR, 'Caching Laravel configuration'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Caching Laravel configuration');

$app->make(ConsoleKernelContract::class)->call('config:cache');

Expand All @@ -72,7 +72,7 @@
| for Lambda invocations to be received for this Vapor application.
|
*/
fwrite(STDERR, 'Preparing to boot Octane'.PHP_EOL);
function_exists('__vapor_debug') && __vapor_debug('Preparing to boot Octane');

Octane::boot(
__DIR__,
Expand All @@ -98,8 +98,8 @@
while (true) {
$lambdaRuntime->nextInvocation(function ($invocationId, $event) {
return OctaneHttpHandlerFactory::make($event)
->handle($event)
->toApiGatewayFormat();
->handle($event)
->toApiGatewayFormat();
});

LambdaContainer::terminateIfInvocationLimitHasBeenReached(
Expand Down
Loading