|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SSpkS\Device; |
|
4
|
|
|
|
|
5
|
|
|
use \Symfony\Component\Yaml\Yaml; |
|
6
|
|
|
use \Symfony\Component\Yaml\Exception\ParseException; |
|
7
|
|
|
|
|
8
|
|
|
class DeviceList |
|
9
|
|
|
{ |
|
10
|
|
|
private $config; |
|
11
|
|
|
private $yamlFilepath; |
|
12
|
|
|
private $devices = array(); |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param \SSpkS\Config $config Config object |
|
16
|
|
|
* @throws \Exception if file is not found or parsing error. |
|
17
|
|
|
*/ |
|
18
|
5 |
|
public function __construct(\SSpkS\Config $config) |
|
19
|
|
|
{ |
|
20
|
5 |
|
$this->config = $config; |
|
21
|
5 |
|
$this->yamlFilepath = $this->config->paths['models']; |
|
22
|
5 |
|
if (!file_exists($this->yamlFilepath)) { |
|
23
|
1 |
|
throw new \Exception('DeviceList file ' . $this->yamlFilepath . ' not found!'); |
|
24
|
|
|
} |
|
25
|
|
|
try { |
|
26
|
4 |
|
$this->parseYaml(); |
|
27
|
1 |
|
} catch (\Exception $e) { |
|
28
|
1 |
|
throw $e; |
|
29
|
|
|
} |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parse Yaml file with device data. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \Exception if Yaml couldn't be parsed. |
|
36
|
|
|
*/ |
|
37
|
4 |
|
private function parseYaml() |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
/** @var array $familylist */ |
|
41
|
4 |
|
$familylist = Yaml::parse(file_get_contents($this->yamlFilepath)); |
|
42
|
|
|
} catch (ParseException $e) { |
|
43
|
|
|
throw new \Exception($e->getMessage()); |
|
44
|
|
|
} |
|
45
|
4 |
|
$idx = 0; |
|
46
|
4 |
|
$sortkey = array(); |
|
47
|
4 |
|
foreach ($familylist as $family => $archlist) { |
|
48
|
4 |
|
if (!is_array($archlist) && !is_object($archlist)) { |
|
49
|
1 |
|
throw new \Exception("Models list in $family is not an array"); |
|
50
|
|
|
} |
|
51
|
3 |
|
foreach ($archlist as $arch => $archmodels) { |
|
52
|
3 |
|
foreach ($archmodels as $model) { |
|
53
|
3 |
|
$this->devices[$idx] = array( |
|
54
|
3 |
|
'arch' => $arch, |
|
55
|
3 |
|
'name' => $model, |
|
56
|
3 |
|
'family' => $family, |
|
57
|
|
|
); |
|
58
|
3 |
|
$sortkey[$idx] = $model; |
|
59
|
3 |
|
$idx++; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
3 |
|
array_multisort($sortkey/*, SORT_NATURAL | SORT_FLAG_CASE*/, $this->devices); |
|
64
|
3 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the architecture family for given $arch |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $arch Architecture |
|
70
|
|
|
* @return string Family or $arch if not found |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function getFamily($arch) |
|
73
|
|
|
{ |
|
74
|
1 |
|
foreach ($this->devices as $d) { |
|
75
|
1 |
|
if ($d['arch'] == $arch) { |
|
76
|
|
|
return $d['family']; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
1 |
|
return $arch; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the list of devices and their architectures. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array List of devices and architectures. |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getDevices() |
|
88
|
|
|
{ |
|
89
|
2 |
|
return $this->devices; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|