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

Added support for Livewire 3 #798

Merged
merged 14 commits into from
Nov 16, 2023
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ jobs:
- name: Install Composer dependencies
run: |
# friendsofphp/php-cs-fixer: No need for this package to run phpunit and it conflicts with older Laravel versions
# livewire/livewire: Only supported on Laravel 7.0 and above
# laravel/folio: Only supported on PHP 8.1 + Laravel 10.0 and above
composer remove friendsofphp/php-cs-fixer laravel/folio --dev --no-interaction --no-update
composer remove friendsofphp/php-cs-fixer livewire/livewire laravel/folio --dev --no-interaction --no-update

# Require the correct versions we want to run phpunit for
composer require \
Expand Down
1 change: 1 addition & 0 deletions composer.json
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do this mostly for the types, so we can get autocomplete working and make phpstan a little happier without many exclusions.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"require-dev": {
"phpunit/phpunit": "^8.4 | ^9.3",
"laravel/framework": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
"livewire/livewire": "^2.0 | ^3.0",
"orchestra/testbench": "^4.7 | ^5.1 | ^6.0 | ^7.0 | ^8.0",
"friendsofphp/php-cs-fixer": "^3.11",
"mockery/mockery": "^1.3",
Expand Down
25 changes: 0 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,6 @@ parameters:
count: 1
path: src/Sentry/Laravel/EventHandler.php

-
message: "#^Parameter \\$component of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:handleComponentBoot\\(\\) has invalid type Livewire\\\\Component\\.$#"
count: 1
path: src/Sentry/Laravel/Features/LivewirePackageIntegration.php

-
message: "#^Parameter \\$component of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:handleComponentBooted\\(\\) has invalid type Livewire\\\\Component\\.$#"
count: 1
path: src/Sentry/Laravel/Features/LivewirePackageIntegration.php

-
message: "#^Parameter \\$component of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:handleComponentDehydrate\\(\\) has invalid type Livewire\\\\Component\\.$#"
count: 1
path: src/Sentry/Laravel/Features/LivewirePackageIntegration.php

-
message: "#^Parameter \\$component of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:handleComponentMount\\(\\) has invalid type Livewire\\\\Component\\.$#"
count: 1
path: src/Sentry/Laravel/Features/LivewirePackageIntegration.php

-
message: "#^Parameter \\$livewireManager of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:onBoot\\(\\) has invalid type Livewire\\\\LivewireManager\\.$#"
count: 1
path: src/Sentry/Laravel/Features/LivewirePackageIntegration.php

-
message: "#^Parameter \\$request of method Sentry\\\\Laravel\\\\Features\\\\LivewirePackageIntegration\\:\\:handleComponentBooted\\(\\) has invalid type Livewire\\\\Request\\.$#"
count: 1
Expand Down
90 changes: 82 additions & 8 deletions src/Sentry/Laravel/Features/LivewirePackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sentry\Laravel\Features;

use Livewire\Component;
use Livewire\EventBus;
use Livewire\LivewireManager;
use Livewire\Request;
use Sentry\Breadcrumb;
Expand All @@ -16,8 +17,6 @@
{
private const FEATURE_KEY = 'livewire';

private const COMPONENT_SPAN_OP = 'ui.livewire.component';

/** @var array<Span> */
private $spanStack = [];

Expand All @@ -32,11 +31,56 @@
}

public function onBoot(LivewireManager $livewireManager): void
{
if (class_exists(EventBus::class)) {
cleptric marked this conversation as resolved.
Show resolved Hide resolved
$this->registerLivewireThreeEventListeners($livewireManager);

return;
}

$this->registerLivewireTwoEventListeners($livewireManager);
}

Check warning on line 42 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L41-L42

Added lines #L41 - L42 were not covered by tests

private function registerLivewireThreeEventListeners(LivewireManager $livewireManager): void
{
$livewireManager->listen('mount', function ($component, array $data) {
if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) {
$this->handleComponentBoot($component);

Check warning on line 48 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L47-L48

Added lines #L47 - L48 were not covered by tests
}

if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) {
$this->handleComponentMount($component, $data);

Check warning on line 52 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L51-L52

Added lines #L51 - L52 were not covered by tests
}
});

$livewireManager->listen('hydrate', function ($component, array $data) {
if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) {
$this->handleComponentBoot($component);

Check warning on line 58 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L57-L58

Added lines #L57 - L58 were not covered by tests
}

if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) {
$this->handleComponentHydrate($component, $data);

Check warning on line 62 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L61-L62

Added lines #L61 - L62 were not covered by tests
}
});

if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) {
$livewireManager->listen('dehydrate', [$this, 'handleComponentDehydrate']);
}

if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) {
$livewireManager->listen('call', [$this, 'handleComponentCall']);
}
}

Check warning on line 73 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L73

Added line #L73 was not covered by tests

private function registerLivewireTwoEventListeners(LivewireManager $livewireManager): void

Check warning on line 75 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L75

Added line #L75 was not covered by tests
{
$livewireManager->listen('component.booted', [$this, 'handleComponentBooted']);

if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) {
$livewireManager->listen('component.boot', [$this, 'handleComponentBoot']);
$livewireManager->listen('component.boot', function ($component) {
$this->handleComponentBoot($component);
});

Check warning on line 82 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L80-L82

Added lines #L80 - L82 were not covered by tests

$livewireManager->listen('component.dehydrate', [$this, 'handleComponentDehydrate']);
}

Expand All @@ -45,8 +89,23 @@
}
}

public function handleComponentBoot(Component $component): void
public function handleComponentCall(Component $component, string $method, array $data): void

Check warning on line 92 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L92

Added line #L92 was not covered by tests
{
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'livewire',
"Component call: {$component->getName()}::{$method}",
$data
));
}

Check warning on line 101 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L94-L101

Added lines #L94 - L101 were not covered by tests

public function handleComponentBoot(Component $component, ?string $method = null): void

Check warning on line 103 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L103

Added line #L103 was not covered by tests
{
if ($this->isLivewireRequest()) {
$this->updateTransactionName($component->getName());

Check warning on line 106 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L105-L106

Added lines #L105 - L106 were not covered by tests
}

$currentSpan = SentrySdk::getCurrentHub()->getSpan();

if ($currentSpan === null) {
Expand All @@ -56,8 +115,12 @@
$this->spanStack[] = $currentSpan;

$context = new SpanContext;
$context->setOp(self::COMPONENT_SPAN_OP);
$context->setDescription($component->getName());
$context->setOp('ui.livewire.component');
$context->setDescription(
empty($method)
? $component->getName()
: "{$component->getName()}::{$method}"
);

Check warning on line 123 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L118-L123

Added lines #L118 - L123 were not covered by tests

$componentSpan = $currentSpan->startChild($context);

Expand Down Expand Up @@ -92,10 +155,21 @@
}

if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) {
$this->updateTransactionName($component::getName());
$this->updateTransactionName($component->getName());

Check warning on line 158 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L158

Added line #L158 was not covered by tests
}
}

public function handleComponentHydrate(Component $component, array $data): void

Check warning on line 162 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L162

Added line #L162 was not covered by tests
{
Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'livewire',
"Component hydrate: {$component->getName()}",
$data
));
}

Check warning on line 171 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L164-L171

Added lines #L164 - L171 were not covered by tests

public function handleComponentDehydrate(Component $component): void
{
$currentSpan = SentrySdk::getCurrentHub()->getSpan();
Expand Down Expand Up @@ -137,7 +211,7 @@
return false;
}

return $request->header('x-livewire') === 'true';
return $request->hasHeader('x-livewire');

Check warning on line 214 in src/Sentry/Laravel/Features/LivewirePackageIntegration.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Laravel/Features/LivewirePackageIntegration.php#L214

Added line #L214 was not covered by tests
} catch (\Throwable $e) {
// If the request cannot be resolved, it's probably not a Livewire request.
return false;
Expand Down