matomo-org / device-detector

The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
http://devicedetector.net
GNU Lesser General Public License v3.0
3.1k stars 478 forks source link

Symfony yaml parser not working #7365

Closed AndiLeni closed 1 year ago

AndiLeni commented 1 year ago

Hello,

I have problems with spyc in my CMS which uses its own autoloader. That’s why I wanted to switch to the Symfony Yaml Parser.

I installed device detector via composer and deleted the mustangostang/spyc folder in /vendor. I then tried to switch to Symfony like this:

use DeviceDetector\ClientHints;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Yaml\Symfony as DeviceDetectorSymfonyYamlParser;

...

$this->DeviceDetector = new DeviceDetector($this->userAgent, $clientHints);
$this->DeviceDetector->setYamlParser(new DeviceDetectorSymfonyYamlParser());
echo $this->DeviceDetector->getYamlParser()) # prints DeviceDetector\Yaml\Symfony
echo 'A'; # is printed
$this->DeviceDetector->parse();
echo 'B'; # is NOT printed

Instead a Class 'Spyc' not found error is thrown. Any ideas what might be wrong with this approach?

Thanks and kind regards

sanchezzzhak commented 1 year ago

Hi. When you have manually deleted something from the vendor folder, you must do

composer dumpautoload
AndiLeni commented 1 year ago

Thanks for the suggestion. Unfortunately, this did not help. I tried using device-detector without composer, but this gave me the same error.

AndiLeni commented 1 year ago

@sanchezzzhak It seems to be a problem with clients hints.

You may reproduce this issue with the following steps:

<?php

require_once 'vendor/autoload.php';
require_once 'device-detector/autoload.php';

use DeviceDetector\ClientHints;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\AbstractDeviceParser;
use DeviceDetector\Yaml\Symfony as SYP;

$userAgent = $_SERVER['HTTP_USER_AGENT']; // change this to the useragent you want to parse
$clientHints = ClientHints::factory($_SERVER); // client hints are optional

$dd = new DeviceDetector($userAgent, $clientHints);

$dd->setYamlParser(new SYP());

$dd->parse();

if ($dd->isBot()) {
    // handle bots,spiders,crawlers,...
    $botInfo = $dd->getBot();
} else {
    $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ...
    $osInfo = $dd->getOs();
    $device = $dd->getDeviceName();
    $brand = $dd->getBrandName();
    $model = $dd->getModel();
}

as soon as I change this line

$dd = new DeviceDetector($userAgent, $clientHints);

to

$dd = new DeviceDetector($userAgent);

the error disappears.

AndiLeni commented 1 year ago

It seems like the AppHints parser is not using the correct yaml parser.

Fatal error: Uncaught Error: Class "Spyc" not found in C:\Users\andil\Desktop\Neuer Ordner\device-detector\Yaml\Spyc.php:28 Stack trace: #0 C:\Users\andil\Desktop\Neuer Ordner\device-detector\Parser\AbstractParser.php(252): DeviceDetector\Yaml\Spyc->parseFile('C:\\Users\\andil\\...') #1 C:\Users\andil\Desktop\Neuer Ordner\device-detector\Parser\Client\Hints\AppHints.php(43): DeviceDetector\Parser\AbstractParser->getRegexes() #2 C:\Users\andil\Desktop\Neuer Ordner\device-detector\Parser\Client\MobileApp.php(97): DeviceDetector\Parser\Client\Hints\AppHints->parse() #3 C:\Users\andil\Desktop\Neuer Ordner\device-detector\DeviceDetector.php(858): DeviceDetector\Parser\Client\MobileApp->parse() #4 C:\Users\andil\Desktop\Neuer Ordner\device-detector\DeviceDetector.php(613): DeviceDetector\DeviceDetector->parseClient() #5 C:\Users\andil\Desktop\Neuer Ordner\index.php(19): DeviceDetector\DeviceDetector->parse() #6 {main}

AndiLeni commented 1 year ago

Thanks for the quick fix! :-)