tourze / doctrine-ip-bundle
Doctrine+IP
Installs: 17 844
Dependents: 36
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-ip-bundle
Requires
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- monolog/monolog: ^3.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 tracks and records IP addresses for entity creation and updates using PHP 8.1 attributes. This bundle provides seamless integration with Doctrine ORM to capture client IP addresses during entity lifecycle events.
Features
- π Automatic IP Tracking: Automatically captures client IP addresses during entity creation and updates
- π·οΈ PHP 8.1 Attributes: Uses modern PHP attributes for simple, declarative configuration
- π Private Property Support: Works with private properties through Symfony's PropertyAccessor
- π§΅ Thread-Safe: Implementation with ResetInterface for proper request isolation
- π Seamless Integration: Integrates with Symfony's request cycle and Doctrine lifecycle events
- π¦ Zero Configuration: Works out of the box with no additional setup required
- π― Flexible: Supports both individual attributes and convenient traits
Installation
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM 2.20/3.0 or higher
- Doctrine Bundle 2.13 or higher
Install via Composer
composer require tourze/doctrine-ip-bundle
The bundle will be automatically registered thanks to Symfony Flex.
Quick Start
Method 1: Using Individual Attributes
Add the appropriate attributes to your entity properties:
use Tourze\DoctrineIpBundle\Attribute\CreateIpColumn; use Tourze\DoctrineIpBundle\Attribute\UpdateIpColumn; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class YourEntity { // This property will store the IP address when the entity is created #[CreateIpColumn] #[ORM\Column(length: 45, nullable: true)] private ?string $createIp = null; // This property will store the IP address when the entity is updated #[UpdateIpColumn] #[ORM\Column(length: 45, nullable: true)] private ?string $updateIp = null; // Getters and setters public function getCreateIp(): ?string { return $this->createIp; } public function getUpdateIp(): ?string { return $this->updateIp; } }
Method 2: Using Convenient Traits
For easier implementation, use the provided traits:
use Tourze\DoctrineIpBundle\Traits\IpTraceableAware; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class YourEntity { use IpTraceableAware; // Your other properties... }
Or use the creation-only trait:
use Tourze\DoctrineIpBundle\Traits\CreatedFromIpAware; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class YourEntity { use CreatedFromIpAware; // Your other properties... }
Available Attributes and Traits
Attributes
- #[CreateIpColumn]: Marks a property to be automatically populated with the client IP address when the entity is first persisted
- #[UpdateIpColumn]: Marks a property to be automatically populated with the client IP address when the entity is updated
Traits
- IpTraceableAware: Provides both- $createdFromIpand- $updatedFromIpproperties with getters and setters
- CreatedFromIpAware: Provides only- $createdFromIpproperty with getter and setter
Both traits include the proper Doctrine ORM mapping annotations.
How It Works
The bundle works by listening to Doctrine's lifecycle events:
- It registers event listeners for Doctrine's prePersistandpreUpdateevents
- It captures the client IP from the request through Symfony's kernel.requestevent
- When an entity is created or updated, it checks for properties with the appropriate attributes
- If found, it automatically sets the client IP to those properties
Configuration
The bundle is auto-configured and requires no additional configuration. It automatically:
- Registers the IP tracking listener with Doctrine
- Configures the PropertyAccessor for private property access
- Sets up the request listener to capture client IP addresses
Advanced Usage
Custom IP Detection
The bundle automatically detects client IP addresses using Symfony's Request::getClientIp() method, which handles:
- Standard REMOTE_ADDRheader
- Proxy headers like X-Forwarded-For
- Load balancer headers like X-Real-IP
Thread Safety
The bundle implements Symfony's ResetInterface to ensure proper request isolation
in long-running processes like ReactPHP or Swoole.
Existing Values
The bundle respects existing values - if a property already has a value, it will not be overwritten.
Dependencies
This bundle automatically requires and configures:
- tourze/doctrine-entity-checker-bundle- For entity validation and checking capabilities
License
This bundle is licensed under the MIT License.