|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Snapshots; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Exceptions\ExceptionRendererInterface; |
|
8
|
|
|
use Spiral\Exceptions\Verbosity; |
|
9
|
|
|
use Spiral\Files\Exception\FilesException; |
|
10
|
|
|
use Spiral\Files\FilesInterface; |
|
11
|
|
|
use Symfony\Component\Finder\Finder; |
|
|
|
|
|
|
12
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
13
|
|
|
|
|
14
|
|
|
class FileSnapshot |
|
15
|
|
|
{ |
|
16
|
6 |
|
public function __construct( |
|
17
|
|
|
private string $directory, |
|
18
|
|
|
private int $maxFiles, |
|
19
|
|
|
private Verbosity $verbosity, |
|
20
|
|
|
private ExceptionRendererInterface $renderer, |
|
21
|
|
|
private FilesInterface $files |
|
22
|
|
|
) { |
|
23
|
6 |
|
} |
|
24
|
|
|
|
|
25
|
3 |
|
public function create(\Throwable $e): SnapshotInterface |
|
26
|
|
|
{ |
|
27
|
3 |
|
$snapshot = new Snapshot($this->getID($e), $e); |
|
28
|
|
|
|
|
29
|
3 |
|
$this->saveSnapshot($snapshot); |
|
30
|
3 |
|
$this->rotateSnapshots(); |
|
31
|
|
|
|
|
32
|
3 |
|
return $snapshot; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
protected function saveSnapshot(SnapshotInterface $snapshot): void |
|
36
|
|
|
{ |
|
37
|
3 |
|
$filename = $this->getFilename($snapshot, new \DateTime()); |
|
38
|
|
|
|
|
39
|
3 |
|
$this->files->write( |
|
40
|
3 |
|
$filename, |
|
41
|
3 |
|
$this->renderer->render($snapshot->getException(), $this->verbosity), |
|
42
|
3 |
|
FilesInterface::RUNTIME, |
|
43
|
3 |
|
true |
|
44
|
3 |
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Remove older snapshots. |
|
49
|
|
|
*/ |
|
50
|
3 |
|
protected function rotateSnapshots(): void |
|
51
|
|
|
{ |
|
52
|
3 |
|
$finder = new Finder(); |
|
53
|
3 |
|
$finder->in($this->directory)->sort( |
|
54
|
3 |
|
static fn (SplFileInfo $a, SplFileInfo $b) => $b->getMTime() - $a->getMTime() |
|
55
|
3 |
|
); |
|
56
|
|
|
|
|
57
|
3 |
|
$count = 0; |
|
58
|
3 |
|
foreach ($finder as $file) { |
|
59
|
3 |
|
$count++; |
|
60
|
3 |
|
if ($count > $this->maxFiles) { |
|
61
|
|
|
try { |
|
62
|
1 |
|
$this->files->delete($file->getRealPath()); |
|
63
|
|
|
} catch (FilesException) { |
|
64
|
|
|
// ignore |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
*/ |
|
73
|
3 |
|
protected function getFilename(SnapshotInterface $snapshot, \DateTimeInterface $time): string |
|
74
|
|
|
{ |
|
75
|
3 |
|
return \sprintf( |
|
76
|
3 |
|
'%s/%s-%s.txt', |
|
77
|
3 |
|
$this->directory, |
|
78
|
3 |
|
$time->format('d.m.Y-Hi.s'), |
|
79
|
3 |
|
(new \ReflectionClass($snapshot->getException()))->getShortName() |
|
80
|
3 |
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
protected function getID(\Throwable $e): string |
|
84
|
|
|
{ |
|
85
|
3 |
|
return \md5(\implode('|', [$e->getMessage(), $e->getFile(), $e->getLine()])); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths