1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/twig-extra-bundle |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocubom\TwigExtraBundle\DataCollector; |
13
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\Svg\Loader\LoaderInterface; |
15
|
|
|
use Ocubom\Twig\Extension\Svg\Svg; |
16
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
17
|
|
|
|
18
|
|
|
class SvgTraceableLoader implements LoaderInterface |
19
|
|
|
{ |
20
|
|
|
private LoaderInterface $inner; |
21
|
|
|
private ?Stopwatch $watch; |
22
|
|
|
private \ReflectionClass $class; |
23
|
|
|
private array $traces = []; |
24
|
|
|
|
25
|
|
|
public function __construct(LoaderInterface $inner, Stopwatch $watch = null) |
26
|
|
|
{ |
27
|
|
|
$this->inner = $inner; |
28
|
|
|
$this->class = new \ReflectionClass($this->inner); |
29
|
|
|
|
30
|
|
|
$this->watch = $watch; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function resolve(string $ident, iterable $options = null): Svg |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
|
|
if ($this->watch instanceof Stopwatch) { |
37
|
|
|
$this->watch->start($this->class->getShortName(), 'ocubom.svg_loader'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->traces[$idx = count($this->traces)] = [ |
41
|
|
|
// Metadata |
42
|
|
|
'ts0' => \DateTimeImmutable::createFromFormat('0.u00 U', microtime()), |
43
|
|
|
'ts1' => \DateTimeImmutable::createFromFormat('0.u00 U', microtime()), |
44
|
|
|
// Loader |
45
|
|
|
'loader_class' => $this->class->getName(), |
46
|
|
|
// Search input |
47
|
|
|
'search_ident' => $ident, |
48
|
|
|
'search_options' => $options, |
49
|
|
|
// Search output |
50
|
|
|
'value' => null, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
return $this->traces[$idx]['value'] = $this->inner->resolve($ident, $options); |
54
|
|
|
} catch (\Throwable $err) { |
55
|
|
|
// Register error as "returned" value |
56
|
|
|
$idx = count($this->traces) - 1; |
57
|
|
|
$this->traces[$idx]['value'] = $err; |
58
|
|
|
|
59
|
|
|
throw $err; |
60
|
|
|
} finally { |
61
|
|
|
// Update finish timestamp |
62
|
|
|
$idx = count($this->traces) - 1; |
63
|
|
|
$this->traces[$idx]['ts1'] = \DateTimeImmutable::createFromFormat('0.u00 U', microtime()); |
64
|
|
|
|
65
|
|
|
if ($this->watch instanceof Stopwatch) { |
66
|
|
|
$this->watch->stop($this->class->getShortName()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getTraces(): array |
72
|
|
|
{ |
73
|
|
|
return $this->traces; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function resetTraces(): void |
77
|
|
|
{ |
78
|
|
|
$this->traces = []; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getTracedClassName(): string |
82
|
|
|
{ |
83
|
|
|
return $this->class->getName(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|