event-manager Documentation

The Doctrine Event Manager is a library that provides a simple event system.

6,040 stars PHP PHP 1 file ยท ~3,304 tokens #event#eventmanager
RAW Doc

File: README.md

Doctrine Event Manager



The Doctrine Event Manager is a library that provides a simple event system.

More resources:


File: composer.json

{
"name": "doctrine/event-manager",
"description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.",
"license": "MIT",
"type": "library",
"keywords": [
"events",
"event",
"event dispatcher",
"event manager",
"event system"
],
"authors": [
{
"name": "Guilherme Blanco",
"email": "[email protected]"
},
{
"name": "Roman Borschel",
"email": "[email protected]"
},
{
"name": "Benjamin Eberlei",
"email": "[email protected]"
},
{
"name": "Jonathan Wage",
"email": "[email protected]"
},
{
"name": "Johannes Schmitt",
"email": "[email protected]"
},
{
"name": "Marco Pivetta",
"email": "[email protected]"
}
],
"homepage": "https://www.doctrine-project.org/projects/event-manager.html",
"require": {
"php": "^8.1"
},
"require-dev": {
"doctrine/coding-standard": "^14",
"phpdocumentor/guides-cli": "^1.4",
"phpstan/phpstan": "^2.1.32",
"phpunit/phpunit": "^10.5.58"
},
"conflict": {
"doctrine/common": "<2.9"
},
"autoload": {
"psr-4": {
"Doctrine\Common\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Doctrine\Tests\Common\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true
},
"scripts": {
"docs": "composer --working-dir docs update && ./docs/vendor/bin/build-docs.sh @additional_args"
}
}


File: SECURITY.md

Security

If you find a Security bug in Doctrine, please follow our
Security reporting guidelines.

Security vulnerabilities should be reported to [email protected]
and not posted on the GitHub issue tracker of the project.


File: UPGRADE.md

Upgrade to 2.0

Made the `$event` parameter of `EventManager::getListeners()` mandatory

When calling EventManager::getListeners() you need to specify the event that
you want to fetch the listeners for. Call getAllListeners() instead if you
want to access the listeners of all events.

Upgrade to 1.2

Deprecated calling `EventManager::getListeners()` without an event name

When calling EventManager::getListeners() without an event name, all
listeners were returned, keyed by event name. A new method getAllListeners()
has been added to provide this functionality. It should be used instead.


File: .doctrine-project.json

{
"active": true,
"name": "Event Manager",
"slug": "event-manager",
"docsSlug": "doctrine-event-manager",
"versions": [
{
"name": "3.0",
"branchName": "3.0.x",
"slug": "latest",
"upcoming": true
},
{
"name": "2.1",
"branchName": "2.1.x",
"current": true,
"slug": "2.1"
},
{
"name": "2.0",
"slug": "2.0",
"maintained": false
},
{
"name": "1.2",
"slug": "1.2",
"maintained": false
},
{
"name": "1.1",
"slug": "1.1",
"maintained": false
}
]
}


File: .scrutinizer.yml

build:
nodes:
analysis:
environment:
php:
version: 7.1
cache:
disabled: false
directories:
- ~/.composer/cache
project_setup:
override: true
tests:
override:
- php-scrutinizer-run
- phpcs-run
dependencies:
override:
- composer install -noa

tools:
external_code_coverage:
timeout: 600

build_failure_conditions:
- 'elements.rating(<= C).new.exists' # No new classes/methods with a rating of C or worse allowed
- 'issues.label("coding-style").new.exists' # No new coding style issues allowed
- 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity
- 'project.metric_change("scrutinizer.test_coverage", < 0)' # Code Coverage decreased from previous inspection


File: docs/composer.json

{
"require": {
"doctrine/docs-builder": "^1.0"
}
}


File: docs/en/index.rst

Event Manager Documentation

The Doctrine Event Manager documentation is a reference guide to everything you need
to know about the project.

Getting Help

If this documentation is not helping to answer questions you have about the
Doctrine DBAL, don't panic. You can get help from different sources:

  • Slack chat room #event-manager <https://www.doctrine-project.org/slack>_
  • On Stack Overflow <http://stackoverflow.com/questions/tagged/doctrine-event-manager>_
  • The Doctrine Mailing List <http://groups.google.com/group/doctrine-user>_
  • Report a bug on GitHub <https://github.com/doctrine/event-manager/issues>_.

Getting Started

The best way to get started is with the :doc:Introduction <reference/index#introduction> section
in the documentation. Use the sidebar to browse other documentation for the Doctrine Event Manager.


File: docs/en/sidebar.rst

:orphan:

.. toctree::
:depth: 3

text
reference/index

File: docs/en/reference/index.rst

Introduction

The Doctrine Event Manager is a simple event system used by the various Doctrine projects. It was originally built
for the DBAL and ORM but over time other projects adopted it and now it is available as a standalone library.

Installation

The library can easily be installed with composer.

.. code-block:: sh

text
$ composer require doctrine/event-manager

Setup

The event system is controlled by the Doctrine\Common\EventManager class.

.. code-block:: php

text
use Doctrine\Common\EventManager;

$eventManager = new EventManager();

Listeners

Now you are ready to listen for events. Here is an example of a custom event listener named TestEvent.

.. code-block:: php

text
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;

final class TestEvent
{
    public const preFoo = 'preFoo';
    public const postFoo = 'postFoo';

    /** @var EventManager */
    private $eventManager;

    /** @var bool */
    public $preFooInvoked = false;

    /** @var bool */
    public $postFooInvoked = false;

    public function __construct(EventManager $eventManager)
    {
        $eventManager->addEventListener([self::preFoo, self::postFoo], $this);
    }

    public function preFoo(EventArgs $eventArgs) : void
    {
        $this->preFooInvoked = true;
    }

    public function postFoo(EventArgs $eventArgs) : void
    {
        $this->postFooInvoked = true;
    }
}

// Create a new instance
$testEvent = new TestEvent($eventManager);

Dispatching Events

Now you can dispatch events with the dispatchEvent() method.

.. code-block:: php

text
$eventManager->dispatchEvent(TestEvent::preFoo);
$eventManager->dispatchEvent(TestEvent::postFoo);

Removing Event Listeners

You can easily remove a listener with the removeEventListener() method.

.. code-block:: php

text
$eventManager->removeEventListener([TestEvent::preFoo, TestEvent::postFoo], $testEvent);

Event Subscribers

The Doctrine event system also has a simple concept of event subscribers. We can define a simple TestEventSubscriber class which implements the Doctrine\Common\EventSubscriber interface with a getSubscribedEvents() method which returns an array of events it should be subscribed to.

.. code-block:: php

text
use Doctrine\Common\EventSubscriber;

final class TestEventSubscriber implements EventSubscriber
{
    /** @var bool */
    public $preFooInvoked = false;

    public function preFoo() : void
    {
        $this->preFooInvoked = true;
    }

    public function getSubscribedEvents() : array
    {
        return [TestEvent::preFoo];
    }
}

$eventSubscriber = new TestEventSubscriber();
$eventManager->addEventSubscriber($eventSubscriber);

.. note::

text
The array returned by the ``getSubscribedEvents()`` method is a simple array with the values being the event names. The subscriber must have a method that is named exactly like the event.

Now when you dispatch an event, any event subscribers will be notified of that event.

.. code-block:: php

text
$eventManager->dispatchEvent(TestEvent::preFoo);

Now you can check the preFooInvoked property to see if the event subscriber was notified of the event:

.. code-block:: php

text
if ($eventSubscriber->preFooInvoked) {
    // the preFoo method was invoked
}

File: .github/dependabot.yml

version: 2
updates:

  • package-ecosystem: "github-actions"
    directory: "/"
    schedule:
    interval: "weekly"
    labels:
    • "CI"
      groups:
      doctrine:
      patterns:
      • "doctrine/*"

File: .github/FUNDING.yml

patreon: phpdoctrine
tidelift: packagist/doctrine%2Fevent-manager
custom: https://www.doctrine-project.org/sponsorship.html


File: .github/workflows/coding-standards.yml

name: "Coding Standards"

on:
pull_request:
branches:
- ".x"
push:
branches:
- "
.x"

jobs:
coding-standards:
name: "Coding Standards"
uses: "doctrine/.github/.github/workflows/[email protected]"


File: .github/workflows/composer-lint.yml

name: "Composer Lint"

on:
pull_request:
branches:
- ".x"
paths:
- "composer.json"
push:
branches:
- "
.x"
paths:
- "composer.json"

jobs:
composer-lint:
name: "Composer Lint"
uses: "doctrine/.github/.github/workflows/[email protected]"


File: .github/workflows/continuous-integration.yml

name: "Continuous Integration"

on:
pull_request:
branches:
- ".x"
push:
branches:
- "
.x"

jobs:
phpunit:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/[email protected]"
with:
php-versions: '["8.1", "8.2", "8.3", "8.4", "8.5"]'
secrets:
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"


File: .github/workflows/documentation.yml

name: "Documentation"

on:
pull_request:
branches:
- ".x"
paths:
- ".github/workflows/documentation.yml"
- "docs/**"
push:
branches:
- "
.x"
paths:
- ".github/workflows/documentation.yml"
- "docs/**"

jobs:
documentation:
name: "Documentation"
uses: "doctrine/.github/.github/workflows/[email protected]"


File: .github/workflows/release-on-milestone-closed.yml

name: "Automatic Releases"

on:
milestone:
types:
- "closed"

jobs:
release:
name: "Git tag, release & create merge-up PR"
uses: "doctrine/.github/.github/workflows/[email protected]"
secrets:
GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }}
GIT_AUTHOR_NAME: ${{ secrets.GIT_AUTHOR_NAME }}
ORGANIZATION_ADMIN_TOKEN: ${{ secrets.ORGANIZATION_ADMIN_TOKEN }}
SIGNING_SECRET_KEY: ${{ secrets.SIGNING_SECRET_KEY }}


File: .github/workflows/static-analysis.yml

name: "Static Analysis"

on:
pull_request:
branches:
- ".x"
push:
branches:
- "
.x"

jobs:
static-analysis:
name: "Static Analysis"
uses: "doctrine/.github/.github/workflows/[email protected]"


File: .github/workflows/website-schema.yml

name: "Website config validation"

on:
pull_request:
branches:
- ".x"
paths:
- ".doctrine-project.json"
- ".github/workflows/website-schema.yml"
push:
branches:
- "
.x"
paths:
- ".doctrine-project.json"
- ".github/workflows/website-schema.yml"

jobs:
json-validate:
name: "Validate JSON schema"
uses: "doctrine/.github/.github/workflows/[email protected]"