### Install MobileDetect 2.8.x via Git Source: https://docs.mobiledetect.net/home/install Use this for PHP versions >= 5.0. ```bash git checkout 2.8.x ``` ```php include "Mobile_Detect.php" ``` -------------------------------- ### Install MobileDetect 4.8.x via Git Source: https://docs.mobiledetect.net/home/install Use this for PHP versions >= 8.0. ```bash git checkout 4.8.x ``` ```bash composer require mobiledetect/mobiledetectlib ``` -------------------------------- ### Install MobileDetect 3.74.x via Git Source: https://docs.mobiledetect.net/home/install Use this for PHP versions 7.4 to 8.0. ```bash git checkout 3.74.x ``` ```php include "src/MobileDetect.php" ``` -------------------------------- ### Install Mobile-Detect via Git Source: https://docs.mobiledetect.net/home/usage-standalone Use these commands to clone the repository and switch to the stable 4.8.x branch. ```bash git clone git@github.com:serbanghita/Mobile-Detect.git git checkout 4.8.x ``` -------------------------------- ### Install Mobile Detect via Composer Source: https://docs.mobiledetect.net/home/usage-composer Use this command to add the library to your project dependencies. ```bash composer require mobiledetect/mobiledetectlib ``` -------------------------------- ### Create Local Branch Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Creates a new working branch based on the target version and installs dependencies. ```bash git checkout -b my-new-patch origin/x.x.x composer install ``` -------------------------------- ### Clone and Configure Repository Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Initializes the local development environment by cloning the repository and setting up remote tracking. ```bash git clone https://github.com/[yourname]/Mobile-Detect.git git add remote serbanghita https://github.com/serbanghita/Mobile-Detect.git git remote -v ... origin git@github.com:serbanghita/Mobile-Detect.git serbanghita https://github.com/serbanghita/Mobile-Detect.git ``` -------------------------------- ### Initialize Mobile-Detect in PHP Source: https://docs.mobiledetect.net/home/usage-standalone Demonstrates loading the autoloader and performing a basic mobile detection check. ```php setUserAgent('iPad'); try { var_dump($detection); var_dump($detection->isMobile()); } catch (MobileDetectException $e) { print_r($e); } ``` -------------------------------- ### Initialize Mobile Detect 2.x Source: https://docs.mobiledetect.net/home/2.x-and-3.x Use this for legacy 2.x versions of the library. Requires the Mobile_Detect.php file path. ```php require_once("/path/to/package/Mobile_Detect.php"); $detect = new Mobile_Detect(); $detect->isMobile(); $detect->isTablet(); ``` -------------------------------- ### Initialize Mobile Detect 3.x Source: https://docs.mobiledetect.net/home/2.x-and-3.x Use this for 3.x versions of the library. Note the namespace usage and updated class path. ```php require_once "/path/to/package/src/MobileDetect.php" $detect = new \Detection\MobileDetect; $detect->isMobile(); $detect->isTablet(); ``` -------------------------------- ### Initialize MobileDetect Source: https://docs.mobiledetect.net/home/the-constructor Instantiate the class with an optional cache interface and configuration array. ```php $detect = new MobileDetect(?CacheInterface $cache = null, array $config = []) ``` -------------------------------- ### Run Performance Benchmarks Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Executes performance tests using PHPBench to validate changes against a baseline. ```bash ./vendor/bin/phpbench run tests/Benchmark/MobileDetectBench.php --ref=baseline --retry-threshold=1 --iterations=10 --revs=1000 --report=aggregate ``` ```bash ./vendor/bin/phpbench run tests/Benchmark/ --retry-threshold=1 --iterations=10 --revs=1000 --report=aggregate --tag=baseline --dump-file=phpbench-baseline.xml ``` -------------------------------- ### Initialize and Use Mobile Detect Source: https://docs.mobiledetect.net/home/usage-composer Instantiate the MobileDetect class and use its methods to identify mobile or tablet devices. ```php // your_script.php // (optional) You're app is probably already using composer autoloader. require __DIR__ . '/vendor/autoload.php'; // (mandatory) use Detection MobileDetect; $detect = new MobileDetect(); // (optional) By default the Mobile Detect library auto-detects the HTTP headers from $_SERVER global variable. $detect->setUserAgent('Mozilla/5.0 (iPad; CPU OS 14_7 like Mac OS X) ...'); // Use whatever methods you want. $isMobile = $detect->isMobile(); $isTablet = $detect->isTablet(); var_dump($isMobile); var_dump($isTablet); var_dump($detect); // debug ``` -------------------------------- ### Configure composer.json Source: https://docs.mobiledetect.net/home/usage-composer Add the library to your composer.json file to manage dependencies. ```json // composer.json { "require": { "mobiledetect/mobiledetectlib": "4.8.09" // Change to the lastest version. } } ``` -------------------------------- ### Run Unit and Integration Tests Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Executes the PHPUnit test suite with coverage reporting enabled. ```bash vendor/bin/phpunit -v -c tests/phpunit.xml --coverage-html .coverage ``` -------------------------------- ### Add MobileDetect to composer.json Source: https://docs.mobiledetect.net/home/install Manual configuration for the composer.json file. ```json { "require": { "mobiledetect/mobiledetectlib": "^4.8" } } ``` -------------------------------- ### Detect Mobile and Tablet Devices with MobileDetect Source: https://docs.mobiledetect.net/home/example Instantiates the MobileDetect class and checks for mobile or tablet devices. The try/catch blocks are recommended to handle potential environment or cache exceptions. ```php $detect = new MobileDetect(); var_dump($detect->getUserAgent()); // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) ..." try { $isMobile = $detect->isMobile(); // bool(false) var_dump($isMobile); } catch (\Detection\Exception\MobileDetectException $e) { } try { $isTablet = $detect->isTablet(); // bool(false) var_dump($isTablet); } catch (\Detection\Exception\MobileDetectException $e) { } ``` -------------------------------- ### Commit Changes Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Standard Git workflow for staging, committing, and pushing changes to the remote repository. ```bash git status git stage git commit -m "your commit message here" git push ``` -------------------------------- ### MobileDetect Constructor Source: https://docs.mobiledetect.net/home/the-constructor The constructor for the MobileDetect class accepts an optional PSR-16 compatible cache implementation and an array of configuration options. ```APIDOC ## MobileDetect Constructor ### Description Initializes the MobileDetect class with optional caching and configuration. ### Method `__construct` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **cache** (`Psr\SimpleCache\CacheInterface` | `null`) - Optional - An instance of a cache class compatible with PSR-16 Simple Cache. - **config** (`array`) - Optional - An associative array of configuration options. The following configuration variables are supported: - **autoInitOfHttpHeaders** (`bool`) - Default: `true` - Automatically initializes from HTTP headers. Set to `false` for performance or manual User-Agent setting. - **maximumUserAgentLength** (`int`) - Default: `500` - The maximum allowed length for the User-Agent string. - **cacheKeyFn** (`callable` | `string`) - Default: `'sha1'` - The method used for encoding the cache key. - **cacheTtl** (`int`) - Default: `86400` - The default expiration time in seconds for the cache. ### Request Example ```php $detect = new MobileDetect(); // Basic initialization $cache = new MyCacheImplementation(); // Assuming MyCacheImplementation implements CacheInterface $config = [ 'autoInitOfHttpHeaders' => false, 'maximumUserAgentLength' => 1000, 'cacheKeyFn' => 'md5', 'cacheTtl' => 3600 ]; $detect = new MobileDetect($cache, $config); ``` ### Response None (constructor does not return a value) ### Error Handling None explicitly defined for constructor parameters, but underlying cache implementation might throw exceptions. ``` -------------------------------- ### MobileDetect.json Structure Source: https://docs.mobiledetect.net/home/extending-porting This JSON structure outlines the data used for mobile detection. It includes versioning, HTTP header matching rules, user-agent matching patterns for various device types and browsers, and operating systems. This file is essential for porting the detection logic to other languages. ```json { // Current released version. Respects semver. "version": "x.x.x", // Specific HTTP headers that attest for a "mobile" device. "headerMatch": { ... }, // Array with HTTP header names that can potentially contain the "User-Agent" string. "uaHttpHeaders": [ ... ], // Amazon CloudFront headers. "cloudFrontHttpHeaders": [ ... ], // Regexes ordered by priority of "mobile" devices. "uaMatch": { "phones": { ... }, "tablets": { ... }, "browsers": { ... }, "os": { ... } } } ``` -------------------------------- ### Lint Code Source: https://docs.mobiledetect.net/home/contribute-by-developing-code Executes code style checkers and fixers to ensure compliance with project standards. ```bash ./vendor/bin/phpcs ./vendor/bin/phpcbf ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.