定制 tourze/doctrine-user-agent-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tourze/doctrine-user-agent-bundle

Composer 安装命令:

composer require tourze/doctrine-user-agent-bundle

包简介

A Symfony bundle that automatically captures and records User-Agent information in Doctrine entities for analytics and audit purposes

README 文档

README

English | 中文

Latest Version Total Downloads PHP Version Require License Code Coverage

A Symfony bundle that automatically captures and records User-Agent information in Doctrine entities for analytics and audit purposes.

Features

  • 🚀 Zero Configuration - Works out of the box with simple attributes
  • 📱 Auto-Detection - Automatically captures User-Agent from HTTP requests
  • 🏷️ Attribute-Based - Simple PHP 8+ attributes to mark entity properties
  • Event-Driven - Seamless integration with Doctrine ORM lifecycle events
  • 🔄 Create & Update - Separate tracking for entity creation and modification
  • 🧩 Ready-to-Use Traits - Pre-built traits for common use cases
  • 🔒 Non-Intrusive - Only sets values when properties are null

Installation

composer require tourze/doctrine-user-agent-bundle

Quick Start

  1. Add the bundle to your application's kernel:
// config/bundles.php
return [
    // ...
    Tourze\DoctrineUserAgentBundle\DoctrineUserAgentBundle::class => ['all' => true],
];
  1. Use attributes in your entity:
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Tourze\DoctrineUserAgentBundle\Attribute\CreateUserAgentColumn;
use Tourze\DoctrineUserAgentBundle\Attribute\UpdateUserAgentColumn;

#[ORM\Entity]
class Article
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[CreateUserAgentColumn]
    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $createdUserAgent = null;

    #[UpdateUserAgentColumn]
    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $updatedUserAgent = null;

    // Getters and setters...
}
  1. Or use the convenience trait:
use Tourze\DoctrineUserAgentBundle\Traits\CreatedByUAAware;

#[ORM\Entity]
class Article
{
    use CreatedByUAAware;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    // Your other properties...
}

Usage

Available Attributes

#[CreateUserAgentColumn]

Records the User-Agent header when the entity is first persisted to the database.

#[CreateUserAgentColumn]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $createdUserAgent = null;

#[UpdateUserAgentColumn]

Records the User-Agent header when the entity is updated.

#[UpdateUserAgentColumn]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $updatedUserAgent = null;

Available Traits

CreatedByUAAware

A ready-to-use trait that adds a createdFromUa property with proper Doctrine mapping:

use Tourze\DoctrineUserAgentBundle\Traits\CreatedByUAAware;

class MyEntity
{
    use CreatedByUAAware;
    
    // Access the User-Agent
    public function getCreatedFromUa(): ?string
    {
        return $this->createdFromUa;
    }
}

How It Works

  1. Request Capture: The bundle listens to Symfony's KernelEvents::REQUEST and captures the User-Agent header
  2. Entity Events: When Doctrine triggers prePersist or preUpdate events, the bundle checks for marked properties
  3. Value Assignment: Only assigns User-Agent values to properties that are currently null
  4. Non-Intrusive: Existing values are never overwritten

Use Cases

  • Analytics: Track which browsers/devices are creating content
  • Audit Logging: Maintain detailed records of entity modifications
  • Security: Monitor suspicious User-Agent patterns
  • User Experience: Understand your users' technology preferences

Requirements

  • PHP 8.1 or higher
  • Symfony 6.4 or higher
  • Doctrine ORM 3.0 or higher
  • Doctrine DBAL 4.0 or higher
  • Doctrine Bundle 2.13 or higher

Contributing

Please see CONTRIBUTING.md for details.

License

The MIT License (MIT). Please see License File for more information.

tourze/doctrine-user-agent-bundle 适用场景与选型建议

tourze/doctrine-user-agent-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tourze/doctrine-user-agent-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tourze/doctrine-user-agent-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 tourze/doctrine-user-agent-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-07