Skip to content

Commit

Permalink
Add support for better registering flare in laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Apr 26, 2024
1 parent 3400ed6 commit e04f998
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
76 changes: 63 additions & 13 deletions src/Commands/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Spatie\LaravelIgnition\Commands;

use Closure;
use Composer\InstalledVersions;
use Exception;
use Illuminate\Config\Repository;
use Illuminate\Console\Command;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Foundation\Exceptions\ReportableHandler;
use Illuminate\Log\LogManager;
use Laravel\SerializableClosure\Support\ReflectionClosure;
use ReflectionException;
use ReflectionNamedType;
use ReflectionProperty;
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\Http\Exceptions\BadResponseCode;
use Spatie\LaravelIgnition\Support\LaravelFlare;

class TestCommand extends Command
{
Expand Down Expand Up @@ -44,24 +52,26 @@ protected function checkFlareKey(): self

public function checkFlareLogger(): self
{
$defaultLogChannel = $this->config->get('logging.default');
if (! $this->hasFlareReportableCallback()) {
$defaultLogChannel = $this->config->get('logging.default');

$activeStack = $this->config->get("logging.channels.{$defaultLogChannel}");
$activeStack = $this->config->get("logging.channels.{$defaultLogChannel}");

if (is_null($activeStack)) {
$this->info("❌ The default logging channel `{$defaultLogChannel}` is not configured in the `logging` config file");
}
if (is_null($activeStack)) {
$this->info("❌ The default logging channel `{$defaultLogChannel}` is not configured in the `logging` config file");
}

if (! isset($activeStack['channels']) || ! in_array('flare', $activeStack['channels'])) {
$this->info("❌ The logging channel `{$defaultLogChannel}` does not contain the 'flare' channel");
}
if (! isset($activeStack['channels']) || ! in_array('flare', $activeStack['channels'])) {
$this->info("❌ The logging channel `{$defaultLogChannel}` does not contain the 'flare' channel");
}

if (is_null($this->config->get('logging.channels.flare'))) {
$this->info('❌ There is no logging channel named `flare` in the `logging` config file');
}
if (is_null($this->config->get('logging.channels.flare'))) {
$this->info('❌ There is no logging channel named `flare` in the `logging` config file');
}

if ($this->config->get('logging.channels.flare.driver') !== 'flare') {
$this->info('❌ The `flare` logging channel defined in the `logging` config file is not set to `flare`.');
if ($this->config->get('logging.channels.flare.driver') !== 'flare') {
$this->info('❌ The `flare` logging channel defined in the `logging` config file is not set to `flare`.');
}
}

if ($this->config->get('ignition.with_stack_frame_arguments') && ini_get('zend.exception_ignore_args')) {
Expand All @@ -73,6 +83,46 @@ public function checkFlareLogger(): self
return $this;
}

protected function hasFlareReportableCallback(): bool
{
if (version_compare(app()->version(), '11.0.0', '<')) {
return false;
}

try {
$handler = app(Handler::class);

$reflection = new ReflectionProperty($handler, 'reportCallbacks');
$reportCallbacks = $reflection->getValue($handler);

foreach ($reportCallbacks as $reportCallback) {
if (! $reportCallback instanceof ReportableHandler) {
continue;
}

$reflection = new ReflectionProperty($reportCallback, 'callback');
$callback = $reflection->getValue($reportCallback);

if (! $callback instanceof Closure) {
return false;
}

$reflection = new ReflectionClosure($callback);
$closureReturnTypeReflection = $reflection->getReturnType();

if(! $closureReturnTypeReflection instanceof ReflectionNamedType){
return false;
}

return $closureReturnTypeReflection->getName() === Flare::class;
}
} catch (ReflectionException $exception) {
return false;
}

return false;
}

protected function sendTestException(): void
{
$testException = new Exception('This is an exception to test if the integration with Flare works.');
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Flare extends Facade
{
protected static function getFacadeAccessor()
{
return \Spatie\FlareClient\Flare::class;
return \Spatie\LaravelIgnition\Support\LaravelFlare::class;
}

public static function sentReports(): SentReports
Expand Down
17 changes: 17 additions & 0 deletions src/Support/LaravelFlare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\LaravelIgnition\Support;

use Illuminate\Foundation\Configuration\Exceptions;
use Spatie\FlareClient\Flare;
use Throwable;

class LaravelFlare extends Flare
{
public static function handles(Exceptions $exceptions): void
{
$exceptions->reportable(static function (Throwable $exception): Flare {
return app(Flare::class)->report($exception);
});
}
}

0 comments on commit e04f998

Please sign in to comment.