https://github.com/bpolaszek/doctrine-watcher
Watch changes in your Doctrine entities to let you trigger custom events.
https://github.com/bpolaszek/doctrine-watcher
Last synced: 11 months ago
JSON representation
Watch changes in your Doctrine entities to let you trigger custom events.
- Host: GitHub
- URL: https://github.com/bpolaszek/doctrine-watcher
- Owner: bpolaszek
- License: mit
- Created: 2018-11-22T16:08:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-09T14:10:33.000Z (over 6 years ago)
- Last Synced: 2025-02-12T02:17:14.281Z (11 months ago)
- Language: PHP
- Size: 15.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/bentools/doctrine-watcher)
[](https://packagist.org/packages/bentools/doctrine-watcher)
[](https://travis-ci.org/bpolaszek/doctrine-watcher)
[](https://scrutinizer-ci.com/g/bpolaszek/doctrine-watcher)
[](https://packagist.org/packages/bentools/doctrine-watcher)
# Doctrine Watcher
This little library will help you to monitor changes on Doctrine insertions and/or updates, for specific classes, for specific properties.
## Usage
```php
use App\Entity\User;
use BenTools\DoctrineWatcher\Changeset\PropertyChangeset;
use BenTools\DoctrineWatcher\Watcher\DoctrineWatcher;
/**
* Instanciate watcher
*/
$watcher = new DoctrineWatcher();
/**
* Register it as an event subscriber
* @var \Doctrine\Common\EventManager $eventManager
*/
$eventManager->addEventSubscriber($watcher);
/**
* Watch for changes on the $email property for the User class
*/
$watcher->watch(User::class, 'email', function (
PropertyChangeset $changeset,
string $operationType,
User $user
) {
if (!$changeset->hasChanges()) {
return;
}
vprintf('Changed email from %s to %s for user %s' . PHP_EOL, [
$changeset->getOldValue(),
$changeset->getNewValue(),
$user->getName(),
]);
});
/**
* Watch for changes on the $roles property for the User class
*/
$watcher->watch(User::class, 'roles', function (
PropertyChangeset $changeset,
string $operationType,
User $user
) {
if ($changeset::INSERT === $operationType) {
return;
}
if ($changeset->hasAdditions()) {
vprintf('Roles %s were granted for user %s' . PHP_EOL, [
implode(', ', $changeset->getAdditions()),
$user->getName(),
]);
}
if ($changeset->hasRemovals()) {
vprintf('Roles %s were revoked for user %s' . PHP_EOL, [
implode(', ', $changeset->getRemovals()),
$user->getName(),
]);
}
});
```
## Installation
PHP7.1+ is required.
```bash
composer require bentools/doctrine-watcher:0.2.*
```
## Tests
> ./vendor/bin/phpunit
## F.A.Q.
### Can I also trigger callable on insertions ?
```php
$watcher = new DoctrineWatcher(['trigger_on_persist' => true]); // Will be default config for all watchers
```
or
```php
$watcher->watch(Entity::class, 'property', $callable, ['trigger_on_persist' => true]); // Will apply on this watcher only
```
### How do I trigger something even when there are no changes?
```php
$watcher = new DoctrineWatcher(['trigger_when_no_changes' => true]); // Will be default config
```
or
```php
$watcher->watch(Entity::class, 'property', $callable, ['trigger_when_no_changes' => true]); // Will apply on this watcher only
```
### When are the callback triggered?
On `postPersist` and `postUpdate` events.
## License
MIT
## See also
[bentools/doctrine-watcher-bundle](https://github.com/bpolaszek/doctrine-watcher-bundle) - A Symfony Bundle for this library