forked from wire-elements/livewire-strict
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportSignedActions.php
More file actions
77 lines (60 loc) · 2.25 KB
/
SupportSignedActions.php
File metadata and controls
77 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace WireElements\LivewireStrict\Features\SupportSignedActions;
use Livewire\ComponentHook;
use Livewire\Features\SupportAttributes\AttributeLevel;
use WireElements\LivewireStrict\Attributes\Signed;
use WireElements\LivewireStrict\Features\Concerns\MatchesComponents;
use WireElements\LivewireStrict\Features\SupportSignedActions\Exceptions\InvalidSignedActionException;
class SupportSignedActions extends ComponentHook
{
use MatchesComponents;
public static bool $enabled = false;
public static array $components = [];
public static ?int $ttl = null;
public function call($method, $params, $returnEarly, $metadata = null, $componentContext = null)
{
if (self::$enabled === false) {
return;
}
if (! $this->checkIsRequired()) {
return;
}
if ($method === '__callSigned') {
$this->handleSignedCall($params, $returnEarly);
return;
}
if ($this->methodIsSigned($method)) {
throw new InvalidSignedActionException($method);
}
}
private function handleSignedCall(array $params, callable $returnEarly): void
{
throw_if(
method_exists($this->component, '__callSigned'),
\LogicException::class,
'Component ['.$this->component::class.'] defines a __callSigned method, which collides with the internal signed-action hook.'
);
throw_unless(
isset($params[0]) && is_string($params[0]),
InvalidSignedActionException::class,
'__callSigned',
);
$payload = SignedPayload::verify($params[0], $this->component);
throw_unless(
$this->methodIsSigned($payload->method),
InvalidSignedActionException::class,
$payload->method
);
$returnEarly(
$this->component->{$payload->method}(...$payload->params)
);
}
private function methodIsSigned(string $method): bool
{
return $this->component
->getAttributes()
->whereInstanceOf(Signed::class)
->filter(fn (Signed $attribute) => $attribute->getLevel() === AttributeLevel::METHOD && $attribute->getName() === $method)
->isNotEmpty();
}
}