mimmi20 / wurfl
适用于PHP 5.3+的Wurfl PHP库
Requires
- php: >=5.3.3
- hassankhan/config: ^0.10
- mimmi20/wurfl-constants: ^1.7
- mimmi20/wurfl-generic-request: ^1.7
- mimmi20/wurflcache: ^1.7
- psr/log: ^1.0
Requires (Dev)
- fabpot/php-cs-fixer: ^1.11
- mikey179/vfsstream: ^1.3
- monolog/monolog: ^1.0
- phpunit/phpunit: ^4.8|^5.0
- squizlabs/php_codesniffer: ^2.0
Suggests
- monolog/monolog: if you want to log to more than a file
This package is not auto-updated.
Last update: 2019-02-20 17:35:04 UTC
README
官方Wurfl PHP库的克隆版,已更新以支持PHP 5.3
提交错误和功能请求
错误和功能请求在GitHub上跟踪
重要变更
这些变更包括:
- 添加了Wurfl命名空间,从文件名中移除了"WURFL"部分
- \Wurfl\Service和\Wurfl\ManagerFactory已合并到\Wurl\Manager
- 原始Wurfl包的部分被提取到它们自己的仓库中,这些是通过composer安装的
- Xml配置类已被FileConfig类替换,该类也能读取ini/yaml格式的配置文件
官方WURFL PHP API
==============================
许可证
本程序是自由软件:您可以自由地重新分发和/或修改它,具体请参阅由自由软件基金会发布的GNU Affero通用公共许可证的条款,许可证版本为3,或(根据您的选择)任何更新的版本。
有关适用的GNU Affero通用公共许可证的完整文本,请参阅随本软件包一起分发的COPYING文件。
如果您无法遵守AGPL许可证的条款,可以从ScientiaMobile,Inc获得商业许可证,详情请访问http://www.scientiamobile.com/
入门
从wurfl网站下载发行存档,并将其解压缩到适合您应用程序的目录中。
要开始使用API,您需要设置一些配置选项。
重要:WURFL API与WURFL.XML文件紧密相关。WURFL.XML的新版本可能默认与旧版本的API兼容,但反之则不成立。旧版本的WURFL.XML不保证与新版本的API兼容。让我们再次强调:此API与旧版本的WURFL.XML不兼容。此分发中包含可以与此API一起使用的最老版本的WURFL。
/** * This makes our life easier when dealing with paths. Everything is relative * to the application root now. * * change this to pass to your project settings */ chdir(dirname(__DIR__)); require 'vendor/autoload.php'; /* * relative to the root dir * -> $resourcesDir = <Project Root>/resources */ $resourcesDir = 'resources'; $persistenceDir = $resourcesDir . '/storage/persistence'; $cacheDir = $resourcesDir . '/storage/cache'; // Create WURFL Configuration $wurflConfig = new \Wurfl\Configuration\InMemoryConfig(); // Set location of the WURFL File $wurflConfig->wurflFile($resourcesDir . '/wurfl.zip'); /* * Set the match mode for the API * * It is recommended to use the defined class constants instead of their * string values: * * \Wurfl\Configuration\Config::MATCH_MODE_PERFORMANCE * \Wurfl\Configuration\Config::MATCH_MODE_ACCURACY */ $wurflConfig->matchMode(\Wurfl\Configuration\Config::MATCH_MODE_PERFORMANCE); // Setup WURFL Persistence $wurflConfig->persistence( 'file', array(\Wurfl\Configuration\Config::DIR => $persistenceDir) ); // Setup Caching $wurflConfig->cache( 'file', array( \Wurfl\Configuration\Config::DIR => $cacheDir, \Wurfl\Configuration\Config::EXPIRATION => 36000 ) ); // Create the cache instance from the configuration $cacheStorage = Storage\Factory::create($wurflConfig->cache); // Create the persistent cache instance from the configuration $persistenceStorage = Storage\Factory::create($wurflConfig->persistence); // Create a WURFL Manager from the WURFL Configuration $wurflManager = new \Wurfl\Manager($wurflConfig, $persistenceStorage, $cacheStorage);
如果您不想自己构建这三个对象,可以使用工厂方法。
$wurflManager = \Wurfl\Manager::factory($wurflConfig);
现在您可以开始使用一些\Wurfl\Manager类的方法;
$device = $wurflManager->getDeviceForHttpRequest($_SERVER); $device->getCapability('is_wireless_device'); $device->getVirtualCapability('is_smartphone');
创建配置对象
-
设置主WURFL文件的位置路径(如果启用了zip扩展,可以使用zip文件)
-
通过指定提供者和初始化提供者所需的额外参数来配置持久性提供者。API支持以下机制:
- Memcache (http://uk2.php.net/memcache)
- APC(替代PHP缓存 https://php.ac.cn/apc)
- 内存
- 文件
除了官方提供者外,还添加了以下连接器
- Zend 缓存
-
通过指定提供者和初始化提供者所需的额外参数来配置缓存提供者。API 支持以下缓存机制
- Memcache (http://uk2.php.net/memcache)
- APC(替代PHP缓存 https://php.ac.cn/apc)
- 文件
- 空(无缓存)
除了官方提供者外,还添加了以下连接器
- Zend 缓存
示例配置
// Create WURFL Configuration $wurflConfig = new \Wurfl\Configuration\InMemoryConfig(); // Set location of the WURFL File $wurflConfig->wurflFile($resourcesDir . '/wurfl.zip'); /* * Set the match mode for the API * * It is recommended to use the defined class constants instead of their * string values: * * \Wurfl\Configuration\Config::MATCH_MODE_PERFORMANCE * \Wurfl\Configuration\Config::MATCH_MODE_ACCURACY */ $wurflConfig->matchMode(\Wurfl\Configuration\Config::MATCH_MODE_PERFORMANCE); // Setup WURFL Persistence $wurflConfig->persistence( 'file', array(\Wurfl\Configuration\Config::DIR => $persistenceDir) ); // Setup Caching $wurflConfig->cache( 'file', array( \Wurfl\Configuration\Config::DIR => $cacheDir, \Wurfl\Configuration\Config::EXPIRATION => 36000 ) );
使用 WURFL PHP API
获取设备
您有四种方法来检索设备
-
getDeviceForRequest(\Wurfl\Request\GenericRequest $request)
其中 \Wurfl\Request\GenericRequest 是一个对象,封装了设备的 userAgent、ua-profile 以及对 xhtml 的支持。 -
getDeviceForHttpRequest($_SERVER)
大多数情况下您将使用此方法,并且 API 将为您创建 \Wurfl\Request\GenericRequest 对象。 -
getDeviceForUserAgent(string $userAgent)
用于查询给定 User Agent 字符串的 API。 -
getDevice(string $deviceID)
通过设备的设备 ID(例如:apple_iphone_ver1
)获取设备。
用法示例
$device = $wurflManager->getDeviceForHttpRequest($_SERVER);
获取设备属性及其功能
设备 ID 和回退设备 ID 是设备的属性
$deviceID = $device->id; $fallBack = $device->fallBack;
要获取功能值,请使用 getCapability()
$value = $device->getCapability("is_tablet"); $allCaps = $device->getAllCapabilities();
要获取虚拟功能值,请使用 getVirtualCapability()
$value = $device->getVirtualCapability("is_smartphone"); $allVCaps = $device->getAllVirtualCapabilities();
有用方法
根 WURFL 设备对象具有一些有用函数
/* @var $device \Wurfl\CustomDeviceInterface */ $device = $wurflManager->getDeviceForHttpRequest($_SERVER); /* @var $root \Wurfl\Device\ModelDeviceInterface */ $root = $device->getRootDevice(); $group_names = $root->getGroupNames(); $cap_names = $root->getCapNames(); $defined = $root->isCapabilityDefined("foobar");
WURFL 重新加载器
WURFL 可以通过检查 WURFL 文件的修改时间来自动更新持久化数据,无需任何配置。要启用,请在配置中将 allow-reload 设置为 true。
$wurflConfig->allowReload(true);
如果您有任何问题,请参阅 http://wurfl.sourceforge.net 上的文档,以及 http://www.scientiamobile.com/forum 上的 ScientiaMobile 支持论坛。