eprofos/user-agent-analyzer

A powerful Symfony bundle for user-agent analysis. It provides accurate detection of operating systems (Windows, MacOS, Linux, iOS, Android...), browsers (Chrome, Firefox, Safari...), and device types (Desktop, Mobile, Tablet, TV...). Supports specific version detection and includes advanced handlin

v2.1.0 2024-11-02 20:35 UTC

This package is auto-updated.

Last update: 2025-05-09 07:43:10 UTC


README

A powerful Symfony bundle for user-agent analysis. It provides accurate detection of operating systems (Windows, MacOS, Linux, iOS, Android...), browsers (Chrome, Firefox, Safari...), and device types (Desktop, Mobile, Tablet, TV...). Supports specific version detection and includes advanced handling of special cases like WebViews and compatibility modes.

Latest Stable Version License Tests PHP Version Symfony Version

Features

  • Operating System Detection

    • Windows, MacOS, Linux, iOS, Android, and more
    • Version detection with codename support for MacOS
    • Mobile OS variants (MIUI, EMUI, ColorOS, etc.)
    • 64-bit mode detection
  • Browser Detection

    • Major browsers: Chrome, Firefox, Safari, Edge, Opera
    • Mobile browsers and WebViews
    • Version detection and rendering engine identification
    • Chromium/Gecko/WebKit version tracking
    • Desktop mode detection
  • Device Detection

    • Device type classification (Desktop, Mobile, Tablet, TV, etc.)
    • Smart device detection (Smart TV, Game Consoles, Car Systems)
    • Touch support detection
    • WebView detection for Android and iOS
  • Advanced Features

    • Automatic User-Agent detection from current request
    • Comprehensive logging support
    • High accuracy through multiple detection methods
    • Easy integration with Symfony applications
    • Extensive test coverage
    • PSR-3 logging support

Requirements

  • PHP 8.2 or higher
  • Symfony 7.0 or higher

Installation

Using Composer

composer require eprofos/user-agent-analyzer

Enable the Bundle

If you're not using Symfony Flex, add the bundle to your config/bundles.php:

return [
    // ...
    Eprofos\UserAgentAnalyzerBundle\EprofosUserAgentAnalyzerBundle::class => ['all' => true],
];

Usage

Basic Usage

use Eprofos\UserAgentAnalyzerBundle\Service\UserAgentAnalyzer;

class YourController
{
    public function someAction(UserAgentAnalyzer $analyzer)
    {
        $result = $analyzer->analyzeCurrentRequest();
        
        // Access the results
        $osName = $result->getOsName();           // e.g., "Windows"
        $osVersion = $result->getOsVersion();     // e.g., 10.0
        $browserName = $result->getBrowserName(); // e.g., "Chrome"
        $deviceType = $result->getDeviceType();   // e.g., "desktop"
        
        // Check device type
        $isMobile = $result->isMobile();    // true if device is mobile
        $isDesktop = $result->isDesktop();  // true if device is desktop
        $isTablet = $result->isTablet();    // true if device is tablet
        
        // Get all information as array
        $allInfo = $result->toArray();
    }
}

Advanced Usage with Touch Support Mode

use Eprofos\UserAgentAnalyzerBundle\Service\UserAgentAnalyzer;

class YourController
{
    public function someAction(
        UserAgentAnalyzer $analyzer,
        Request $request
    ) {
    {
        // Analyze current request with touch support detection
        $result = $analyzer->analyzeCurrentRequest(true);

        // Or analyze specific user agent with touch support
        $userAgent = $request->headers->get('User-Agent');
        $result = $analyzer->analyze($userAgent, true);

        // Check for specific browser features
        $isWebView = $result->isBrowserAndroidWebview() || $result->isBrowserIosWebview();
        $isDesktopMode = $result->isBrowserDesktopMode();
        $is64Bit = $result->is64BitsMode();
    }
}

Twig Functions

The bundle provides several Twig functions for easy device and OS detection in your templates:

{# Device Type Detection #}
{% if is_mobile() %}
    {# Mobile-specific content #}
{% endif %}

{% if is_desktop() %}
    {# Desktop-specific content #}
{% endif %}

{% if is_tablet() %}
    {# Tablet-specific content #}
{% endif %}

{# Operating System Detection #}
{% if is_android() %}
    {# Android-specific content #}
{% endif %}

{% if is_windows() %}
    {# Windows-specific content #}
{% endif %}

{% if is_linux() %}
    {# Linux-specific content #}
{% endif %}

{% if is_ios() %}
    {# iOS-specific content #}
{% endif %}

{% if is_macos() %}
    {# macOS-specific content #}
{% endif %}

All Twig functions automatically analyze the current request's User-Agent string and return a boolean value indicating whether the condition is met.

Available Methods

Operating System Information

  • getOsType(): Get OS type (desktop, mobile, etc.)
  • getOsFamily(): Get OS family (windows, macintosh, etc.)
  • getOsName(): Get OS name
  • getOsVersion(): Get OS version
  • getOsTitle(): Get formatted OS name with version

Browser Information

  • getBrowserName(): Get browser name
  • getBrowserVersion(): Get browser version
  • getBrowserTitle(): Get formatted browser name with version
  • getBrowserChromiumVersion(): Get Chromium version if applicable
  • getBrowserGeckoVersion(): Get Gecko version if applicable
  • getBrowserWebkitVersion(): Get WebKit version if applicable
  • isBrowserChromeOriginal(): Check if browser is original Chrome
  • isBrowserFirefoxOriginal(): Check if browser is original Firefox
  • isBrowserSafariOriginal(): Check if browser is original Safari

Device Information

  • getDeviceType(): Get device type
  • isMobile(): Check if device is mobile
  • isDesktop(): Check if device is desktop
  • isTablet(): Check if device is tablet
  • isBrowserAndroidWebview(): Check if Android WebView
  • isBrowserIosWebview(): Check if iOS WebView
  • isBrowserDesktopMode(): Check if desktop mode
  • is64BitsMode(): Check if 64-bit mode

Testing

composer test

Quality Tools

# Run PHP CS Fixer
composer cs-fix

# Run PHPStan
composer phpstan

# Run all quality tools
composer analyze

Contributing

Feel free to contribute to this bundle by submitting issues and pull requests.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This bundle is released under the MIT License. See the bundled LICENSE file for details.

Credits

Developed by Eprofos.