tourze / doctrine-user-agent-bundle
A Symfony bundle that automatically captures and records User-Agent information in Doctrine entities for analytics and audit purposes
Installs: 13 823
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-user-agent-bundle
Requires
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- psr/log: ^3|^2|^1
- symfony/config: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/event-dispatcher: ^7.3
- symfony/framework-bundle: ^7.3
- symfony/http-foundation: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/service-contracts: ^3.6
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-entity-checker-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
README
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
- Add the bundle to your application's kernel:
// config/bundles.php return [ // ... Tourze\DoctrineUserAgentBundle\DoctrineUserAgentBundle::class => ['all' => true], ];
- 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... }
- 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
- Request Capture: The bundle listens to Symfony's
KernelEvents::REQUESTand captures theUser-Agentheader - Entity Events: When Doctrine triggers
prePersistorpreUpdateevents, the bundle checks for marked properties - Value Assignment: Only assigns User-Agent values to properties that are currently
null - 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.