Skip to content

Instantly share code, notes, and snippets.

@phroggyy
Created October 26, 2019 15:37
Show Gist options
  • Save phroggyy/38877e4dc6739a01b4433e7a9c32cfeb to your computer and use it in GitHub Desktop.
Save phroggyy/38877e4dc6739a01b4433e7a9c32cfeb to your computer and use it in GitHub Desktop.
<?php
namespace App\Jobs;
use Decahedron\StickyLogging\StickyContext;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class LogStackVariables implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var string
*/
private $tempFileName;
/**
* @var Request
*/
private $capturedRequest;
/**
* @var null
*/
private $requestId;
/**
* Create a new job instance.
*
* @param string $tempFileName
* @param Exception $exception
* @param Request $capturedRequest
*/
public function __construct(string $tempFileName, Request $capturedRequest, $requestId = null)
{
$this->tempFileName = $tempFileName;
$this->capturedRequest = $capturedRequest;
$this->requestId = $requestId;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Kernel $kernel)
{
if ($this->requestId) {
StickyContext::add('request', $this->requestId);
}
require_once $this->tempFileName;
$this->capturedRequest['__debug_mode__'] = true;
$kernel->handle($this->capturedRequest);
unlink($this->tempFileName);
Log::debug('ran debug request', ['vars' => StickyContext::stack('vars')->all()]);
}
}
$request = $this->container['request'];
if ($this->container->runningInConsole() || $request['__debug_mode__']) {
return;
}
$lines = file($exception->getFile());
$prepend = "\\Decahedron\\StickyLogging\\StickyContext::stack('vars')->add('{$exception->getFile()}:{$exception->getLine()}', get_defined_vars());";
$lines[$exception->getLine()-1] = $prepend . $lines[$exception->getLine()-1];
$newFile = substr($exception->getFile(), 0, -4) . Str::random() . '.php';
file_put_contents($newFile, implode("", $lines));
dispatch(new LogStackVariables($newFile, Request::capture()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment