Repository navigation
Expand file tree
/
Copy pathHarFileRepository.php
More file actions
88 lines (70 loc) · 1.96 KB
/
Copy pathHarFileRepository.php
File metadata and controls
88 lines (70 loc) · 1.96 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
78
79
80
81
82
83
84
85
86
87
88
<?php
declare(strict_types=1);
namespace Deviantintegral\Har\Repository;
use Deviantintegral\Har\Har;
use Deviantintegral\Har\Serializer;
/**
* Handles loading of HAR objects from disk.
*/
class HarFileRepository implements RepositoryInterface
{
/**
* @var string
*/
private $repositoryPath;
/**
* HarFileRepository constructor.
*
* @param string $repositoryPath the path to the repository on disk
*/
public function __construct(string $repositoryPath)
{
$this->repositoryPath = $repositoryPath;
}
public function load(string $id): Har
{
$contents = $this->loadJson($id);
return (new Serializer())->deserializeHar($contents);
}
public function loadMultiple(array $ids = []): \Generator
{
if (empty($ids)) {
$ids = $this->getIds();
}
foreach ($ids as $id) {
yield $id => $this->load($id);
}
}
public function getIds(): array
{
$hars = scandir($this->repositoryPath);
if (!$hars) {
return [];
}
foreach ($hars as $index => $har_file) {
if (\strlen($har_file) < 4) {
unset($hars[$index]);
continue;
}
// Remove any file not ending in .har.
if (false === strpos($har_file, '.har', \strlen($har_file) - \strlen('.har'))) {
unset($hars[$index]);
continue;
}
}
sort($hars, \SORT_NATURAL);
return $hars;
}
public function loadJson(string $id): string
{
$path = $this->repositoryPath.'/'.$id;
if (!file_exists($path)) {
throw new \RuntimeException(\sprintf('%s does not exist', $path));
}
$contents = file_get_contents($path);
if (!$contents) {
throw new \RuntimeException(\sprintf('%s was unable to be loaded', $path));
}
return $contents;
}
}