1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Telemetry; |
6
|
|
|
|
7
|
|
|
use Spiral\Core\BinderInterface; |
8
|
|
|
use Spiral\Core\Container; |
9
|
|
|
use Spiral\Core\ContainerScope; |
10
|
|
|
use Spiral\Core\InvokerInterface; |
11
|
|
|
use Spiral\Core\Scope; |
12
|
|
|
use Spiral\Core\ScopeInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal The component is under development. |
16
|
|
|
* Something may be changed in the future. We will stable it soon. |
17
|
|
|
* Feedback is welcome {@link https://github.com/spiral/framework/discussions/822}. |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractTracer implements TracerInterface |
20
|
|
|
{ |
21
|
308 |
|
public function __construct( |
22
|
|
|
private readonly ?ScopeInterface $scope = new Container(), |
23
|
308 |
|
) {} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws \Throwable |
27
|
|
|
*/ |
28
|
178 |
|
final protected function runScope(SpanInterface $span, callable $callback): mixed |
29
|
|
|
{ |
30
|
178 |
|
$container = ContainerScope::getContainer(); |
31
|
178 |
|
if ($container === null) { |
32
|
112 |
|
return $this->scope->runScope([ |
|
|
|
|
33
|
112 |
|
SpanInterface::class => $span, |
34
|
112 |
|
TracerInterface::class => $this, |
35
|
112 |
|
], static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback)); |
36
|
|
|
} |
37
|
|
|
|
38
|
126 |
|
if ($container instanceof Container) { |
39
|
126 |
|
$invoker = $container; |
40
|
126 |
|
$binder = $container; |
41
|
126 |
|
$scope = $container; |
42
|
|
|
} else { |
43
|
|
|
/** @var ScopeInterface $scope */ |
44
|
|
|
$scope = $container->get(ScopeInterface::class); |
45
|
|
|
/** @var InvokerInterface $invoker */ |
46
|
|
|
$invoker = $container->get(InvokerInterface::class); |
47
|
|
|
/** @var BinderInterface $binder */ |
48
|
|
|
$binder = $container->get(BinderInterface::class); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
126 |
|
$prevSpan = $container->get(SpanInterface::class); |
53
|
66 |
|
} catch (\Throwable) { |
54
|
66 |
|
$prevSpan = null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @psalm-suppress TooManyArguments */ |
58
|
126 |
|
$binder->bindSingleton(SpanInterface::class, $span, true); |
|
|
|
|
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
/** @psalm-suppress InvalidArgument */ |
62
|
126 |
|
return $prevSpan === null |
63
|
66 |
|
? $scope->runScope( |
64
|
66 |
|
new Scope( |
|
|
|
|
65
|
66 |
|
bindings: [ |
66
|
66 |
|
TracerInterface::class => $this, |
67
|
66 |
|
], |
68
|
66 |
|
), |
69
|
66 |
|
static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback), |
70
|
66 |
|
) |
71
|
119 |
|
: $invoker->invoke($callback); |
72
|
|
|
} finally { |
73
|
126 |
|
$prevSpan === null |
74
|
66 |
|
? $binder->removeBinding(SpanInterface::class) |
75
|
126 |
|
: $binder->bindSingleton(SpanInterface::class, $prevSpan); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.