1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spiral\Snapshotter\FileHandler\Services; |
4
|
|
|
|
5
|
|
|
use Spiral\Core\Service; |
6
|
|
|
use Spiral\Debug\Configs\SnapshotConfig; |
7
|
|
|
use Spiral\Files\FileManager; |
8
|
|
|
use Spiral\Snapshotter\FileHandler\Entities\FileSnapshot; |
9
|
|
|
|
10
|
|
|
class SnapshotService extends Service |
11
|
|
|
{ |
12
|
|
|
/** @var SnapshotConfig */ |
13
|
|
|
private $config; |
14
|
|
|
|
15
|
|
|
/** @var FileManager */ |
16
|
|
|
private $files; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SnapshotService constructor. |
20
|
|
|
* |
21
|
|
|
* @param SnapshotConfig $config |
22
|
|
|
* @param FileManager $files |
23
|
|
|
*/ |
24
|
|
|
public function __construct(SnapshotConfig $config, FileManager $files) |
25
|
|
|
{ |
26
|
|
|
$this->config = $config; |
27
|
|
|
$this->files = $files; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get snapshots. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getSnapshots() |
36
|
|
|
{ |
37
|
|
|
$order = []; |
38
|
|
|
$snapshots = []; |
39
|
|
|
|
40
|
|
|
if (!$this->files->exists($this->config->reportingDirectory())) { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($this->files->getFiles($this->config->reportingDirectory(), '*.html') as $file) { |
45
|
|
|
$snapshots[] = new FileSnapshot($file); |
46
|
|
|
$order[] = $this->files->time($file); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
array_multisort($order, SORT_DESC, $snapshots); |
50
|
|
|
|
51
|
|
|
return $snapshots; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $filename |
56
|
|
|
* @return null|FileSnapshot |
57
|
|
|
*/ |
58
|
|
|
public function getSnapshot(string $filename) |
59
|
|
|
{ |
60
|
|
|
if (!$this->exists($filename)) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new FileSnapshot($this->config->reportingDirectory() . $filename); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function deleteSnapshots() |
71
|
|
|
{ |
72
|
|
|
foreach ($this->files->getFiles($this->config->reportingDirectory(), '*.html') as $file) { |
73
|
|
|
$this->files->delete($file); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param FileSnapshot $filename |
79
|
|
|
*/ |
80
|
|
|
public function deleteSnapshot(FileSnapshot $filename) |
81
|
|
|
{ |
82
|
|
|
$this->files->delete($filename->path()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $filename |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function exists(string $filename): bool |
90
|
|
|
{ |
91
|
|
|
return $this->files->exists($this->config->reportingDirectory() . $filename); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param FileSnapshot $snapshot |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function read(FileSnapshot $snapshot): string |
99
|
|
|
{ |
100
|
|
|
return $this->files->read($snapshot->path()); |
101
|
|
|
} |
102
|
|
|
} |