Skip to content

Commit 86542ac

Browse files
committed
feat(livewire): get topic from livewire component
1 parent cc4c077 commit 86542ac

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

src/TopicDetector.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,27 @@ private function appRunningWithRequest(): bool
4949

5050
private function getTopicByRoute(): string|null
5151
{
52+
$topicId = null;
5253
$route = Route::current();
54+
5355
if (!isset($route->getAction()['controller'])) {
5456
return null;
5557
}
5658

57-
[$controller, $method] = explode('@', $route->getAction()['controller']);
59+
if ($this->isLivewire() && app('livewire')->isLivewireRequest()) {
60+
[$controller, $method] = $this->getMainLivewireClass();
61+
} else {
62+
[$controller, $method] = explode('@', $route->getAction()['controller']);
63+
}
5864

59-
$topicId = $this->getTopicIdByReflection($controller, $method);
60-
if ($topicId === false) {
61-
$topicId = $this->getTopicIdByRegex($controller, $method);
65+
if ($controller !== null) {
66+
$topicId = $this->getTopicIdByReflection($controller, $method);
67+
if ($topicId === false) {
68+
$topicId = $this->getTopicIdByRegex($controller, $method);
69+
}
6270
}
6371

72+
6473
return $topicId;
6574
}
6675

@@ -201,4 +210,43 @@ private function getTopicIdByRegex(string $class, string $method): string|int|nu
201210
return null;
202211
}
203212

204-
}
213+
private function isLivewire(): bool
214+
{
215+
return class_exists(\Livewire\Livewire::class);
216+
}
217+
218+
protected function getMainLivewireClass(): array
219+
{
220+
$class = null;
221+
$method = null;
222+
223+
try {
224+
$request = request();
225+
$payload = $request->all();
226+
227+
if (isset($payload['components'][0])) {
228+
$componentData = $payload['components'][0];
229+
$snapshot = json_decode($componentData['snapshot'], true);
230+
$componentName = $snapshot['memo']['name'] ?? null;
231+
$method = $componentData['calls'][0]['method'] ?? null;
232+
233+
$rootNamespace = config('livewire.class_namespace');
234+
235+
$class = collect(str($componentName)->explode('.'))
236+
->map(fn($segment) => (string)str($segment)->studly())
237+
->join('\\');
238+
239+
if (!empty($rootNamespace)) {
240+
$class = '\\' . $rootNamespace . '\\' . $class;
241+
}
242+
243+
}
244+
} catch (\Throwable $exception) {
245+
//report($exception);
246+
}
247+
248+
return [$class, $method];
249+
250+
}
251+
252+
}

0 commit comments

Comments
 (0)