Skip to content

Commit

Permalink
Drop support for Laravel 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Oct 6, 2022
1 parent eb20d7a commit 22f6554
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 344 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -80,7 +80,7 @@ try {

- Laravel `<= 4.2.x` is supported until `0.8.x`
- Laravel `<= 5.7.x` on PHP `<= 7.0` is supported until `0.11.x`
- Laravel `>= 5.x.x` on PHP `>= 7.1` is supported in all versions
- Laravel `>= 5.x.x` on PHP `>= 7.1` is supported until `2.14.x`
- Laravel `>= 6.x.x` on PHP `>= 7.2` is supported starting from `1.2.0`
- Laravel `>= 7.x.x` on PHP `>= 7.2` is supported starting from `1.7.0`
- Laravel `>= 8.x.x` on PHP `>= 7.3` is supported starting from `1.9.0`
Expand Down
226 changes: 48 additions & 178 deletions src/Sentry/Laravel/EventHandler.php
Expand Up @@ -3,24 +3,19 @@
namespace Sentry\Laravel;

use Exception;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Auth\Events as AuthEvents;
use Illuminate\Console\Events as ConsoleEvents;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\Events as DatabaseEvents;
use Illuminate\Http\Request;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Log\Events as LogEvents;
use Illuminate\Queue\Events as QueueEvents;
use Illuminate\Queue\QueueManager;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Route;
use Illuminate\Routing\Events as RoutingEvents;
use Laravel\Octane\Events as Octane;
use Laravel\Sanctum\Events as Sanctum;
use RuntimeException;
Expand All @@ -36,17 +31,11 @@ class EventHandler
* @var array
*/
protected static $eventHandlerMap = [
'router.matched' => 'routerMatched', // Until Laravel 5.1
'Illuminate\Routing\Events\RouteMatched' => 'routeMatched', // Since Laravel 5.2

'illuminate.query' => 'query', // Until Laravel 5.1
'Illuminate\Database\Events\QueryExecuted' => 'queryExecuted', // Since Laravel 5.2

'illuminate.log' => 'log', // Until Laravel 5.3
'Illuminate\Log\Events\MessageLogged' => 'messageLogged', // Since Laravel 5.4

'Illuminate\Console\Events\CommandStarting' => 'commandStarting', // Since Laravel 5.5
'Illuminate\Console\Events\CommandFinished' => 'commandFinished', // Since Laravel 5.5
LogEvents\MessageLogged::class => 'messageLogged',
RoutingEvents\RouteMatched::class => 'routeMatched',
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
ConsoleEvents\CommandStarting::class => 'commandStarting',
ConsoleEvents\CommandFinished::class => 'commandFinished',
];

/**
Expand All @@ -55,8 +44,8 @@ class EventHandler
* @var array
*/
protected static $authEventHandlerMap = [
'Illuminate\Auth\Events\Authenticated' => 'authenticated', // Since Laravel 5.3
'Laravel\Sanctum\Events\TokenAuthenticated' => 'sanctumTokenAuthenticated', // Since Sanctum 2.13
AuthEvents\Authenticated::class => 'authenticated',
Sanctum\TokenAuthenticated::class => 'sanctumTokenAuthenticated', // Since Sanctum 2.13
];

/**
Expand All @@ -65,10 +54,10 @@ class EventHandler
* @var array
*/
protected static $queueEventHandlerMap = [
'Illuminate\Queue\Events\JobProcessed' => 'queueJobProcessed', // Since Laravel 5.2
'Illuminate\Queue\Events\JobProcessing' => 'queueJobProcessing', // Since Laravel 5.2
'Illuminate\Queue\Events\WorkerStopping' => 'queueWorkerStopping', // Since Laravel 5.2
'Illuminate\Queue\Events\JobExceptionOccurred' => 'queueJobExceptionOccurred', // Since Laravel 5.2
QueueEvents\JobProcessed::class => 'queueJobProcessed',
QueueEvents\JobProcessing::class => 'queueJobProcessing',
QueueEvents\WorkerStopping::class => 'queueWorkerStopping',
QueueEvents\JobExceptionOccurred::class => 'queueJobExceptionOccurred',
];

/**
Expand All @@ -77,17 +66,17 @@ class EventHandler
* @var array
*/
protected static $octaneEventHandlerMap = [
'Laravel\Octane\Events\RequestReceived' => 'octaneRequestReceived',
'Laravel\Octane\Events\RequestTerminated' => 'octaneRequestTerminated',
Octane\RequestReceived::class => 'octaneRequestReceived',
Octane\RequestTerminated::class => 'octaneRequestTerminated',

'Laravel\Octane\Events\TaskReceived' => 'octaneTaskReceived',
'Laravel\Octane\Events\TaskTerminated' => 'octaneTaskTerminated',
Octane\TaskReceived::class => 'octaneTaskReceived',
Octane\TaskTerminated::class => 'octaneTaskTerminated',

'Laravel\Octane\Events\TickReceived' => 'octaneTickReceived',
'Laravel\Octane\Events\TickTerminated' => 'octaneTickTerminated',
Octane\TickReceived::class => 'octaneTickReceived',
Octane\TickTerminated::class => 'octaneTickTerminated',

'Laravel\Octane\Events\WorkerErrorOccurred' => 'octaneWorkerErrorOccurred',
'Laravel\Octane\Events\WorkerStopping' => 'octaneWorkerStopping',
Octane\WorkerErrorOccurred::class => 'octaneWorkerErrorOccurred',
Octane\WorkerStopping::class => 'octaneWorkerStopping',
];

/**
Expand Down Expand Up @@ -261,7 +250,7 @@ public function subscribeQueueEvents(QueueManager $queue): void
* @param string $method
* @param array $arguments
*/
public function __call($method, $arguments)
public function __call(string $method, array $arguments)
{
$handlerMethod = "{$method}Handler";

Expand All @@ -276,14 +265,9 @@ public function __call($method, $arguments)
}
}

/**
* Until Laravel 5.1
*
* @param Route $route
*/
protected function routerMatchedHandler(Route $route)
protected function routeMatchedHandler(RoutingEvents\RouteMatched $match): void
{
[$routeName] = Integration::extractNameAndSourceForRoute($route);
[$routeName] = Integration::extractNameAndSourceForRoute($match->route);

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Expand All @@ -295,106 +279,32 @@ protected function routerMatchedHandler(Route $route)
Integration::setTransaction($routeName);
}

/**
* Since Laravel 5.2
*
* @param \Illuminate\Routing\Events\RouteMatched $match
*/
protected function routeMatchedHandler(RouteMatched $match)
{
$this->routerMatchedHandler($match->route);
}

/**
* Until Laravel 5.1
*
* @param string $query
* @param array $bindings
* @param int $time
* @param string $connectionName
*/
protected function queryHandler($query, $bindings, $time, $connectionName)
protected function queryExecutedHandler(DatabaseEvents\QueryExecuted $query): void
{
if (!$this->recordSqlQueries) {
return;
}

$this->addQueryBreadcrumb($query, $bindings, $time, $connectionName);
}
$data = ['connectionName' => $query->connectionName];

/**
* Since Laravel 5.2
*
* @param \Illuminate\Database\Events\QueryExecuted $query
*/
protected function queryExecutedHandler(QueryExecuted $query)
{
if (!$this->recordSqlQueries) {
return;
}

$this->addQueryBreadcrumb($query->sql, $query->bindings, $query->time, $query->connectionName);
}

/**
* Helper to add an query breadcrumb.
*
* @param string $query
* @param array $bindings
* @param float|null $time
* @param string $connectionName
*/
private function addQueryBreadcrumb($query, $bindings, $time, $connectionName)
{
$data = ['connectionName' => $connectionName];

if ($time !== null) {
$data['executionTimeMs'] = $time;
if ($query->time !== null) {
$data['executionTimeMs'] = $query->time;
}

if ($this->recordSqlBindings) {
$data['bindings'] = $bindings;
$data['bindings'] = $query->bindings;
}

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'sql.query',
$query,
$query->sql,
$data
));
}

/**
* Until Laravel 5.3
*
* @param string $level
* @param string $message
* @param array|null $context
*/
protected function logHandler($level, $message, $context)
{
$this->addLogBreadcrumb($level, $message, is_array($context) ? $context : []);
}

/**
* Since Laravel 5.4
*
* @param \Illuminate\Log\Events\MessageLogged $logEntry
*/
protected function messageLoggedHandler(MessageLogged $logEntry)
{
$this->addLogBreadcrumb($logEntry->level, $logEntry->message, $logEntry->context);
}

/**
* Helper to add an log breadcrumb.
*
* @param string $level Log level. May be any standard.
* @param string|null $message Log message.
* @param array $context Log context.
*/
private function addLogBreadcrumb(string $level, ?string $message, array $context = []): void
protected function messageLoggedHandler(LogEvents\MessageLogged $logEntry): void
{
if (!$this->recordLaravelLogs) {
return;
Expand All @@ -403,35 +313,25 @@ private function addLogBreadcrumb(string $level, ?string $message, array $contex
// A log message with `null` as value will not be recorded by Laravel
// however empty strings are logged so we mimick that behaviour to
// check for `null` to stay consistent with how Laravel logs it
if ($message === null) {
if ($logEntry->message === null) {
return;
}

Integration::addBreadcrumb(new Breadcrumb(
$this->logLevelToBreadcrumbLevel($level),
$this->logLevelToBreadcrumbLevel($logEntry->level),
Breadcrumb::TYPE_DEFAULT,
'log.' . $level,
$message,
$context
'log.' . $logEntry->level,
$logEntry->message,
$logEntry->context
));
}

/**
* Since Laravel 5.3
*
* @param \Illuminate\Auth\Events\Authenticated $event
*/
protected function authenticatedHandler(Authenticated $event)
protected function authenticatedHandler(AuthEvents\Authenticated $event): void
{
$this->configureUserScopeFromModel($event->user);
}

/**
* Since Sanctum 2.13
*
* @param \Laravel\Sanctum\Events\TokenAuthenticated $event
*/
protected function sanctumTokenAuthenticatedHandler(Sanctum\TokenAuthenticated $event)
protected function sanctumTokenAuthenticatedHandler(Sanctum\TokenAuthenticated $event): void
{
$this->configureUserScopeFromModel($event->token->tokenable);
}
Expand Down Expand Up @@ -478,12 +378,7 @@ private function configureUserScopeFromModel($authUser): void
});
}

/**
* Since Laravel 5.2
*
* @param \Illuminate\Queue\Events\JobProcessing $event
*/
protected function queueJobProcessingHandler(JobProcessing $event)
protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event): void
{
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScope);

Expand Down Expand Up @@ -516,43 +411,23 @@ protected function queueJobProcessingHandler(JobProcessing $event)
));
}

/**
* Since Laravel 5.2
*
* @param \Illuminate\Queue\Events\JobExceptionOccurred $event
*/
protected function queueJobExceptionOccurredHandler(JobExceptionOccurred $event)
protected function queueJobExceptionOccurredHandler(QueueEvents\JobExceptionOccurred $event): void
{
$this->afterTaskWithinLongRunningProcess();
}

/**
* Since Laravel 5.2
*
* @param \Illuminate\Queue\Events\JobProcessed $event
*/
protected function queueJobProcessedHandler(JobProcessed $event)
protected function queueJobProcessedHandler(QueueEvents\JobProcessed $event): void
{
$this->afterTaskWithinLongRunningProcess();
}

/**
* Since Laravel 5.2
*
* @param \Illuminate\Queue\Events\WorkerStopping $event
*/
protected function queueWorkerStoppingHandler(WorkerStopping $event)
protected function queueWorkerStoppingHandler(QueueEvents\WorkerStopping $event): void
{
// Flush any and all events that were possibly generated by queue jobs
Integration::flushEvents();
}

/**
* Since Laravel 5.5
*
* @param \Illuminate\Console\Events\CommandStarting $event
*/
protected function commandStartingHandler(CommandStarting $event)
protected function commandStartingHandler(ConsoleEvents\CommandStarting $event): void
{
if ($event->command) {
Integration::configureScope(static function (Scope $scope) use ($event): void {
Expand All @@ -575,12 +450,7 @@ protected function commandStartingHandler(CommandStarting $event)
}
}

/**
* Since Laravel 5.5
*
* @param \Illuminate\Console\Events\CommandFinished $event
*/
protected function commandFinishedHandler(CommandFinished $event)
protected function commandFinishedHandler(ConsoleEvents\CommandFinished $event): void
{
if ($this->recordCommandInfo) {
Integration::addBreadcrumb(new Breadcrumb(
Expand Down

0 comments on commit 22f6554

Please sign in to comment.