| <?php |
| |
| namespace App\Service; |
| |
| use App\Exception\DeviceNotFoundException; |
| use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| use Symfony\Component\DependencyInjection\ContainerInterface; |
| use Symfony\Component\Yaml\Yaml; |
| |
| class DeviceService { |
| private string $devicesDirectory; |
| |
| public function __construct( |
| #[Autowire(service: 'service_container')] ContainerInterface $container |
| ) { |
| $this->devicesDirectory = $container->getParameter('devices_dir'); |
| } |
| |
| public function getAvailableDevices(): array { |
| $availableDevices = scandir($this->devicesDirectory); |
| |
| return array_filter(array_map(function (string $deviceYaml) { |
| if (!in_array($deviceYaml, ['..', '.'])) { |
| return Yaml::parseFile($this->devicesDirectory . $deviceYaml); |
| } |
| }, $availableDevices)); |
| } |
| |
| public function getDeviceVendors(): array { |
| $availableDevices = $this->getAvailableDevices(); |
| $vendors = []; |
| |
| foreach ($availableDevices as $device) { |
| $vendor = $device['vendor']; |
| if (!array_key_exists($vendor, $vendors)) { |
| $vendors[$vendor] = []; |
| } |
| $vendors[$vendor][] = $device; |
| } |
| |
| return $vendors; |
| } |
| |
| public function getDeviceInfo(string $device) { |
| $file = $this->devicesDirectory . $device . '.yml'; |
| |
| if (is_file($file)) { |
| return Yaml::parseFile($file); |
| } else { |
| throw new DeviceNotFoundException("The device \"{$device}\" doesn't exist."); |
| } |
| } |
| } |