• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mimmi20 / browser-detector / 12945342151

22 Jan 2025 07:24PM UTC coverage: 89.212% (-10.8%) from 100.0%
12945342151

push

github

web-flow
Merge pull request #872 from mimmi20/updates

rewrite Headers

2983 of 3466 new or added lines in 72 files covered. (86.06%)

9 existing lines in 2 files now uncovered.

4234 of 4746 relevant lines covered (89.21%)

11.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Loader/Data/Engine.php
1
<?php
2

3
/**
4
 * This file is part of the browser-detector package.
5
 *
6
 * Copyright (c) 2012-2025, Thomas Mueller <mimmi20@live.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types = 1);
13

14
namespace BrowserDetector\Loader\Data;
15

16
use BrowserDetector\Loader\InitData\Engine as DataEngine;
17
use FilterIterator;
18
use Iterator;
19
use Laminas\Hydrator\Strategy\StrategyInterface;
20
use Override;
21
use RecursiveDirectoryIterator;
22
use RecursiveIteratorIterator;
23
use RuntimeException;
24
use SplFileInfo;
25

26
use function array_key_exists;
27
use function assert;
28
use function file_get_contents;
29
use function is_array;
30
use function is_string;
31
use function sprintf;
32
use function str_replace;
33

34
final class Engine
35
{
36
    private const string DATA_PATH = __DIR__ . '/../../../data/engines';
37

38
    /** @var array<string, DataEngine> */
39
    private array $items      = [];
40
    private bool $initialized = false;
41

42
    /** @throws void */
NEW
43
    public function __construct(private readonly StrategyInterface $strategy)
×
44
    {
45
        // nothing to do
NEW
46
    }
×
47

48
    /** @throws RuntimeException */
NEW
49
    public function init(): void
×
50
    {
NEW
51
        if ($this->initialized) {
×
NEW
52
            return;
×
53
        }
54

NEW
55
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::DATA_PATH));
×
NEW
56
        $files    = new class ($iterator, 'json') extends FilterIterator {
×
57
            /**
58
             * @param Iterator<SplFileInfo> $iterator
59
             *
60
             * @throws void
61
             */
62
            public function __construct(Iterator $iterator, private readonly string $extension)
63
            {
NEW
64
                parent::__construct($iterator);
×
65
            }
66

67
            /** @throws void */
68
            #[Override]
69
            public function accept(): bool
70
            {
NEW
71
                $file = $this->getInnerIterator()->current();
×
72

NEW
73
                assert($file instanceof SplFileInfo);
×
74

NEW
75
                return $file->isFile() && $file->getExtension() === $this->extension;
×
76
            }
NEW
77
        };
×
78

NEW
79
        foreach ($files as $file) {
×
NEW
80
            assert($file instanceof SplFileInfo);
×
81

NEW
82
            $pathName = $file->getPathname();
×
NEW
83
            $filepath = str_replace('\\', '/', $pathName);
×
NEW
84
            assert(is_string($filepath));
×
85

NEW
86
            $content = @file_get_contents($filepath);
×
87

NEW
88
            assert($content === false || is_string($content));
×
89

NEW
90
            if ($content === false) {
×
NEW
91
                throw new RuntimeException(sprintf('could not read file "%s"', $file));
×
92
            }
93

NEW
94
            $fileData = $this->strategy->hydrate($content, []);
×
95

NEW
96
            assert(is_array($fileData));
×
97

NEW
98
            foreach ($fileData as $key => $data) {
×
NEW
99
                $stringKey = (string) $key;
×
100

NEW
101
                if (array_key_exists($stringKey, $this->items) || !$data instanceof DataEngine) {
×
NEW
102
                    continue;
×
103
                }
104

NEW
105
                $this->items[$stringKey] = $data;
×
106
            }
107
        }
108

NEW
109
        $this->initialized = true;
×
110
    }
111

112
    /** @throws void */
NEW
113
    public function getItem(string $stringKey): DataEngine | null
×
114
    {
NEW
115
        return $this->items[$stringKey] ?? null;
×
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc