Mobile Detect
repository·4.x·Indexed 27 days ago
https://github.com/serbanghita/mobile-detectA lightweight PHP library used to detect mobile devices and tablets by inspecting User-Agent strings and HTTP headers. It supports detection of specific platforms (iOS, Android), brands (Samsung), and browsers, and integrates with PSR-16 cache interfaces and Amazon CloudFront device detection headers.
What's inside mobile-detect
- Mobile Detect is a lightweight PHP class designed for detecting mobile devices, including tablets. It identifies the mobile environment by analyzing the User-Agent string in conjunction with specific HTTP headers.
Understand the role of Simple Cache PSR-16 interfaces
4.xThis repository contains the interfaces related to the PSR-16 (Simple Cache) standard. It is important to note that this package is not a cache implementation itself; it only defines the interfaces that a cache implementation must follow. To actually store data, you must use a package that implements this specification.Use Amazon CloudFront device detection headers
4.xMobileDetect automatically recognizes and uses Amazon CloudFront headers if device detection is enabled on your distribution.
use Detection\MobileDetect; // Automatically uses: // - HTTP_CLOUDFRONT_IS_MOBILE_VIEWER // - HTTP_CLOUDFRONT_IS_TABLET_VIEWER // - HTTP_CLOUDFRONT_IS_DESKTOP_VIEWER $detect = new MobileDetect(); if ($detect->isMobile()) { // Mobile device detected via CloudFront }Run all pre-release validation checks via Docker Compose
4.xTo run the full suite of pre-release validation checks (including unit tests, performance benchmarks, linting, and static analysis) in a controlled PHP environment, use the
runAllservice. This acts as a pre-release gate.docker compose -p mobile-detect up --build runAllIntegrate MobileDetect with Laravel or Symfony
4.xMobileDetect can be easily integrated into modern frameworks via Middleware (Laravel) or Dependency Injection (Symfony).
// Laravel Middleware Example namespace App\Http\Middleware; use Closure; use Detection\MobileDetect; use Illuminate\Http\Request; class DetectMobileDevice { public function handle(Request $request, Closure $next) { $detect = new MobileDetect(); $request->attributes->set('is_mobile', $detect->isMobile()); $request->attributes->set('is_tablet', $detect->isTablet()); return $next($request); } } // Symfony Service Example (config/services.yaml) // services: // Detection\MobileDetect: // public: trueRun tests for Mobile Detect
4.xTo run the test suite and generate a coverage report, use the following command from your project root:
vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-html .coverageAdd new device models to Mobile-Detect
4.xTo add support for new tablet or phone models, follow this three-step methodology:
- Identify new models: Research vendor websites (e.g., Samsung's regional sites), use GSMArena to find specific
SM-XXXXmodel variants, or check SamMobile. Cross-reference these with existing regex insrc/MobileDetect.phpand test fixtures intests/providers/vendors/{Vendor}.phpto avoid duplicates. - Find User-Agent strings: Search UA databases (user-agents.net, whatismybrowser.com, etc.) or search for the specific model number + "user agent". If the device is too new, construct a UA string using the standard Chrome pattern with the device's Android version and a contemporary Chrome version.
- Add to codebase:
- Update the appropriate regex array in
src/MobileDetect.php(e.g.,$tabletDevices['SamsungTablet']). - Add new test fixture entries in
tests/providers/vendors/{Vendor}.php. - Verify the changes by running the test suite.
- Update the appropriate regex array in
vendor/bin/phpunit -v -c tests/phpunit.xml- Identify new models: Research vendor websites (e.g., Samsung's regional sites), use GSMArena to find specific
Handle long-running processes and batch processing
4.xWhen using MobileDetect in workers, daemons (RoadRunner, Swoole, etc.), or batch processing scripts, ensure you manage the cache to prevent memory exhaustion. The bundled cache is bounded by default, but you can manually
clear()it periodically.use Detection\MobileDetect; use Detection\Cache\Cache; $detect = new MobileDetect(); $cache = $detect->getCache(); $iterationCount = 0; while ($userAgent = getNextUserAgentFromQueue()) { $detect->setUserAgent($userAgent); $isMobile = $detect->isMobile(); $isTablet = $detect->isTablet(); processDevice($userAgent, $isMobile, $isTablet); $iterationCount++; // Periodically reset the cache to manage memory if ($iterationCount % 1000 === 0 && $cache instanceof Cache) { $cache->clear(); } }Run individual validation services via Docker Compose
4.xYou can run specific validation tasks individually using Docker Compose. Each service uses a specific PHP image tailored to the task (e.g., Xdebug for coverage, Alpine for performance/linting).
# Unit tests with coverage docker compose -p mobile-detect up --build runUnitTests # Performance benchmarks docker compose -p mobile-detect up --build runPerfTests # Code style linting docker compose -p mobile-detect up --build runLinting # Static analysis docker compose -p mobile-detect up --build runQualityCheck # Generate JSON model (runs after all checks pass) docker compose -p mobile-detect up --build generateJsonModelFind a PSR-16 cache implementation
4.xSince this repository only provides the interfaces, you need to install a concrete implementation to use caching in your project. You can find compatible packages by searching for thepsr/simple-cache-implementationvirtual package on Packagist.Run performance tests via Docker Compose
4.xTo run the performance benchmark suite, use Docker Compose with the
runPerfTestsservice. The current benchmark environment uses8.4-alpine(linux/amd64).docker compose -p mobile-detect up runPerfTestsClean up Docker Compose resources
4.xTo remove the containers, networks, and volumes created by the
mobile-detectproject and clean up orphaned containers, use thedowncommand with the--volumesand--remove-orphansflags.docker compose -p mobile-detect down --volumes --remove-orphans