acsiomatic/device-detector-bundle

Symfony Bundle for https://github.com/matomo-org/device-detector

安装量: 50 235

依赖项: 1

建议者: 0

安全性: 0

星标: 10

关注者: 3

分支: 1

开放问题: 5

类型:symfony-bundle

0.5.0 2022-07-22 19:43 UTC

This package is auto-updated.

Last update: 2024-09-08 08:57:03 UTC


README

AcsiomaticDeviceDetectorBundle 提供了将 DeviceDetector 库集成到 Symfony 框架中的功能。

DeviceDetector 是一个通用的设备检测库,它解析用户代理和浏览器客户端提示来检测设备(桌面、平板、手机、电视、汽车、控制台等)、客户端(浏览器、新闻阅读器、媒体播放器、个人信息管理软件等)、操作系统、品牌和型号。

From https://github.com/matomo-org/device-detector

此捆绑包提供 DeviceDetector 作为 服务,以及 Twig 全局变量

安装

您可以使用 Composer 安装此捆绑包

composer require acsiomatic/device-detector-bundle

配置

您可以使用 acsiomatic_device_detector 配置部分来配置此捆绑包

# config/packages/acsiomatic_device_detector.yaml

acsiomatic_device_detector:

    # If true, DeviceDetector will trigger parser() when necessary
    auto_parse: true

    # Version truncation behavior, it may assume: major, minor, patch, build, or none
    # By default minor versions will be returned (e.g. X.Y)
    version_truncation: 'minor'

    cache:

        # If null, it will disable caching
        pool: 'cache.app'

    bot:

        # If true getBot() will only return true if a bot was detected (speeds up detection a bit)
        discard_information: false

        # If true, bot detection will completely be skipped (bots will be detected as regular devices then)
        skip_detection: false

    twig:

        # If null, it will not assign Twig variable
        variable_name: 'device'

    routing:

        # If null, it will not tag DeviceDetector as routing condition service
        condition_service_alias: 'device'

使用

控制器中使用

您可以将 DeviceDetector 注入为服务。此捆绑包根据 acsiomatic_device_detector 部分下的配置设置此类的一个实例,以提供有关主请求的信息。

use DeviceDetector\DeviceDetector;
use Symfony\Component\HttpFoundation\Response;

class MyController
{
    public function index(DeviceDetector $device): Response
    {
        if ($device->isSmartphone()) {
            // ...
        }
    }
}

注意

如果 auto_parse 配置为 false,则在请求设备信息之前需要调用 $device->parse()

在 Twig 中使用

DeviceDetector 服务作为 device 变量分配到 Twig 模板中。

{% if device.isSmartphone %}
    {# ... #}
{% endif %}

路由条件中使用

DeviceDetector 被标记为路由条件服务。

use Symfony\Component\HttpFoundation\Response;

class MyController
{
    #[Route('/page', condition: "service('device').isSmartphone()")]
    public function index(): Response
    {
        // this endpoint is available only for smartphone devices
    }
}

解析自定义请求

您可能想解析除当前请求以外的其他请求。此捆绑包提供了一个实现 DeviceDetectorFactoryInterface 的服务。此服务提供根据 acsiomatic_device_detector 部分下的配置创建新鲜 DeviceDetector 实例的方法,但它不会将它们附加到任何请求。

自定义解析器

您可以通过提供它们作为服务来将自定义解析器注入到 DeviceDetector 中。

如果启用 autoconfigure,则捆绑包会注入自定义解析器。否则,您需要为每个自定义解析器添加相应的标签

  • acsiomatic.device_detector.bot_parser
  • acsiomatic.device_detector.client_parser
  • acsiomatic.device_detector.device_parser

发布策略

每次只有一个维护分支。此分支针对次要版本。

当发布新的次要版本时,维护版本达到其生命周期的结束。

依赖兼容策略

此库与 PHPDevice DetectorSymfony 的维护版本兼容。