File: Engine.php

package info (click to toggle)
matomo-device-detector 6.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 19,124 kB
  • sloc: php: 7,493; xml: 79; makefile: 15; sh: 3
file content (104 lines) | stat: -rw-r--r-- 2,326 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

/**
 * Device Detector - The Universal Device Detection library for parsing User Agents
 *
 * @link https://matomo.org
 *
 * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
 */

declare(strict_types=1);

namespace DeviceDetector\Parser\Client\Browser;

use DeviceDetector\Parser\Client\AbstractClientParser;

/**
 * Class Engine
 *
 * Client parser for browser engine detection
 */
class Engine extends AbstractClientParser
{
    /**
     * @var string
     */
    protected $fixtureFile = 'regexes/client/browser_engine.yml';

    /**
     * @var string
     */
    protected $parserName = 'browserengine';

    /**
     * Known browser engines mapped to their internal short codes
     *
     * @var array
     */
    protected static $availableEngines = [
        'WebKit',
        'Blink',
        'Trident',
        'Text-based',
        'Dillo',
        'iCab',
        'Elektra',
        'Presto',
        'Clecko',
        'Gecko',
        'KHTML',
        'NetFront',
        'Edge',
        'NetSurf',
        'Servo',
        'Goanna',
        'EkiohFlow',
        'Arachne',
        'LibWeb',
        'Maple',
    ];

    /**
     * Returns list of all available browser engines
     * @return array
     */
    public static function getAvailableEngines(): array
    {
        return self::$availableEngines;
    }

    /**
     * @inheritdoc
     */
    public function parse(): ?array
    {
        $matches = false;

        foreach ($this->getRegexes() as $regex) {
            $matches = $this->matchUserAgent($regex['regex']);

            if ($matches) {
                break;
            }
        }

        if (empty($matches) || empty($regex)) {
            return null;
        }

        $name = $this->buildByMatch($regex['name'], $matches);

        foreach (self::getAvailableEngines() as $engineName) {
            if (\strtolower($name) === \strtolower($engineName)) {
                return ['engine' => $engineName];
            }
        }

        // This Exception should never be thrown. If so a defined browser name is missing in $availableEngines
        throw new \Exception(\sprintf(
            'Detected browser engine was not found in $availableEngines. Tried to parse user agent: %s',
            $this->userAgent
        )); // @codeCoverageIgnore
    }
}