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
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 2
Open Issues: 1
Type:symfony-bundle
Requires
- php: >=8.2
- psr/log: ^3.0
- symfony/config: ^7.0
- symfony/dependency-injection: ^7.0
- symfony/framework-bundle: ^7.0
- symfony/http-foundation: ^7.0
- symfony/http-kernel: ^7.0
- symfony/yaml: ^7.0
- twig/twig: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- symfony/phpunit-bridge: ^7.0
- symfony/test-pack: ^1.0
Suggests
- ext-intl: For better locale handling
- monolog/monolog: For advanced logging capabilities
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.
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 namegetOsVersion()
: Get OS versiongetOsTitle()
: Get formatted OS name with version
Browser Information
getBrowserName()
: Get browser namegetBrowserVersion()
: Get browser versiongetBrowserTitle()
: Get formatted browser name with versiongetBrowserChromiumVersion()
: Get Chromium version if applicablegetBrowserGeckoVersion()
: Get Gecko version if applicablegetBrowserWebkitVersion()
: Get WebKit version if applicableisBrowserChromeOriginal()
: Check if browser is original ChromeisBrowserFirefoxOriginal()
: Check if browser is original FirefoxisBrowserSafariOriginal()
: Check if browser is original Safari
Device Information
getDeviceType()
: Get device typeisMobile()
: Check if device is mobileisDesktop()
: Check if device is desktopisTablet()
: Check if device is tabletisBrowserAndroidWebview()
: Check if Android WebViewisBrowserIosWebview()
: Check if iOS WebViewisBrowserDesktopMode()
: Check if desktop modeis64BitsMode()
: 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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This bundle is released under the MIT License. See the bundled LICENSE file for details.
Credits
Developed by Eprofos.