<commit_before># The event system

DunglasApiBundle leverages the [Symfony Event Dispatcher Component](http://symfony.com/doc/current/components/event_dispatcher/index.html)
to provide a powerful event system triggered in the object lifecycle.

Additionally to [events triggered by the Symfony HTTP Kernel](http://symfony.com/doc/current/components/http_kernel/introduction.html#creating-an-event-listener)
and [by Doctrine ORM](http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#reference-events-lifecycle-events)
(if you use it), DunglasApiBundle comes with its own set of events occurring during the handling of the request:

## Retrieving list

- `api.retrieve_list` (`Dunglas\ApiBundle\Event::RETRIEVE_LIST`): occurs after the retrieving of an object list during a `GET` request on a collection.

## Retrieving item

- `api.retrieve` (`Dunglas\ApiBundle\Event::RETRIEVE_LIST`): after the retrieving of an object during a `GET` request on an item.

## Creating item

- `api.pre_create_validation` (`Dunglas\ApiBundle\Event::PRE_CREATE_VALIDATION`): occurs before the object validation during a `POST` request.
- `api.pre_create` (`Dunglas\ApiBundle\Event::PRE_CREATE`): occurs after the object validation and before its persistence during a `POST` request
- `api.post_create` (`Dunglas\ApiBundle\Event::POST_CREATE`): event occurs after the object persistence during `POST` request

## Updating item

- `api.pre_update_validation` (`Dunglas\ApiBundle\Event::PRE_UPDATE_VALIDATION`): event occurs before the object validation during a `PUT` request.
- `api.pre_update` (`Dunglas\ApiBundle\Event::PRE_UPDATE`): occurs after the object validation and before its persistence during a `PUT` request
- `api.post_update` (`Dunglas\ApiBundle\Event::POST_UPDATE`): event occurs after the object persistence during a `PUT` request

## Deleting item

- `api.pre_delete` (`Dunglas\ApiBundle\Event::PRE_DELETE`): event occurs before the object deletion during a `DELETE` request
- `api.post_delete` (`Dunglas\ApiBundle\Event::POST_DELETE`): occurs after the object deletion during a `DELETE` request

## Registering an event listener

In the following example, we register an event listener that will be called every time after the creation of an object:

```php
<?php

// src/AppBundle/EventListener/MyEventListener.php

namespace AppBundle\EventListener;

use AppBundle\Entity\MyObject;
use Doctrine\Common\Persistence\ManagerRegistry;
use Dunglas\JsonApiBundle\Event\DataEvent;

class MyEventListener
{
    /**
     * @param ObjectEvent $event
     */
    public function onPostCreate(ObjectEvent $event)
    {
        $object = $event->getObject();

        if ($object instanceof MyObject) {
            $resource = $event->getResource(); // Get the related instance of Dunglas\ApiBundle\Api\ResourceInterface

            // Do something awesome here
        }
    }
}
```

```yaml
# app/config/services.yml
<commit_msg>Fixed code sample to have correct class for type-hint<commit_after># The event system

DunglasApiBundle leverages the [Symfony Event Dispatcher Component](http://symfony.com/doc/current/components/event_dispatcher/index.html)
to provide a powerful event system triggered in the object lifecycle.

Additionally to [events triggered by the Symfony HTTP Kernel](http://symfony.com/doc/current/components/http_kernel/introduction.html#creating-an-event-listener)
and [by Doctrine ORM](http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#reference-events-lifecycle-events)
(if you use it), DunglasApiBundle comes with its own set of events occurring during the handling of the request:

## Retrieving list

- `api.retrieve_list` (`Dunglas\ApiBundle\Event::RETRIEVE_LIST`): occurs after the retrieving of an object list during a `GET` request on a collection.

## Retrieving item

- `api.retrieve` (`Dunglas\ApiBundle\Event::RETRIEVE_LIST`): after the retrieving of an object during a `GET` request on an item.

## Creating item

- `api.pre_create_validation` (`Dunglas\ApiBundle\Event::PRE_CREATE_VALIDATION`): occurs before the object validation during a `POST` request.
- `api.pre_create` (`Dunglas\ApiBundle\Event::PRE_CREATE`): occurs after the object validation and before its persistence during a `POST` request
- `api.post_create` (`Dunglas\ApiBundle\Event::POST_CREATE`): event occurs after the object persistence during `POST` request

## Updating item

- `api.pre_update_validation` (`Dunglas\ApiBundle\Event::PRE_UPDATE_VALIDATION`): event occurs before the object validation during a `PUT` request.
- `api.pre_update` (`Dunglas\ApiBundle\Event::PRE_UPDATE`): occurs after the object validation and before its persistence during a `PUT` request
- `api.post_update` (`Dunglas\ApiBundle\Event::POST_UPDATE`): event occurs after the object persistence during a `PUT` request

## Deleting item

- `api.pre_delete` (`Dunglas\ApiBundle\Event::PRE_DELETE`): event occurs before the object deletion during a `DELETE` request
- `api.post_delete` (`Dunglas\ApiBundle\Event::POST_DELETE`): occurs after the object deletion during a `DELETE` request

## Registering an event listener

In the following example, we register an event listener that will be called every time after the creation of an object:

```php
<?php

// src/AppBundle/EventListener/MyEventListener.php

namespace AppBundle\EventListener;

use AppBundle\Entity\MyObject;
use Doctrine\Common\Persistence\ManagerRegistry;
use Dunglas\JsonApiBundle\Event\DataEvent;

class MyEventListener
{
    /**
     * @param DataEvent $event
     */
    public function onPostCreate(DataEvent $event)
    {
        $data = $event->getData();

        if ($data instanceof MyObject) {
            $resource = $event->getResource(); // Get the related instance of Dunglas\ApiBundle\Api\ResourceInterface

            // Do something awesome here
        }
    }
}
```

```yaml
# app/config/services.yml

{'fraction_non_alphanumeric': 0.10359443879281112, 'fraction_numerical': 0.0, 'mean_word_length': 4.887225548902196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4604873', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1668, 'n_words': 528}