tourze / doctrine-hostname-bundle
记录Hostname
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-hostname-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/framework-bundle: ^7.3
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- 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
- symfony/phpunit-bridge: ^7.3
- tourze/phpunit-symfony-kernel-test: 1.0.*
- tourze/phpunit-symfony-unit-test: 1.*
This package is auto-updated.
Last update: 2025-11-01 19:14:14 UTC
README
A Symfony bundle that automatically records the hostname when creating or updating Doctrine entities. This bundle helps track which server handled entity operations in distributed systems.
Features
- Automatic hostname recording: Captures server hostname during entity persistence
- Attribute-based configuration: Uses PHP 8.1 attributes for clean, declarative setup
- Separate creation and update tracking: Different attributes for creation vs update operations
- Non-intrusive: Doesn't overwrite existing values if already set
- Logger integration: Optional debug logging for troubleshooting
- High performance: Minimal overhead with
-99priority to run after other listeners
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine Bundle 2.13 or higher
- Doctrine ORM 3.0 or higher
Installation
composer require tourze/doctrine-hostname-bundle
The bundle will be automatically registered in your Symfony application.
Quick Start
Add the attributes to your entity properties:
<?php use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineHostnameBundle\Attribute\CreatedInHostColumn; use Tourze\DoctrineHostnameBundle\Attribute\UpdatedInHostColumn; #[ORM\Entity] class Product { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private ?int $id = null; #[ORM\Column(type: 'string', length: 255)] private ?string $name = null; #[CreatedInHostColumn] #[ORM\Column(type: 'string', length: 255, nullable: true)] private ?string $createdInHost = null; #[UpdatedInHostColumn] #[ORM\Column(type: 'string', length: 255, nullable: true)] private ?string $updatedInHost = null; // Getters and setters... public function getCreatedInHost(): ?string { return $this->createdInHost; } public function setCreatedInHost(?string $createdInHost): void { $this->createdInHost = $createdInHost; } public function getUpdatedInHost(): ?string { return $this->updatedInHost; } public function setUpdatedInHost(?string $updatedInHost): void { $this->updatedInHost = $updatedInHost; } }
How It Works
The bundle automatically:
- On entity creation: Sets
createdInHostto the current server hostname using PHP'sgethostname()function - On entity update: Sets
updatedInHostto the current server hostname when entity data changes - Preserves existing values: Won't overwrite hostname if already set (useful for manual assignments)
- Logs operations: Provides debug logging for monitoring hostname assignments
Use Cases
- Distributed systems: Track which server processed specific entities
- Load balancing: Monitor entity operations across multiple application servers
- Auditing: Maintain hostname records for compliance and troubleshooting
- Performance monitoring: Analyze entity operations by server location
Advanced Usage
Custom Property Names
You can use any property name with the attributes:
class MyEntity { #[CreatedInHostColumn] private ?string $originServer = null; #[UpdatedInHostColumn] private ?string $lastModifiedServer = null; }
Conditional Hostname Setting
The bundle respects existing values:
$entity = new Product(); $entity->setCreatedInHost('manual-override'); $entityManager->persist($entity); $entityManager->flush(); // createdInHost will remain 'manual-override'
Configuration
The bundle requires no configuration and works out of the box. It automatically registers:
HostListeneras a Doctrine event subscriber- Custom
PropertyAccessorservice for safe property access - Debug logging integration (if logger is available)
Testing
Run the test suite:
./vendor/bin/phpunit packages/doctrine-hostname-bundle/tests
The bundle includes comprehensive unit and integration tests covering:
- Attribute functionality
- Event listener behavior
- Service container integration
- Hostname recording logic
Performance Considerations
- Minimal overhead: Uses reflection only during entity operations
- Efficient execution: Runs with
-99priority to execute after other listeners - Memory conscious: Properly handles PropertyAccessor exceptions
- Change detection: Only updates hostname when entity data actually changes
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Please follow PSR-12 coding standards and include appropriate tests.
License
The MIT License (MIT). Please see License File for more information.
Changelog
See [CHANGELOG.md] for version history and breaking changes (if available).