Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maryo committed Oct 21, 2016
0 parents commit 0e1d765
Show file tree
Hide file tree
Showing 18 changed files with 3,269 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/vendor/
/composer.phar

.DS_Store
*.swp
*.swo
.local.vimrc
.vimrc.local
*~
/.idea/
/nbproject/
/local/
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php

cache:
directories:
$HOME/.composer/cache/files

php:
- 7.0

install:
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer self-update
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer require satooshi/php-coveralls
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer update

script:
- php -n ~/.phpenv/versions/$(phpenv version-name)/bin/composer lint
- phpunit --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php -n vendor/bin/coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Vanio Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Doctrine Domain Events

[![Build Status](https://travis-ci.org/vaniocz/doctrine-domain-events.svg?branch=master)](https://travis-ci.org/vaniocz/doctrine-domain-events)
[![Coverage Status](https://coveralls.io/repos/github/vaniocz/doctrine-domain-events/badge.svg?branch=master)](https://coveralls.io/github/vaniocz/doctrine-domain-events?branch=master)
![PHP7](https://img.shields.io/badge/php-7-6B7EB9.svg)
[![License](https://poser.pugx.org/vanio/doctrine-domain-events/license)](https://github.com/vaniocz/doctrine-domain-events/blob/master/LICENSE)

Very lightweight extension for Doctrine2 ORM allowing usage of domain events together with an ability to call flush
inside it's listeners. This library is inspired in Beberlei's
[Doctrine and Domain Events article](http://www.whitewashing.de/2013/07/24/doctrine_and_domainevents.html).

# Installation
Installation can be done as usually using composer.
`composer require vanio/doctrine-domain-events`

You also need to register an instance of `Vanio\DoctrineDomainEvents\DoctrineDomainEventDispatcher` inside Doctrine's
event manager.

```php
use Vanio\DoctrineDomainEvents\DoctrineDomainEventDispatcher;

$eventManager->addSubscriber(new DoctrineDomainEventDispatcher);
```

# Usage

## Setting up your entity
First make sure your entity implements `Vanio\DoctrineDomainEvent` interface and uses the
`Vanio\DoctrineDomainEvent\EventProviderTrait`.
```php
use Vanio\DoctrineDomainEvent\EventProvider;
use Vanio\DoctrineDomainEvent\EventProviderTrait;

class MyEntity implements EventProvider
{
use EventProviderTrait;
}
```

## Raising domain events
To raise a domain event from your entity use the `raise` method. This method expects either an instance of
`Vanio\DoctrineDomainEvent\DomainEvent` or just a string representing the domain event name (and optional array of
properties). In case of passing a string it will be turned into an instance of the `DomainEvent` class. If you prefer to
create your own event classes you can do so by extending the `DomainEvent` class, just don't forget to extend the `name`
method. The event is dispatched at the end of transaction once your entity has been flushed and all the changes are
projected into database so it is possible to both perform database queries over the changes as well as cancel the
transaction. Event names will be used as method names of corresponding listeners because this implementation uses the
built-in Doctrine event manager to keep it simple thus you need to make sure all your **event names are globally
unique**.

```php
use Vanio\DoctrineDomainEvent\EventProvider;
use Vanio\DoctrineDomainEvent\EventProviderTrait;

class Article implements EventProvider
{
use EventProviderTrait;

const EVENT_ARTICLE_PUBLISHED = 'onArticlePublish';

// ...

public function publish()
{
$this->raise(self::EVENT_ARTICLE_PUBLISHED, ['property' => 'value']);
}
}
```

## Listening to domain events
Listening to domain events is done the same way as listening to standard Doctrine events because it uses the same event
manager. You need to either prepare a listener and register it to appropriate domain event yourself or you can use a
subscriber that know which events it listens to. More information about the event system can be found inside
[Doctrine documentation](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html).

### Listening to domain events using a listener
```php
class Listener
{
public function onArticlePublish(DomainEvent $event)
{
// ...
}
}

$eventManager->addListener(Article::EVENT_ARTICLE_PUBLISHED, new Listener);
```

### Listening to domain events using a subscriber
```php
use Doctrine\Common\EventSubscriber;

class Subscriber implements EventSubscriber
{
public function onArticlePublish(DomainEvent $event)
{
// ...
}

public function getSubscribedEvents(): array
{
return [Article::EVENT_ARTICLE_PUBLISHED];
}
}

$eventManager->addSubscriber(new Subscriber);
```
41 changes: 41 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "vanio/doctrine-domain-events",
"description": "An extension for Doctrine2 ORM allowing usage of domain events and an ability to call flush inside events triggered by flush.",
"keywords": ["doctrine", "doctrine2", "domain events"],
"homepage": "https://github.com/vaniocz/doctrine-domain-events",
"license": "MIT",
"authors": [
{"name": "Marek Štípek", "email": "marek.stipek@vanio.cz"}
],
"require": {
"php": "7.0.*",
"doctrine/orm": "^2.5",
"vanio/stdlib": "^0.1@dev",
"vanio/type-parser": "^0.1@dev"
},
"require-dev": {
"doctrine/orm": "dev-master#9da83cf",
"phpunit/phpunit": "^5.5",
"vanio/coding-standards": "^0.1@dev"
},
"autoload": {
"psr-4": {
"Vanio\\DoctrineDomainEvents\\": "src/"
},
"exclude-from-classmap": ["/tests/"]
},
"autoload-dev": {
"psr-0": {"Doctrine\\Tests\\": "vendor/doctrine/orm/tests/"},
"psr-4": {"Vanio\\DoctrineDomainEvents\\Tests\\": "tests/"}
},
"scripts": {
"test": "\"vendor/bin/phpunit\"",
"lint": "\"vendor/bin/phpcs\" --standard=ruleset.xml --extensions=php --encoding=utf-8 --ignore=vendor .",
"fix": "\"vendor/bin/phpcbf\" --standard=ruleset.xml --extensions=php --encoding=utf-8 --no-patch --ignore=vendor ."
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
}
}
}

0 comments on commit 0e1d765

Please sign in to comment.