remyb98/object-tracker
最新稳定版本:v1.0.1
Composer 安装命令:
composer require remyb98/object-tracker
包简介
Track changes on objects
README 文档
README
Object tracker is a PHP trait that allows you to track changes in a class.
It captures the initial state of an object and detects modifications on properties annotated with the #[Track] attribute.
Features
m
- Track changes in object properties
- Supports PHP 8+ attributes
- Use aliases or a display property for objects
commit()function to update the snapshot
Installation
Install via Composer:
composer require remyb98/object-tracker
Usage
Declare a class
Use the Trackable trait in your class:
<?php use App\Trait\Trackable; use App\Attribute\Track; class User { use Trackable; #[Track] private string $name; #[Track(alias: "user_email")] private string $email; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } }
Take a snapshot
<?php $user = new User("Alice", "alice@example.com"); $user->snapshot(); ### Detect changes <?php $user->name = "Bob"; $changes = $user->getChanges(); /* $changes = [ 'name' => [ 'before' => 'Alice', 'after' => 'Bob', ] ]; */
Commit changes
<?php $user->commit(); // Updates the snapshot with the current state
Advanced Usage
You can track object properties and display a specific attribute instead of the full object.
<?php use App\Trait\Trackable; use App\Attribute\Track; class Address { public string $street; public string $city; public function __construct(string $street, string $city) { $this->street = $street; $this->city = $city; } } class User { use Trackable; #[Track(display: "city")] private Address $address; public function __construct(Address $address) { $this->address = $address; } } $address = new Address("123 Main St", "Paris"); $user = new User($address); $user->snapshot(); $address->city = "Lyon"; $changes = $user->getChanges(); /* $changes = [ 'address' => [ 'before' => 'Paris', 'after' => 'Lyon' ] ]; */
Notes:
- The display option in
#[Track]tells Trackable which property of the object to show instead of the object itself. - If the object has a
__toString()method, it will be used automatically. - Otherwise, the class name will be displayed in square brackets (e.g.,
[Address]).
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-22