Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed May 4, 2016
0 parents commit 6a4c8f2
Show file tree
Hide file tree
Showing 35 changed files with 1,064 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: tests/_log/coverage.xml
json_path: tests/_log/coveralls.json
8 changes: 8 additions & 0 deletions .gitattributes
@@ -0,0 +1,8 @@
.coveralls.yml export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php_cs export-ignore
.travis.yml export-ignore
codeception.yml export-ignore
docs export-ignore
tests export-ignore
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
/vendor
/tests/_log/*
/tests/_temp/*
/composer*.lock

.php_cs.cache

!.gitkeep
10 changes: 10 additions & 0 deletions .php_cs
@@ -0,0 +1,10 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests/integration')
;

return PhpCsFixer\Config::create()
->finder($finder)
;
27 changes: 27 additions & 0 deletions .travis.yml
@@ -0,0 +1,27 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm

before_install:
- travis_retry composer self-update

install:
- export PATH="$PATH:$HOME/.composer/vendor/bin"
- travis_retry composer global require fabpot/php-cs-fixer "^2.0.0@dev"
- travis_retry composer global require satooshi/php-coveralls "^1.0.0"
- travis_retry composer update --no-interaction --prefer-source

before_script:
- if [ $TRAVIS_PHP_VERSION != "hhvm" ]; then coverage="--coverage-xml"; fi
- vendor/bin/codecept build

script:
- php-cs-fixer fix --dry-run
- vendor/bin/codecept run $coverage

after_script:
- if [ $coverage != "" ]; then coveralls --verbose; fi
16 changes: 16 additions & 0 deletions codeception.yml
@@ -0,0 +1,16 @@
actor: Tester

paths:
tests: tests
log: tests/_log
data: tests/_data
support: tests/_helpers

settings:
bootstrap: _bootstrap.php
log: true

coverage:
enabled: true
include:
- src/*
37 changes: 37 additions & 0 deletions composer.json
@@ -0,0 +1,37 @@
{
"name": "arachne/event-dispatcher",
"description": "Integration of symfony/event-dispatcher to Nette framework.",
"type": "library",
"keywords": [ "nette", "arachne", "symfony", "events" ],
"license": "MIT",
"authors": [
{
"name": "Jáchym Toušek",
"homepage": "http://enumag.cz",
"email": "enumag@gmail.com"
}
],
"require": {
"php": ">=5.5.0",
"arachne/container-adapter": "^0.1.0|^0.2.0",
"nette/di": "^2.3.0",
"nette/utils": "^2.3.0",
"symfony/event-dispatcher": "^2.8.0|^3.0.0"
},
"require-dev": {
"arachne/bootstrap": "~0.2.1",
"codeception/codeception": "~2.2.0@rc",
"nette/application": "^2.3",
"kdyby/console": "^2.6"
},
"autoload": {
"psr-4": {
"Arachne\\EventDispatcher\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Integration\\": "tests/integration/src"
}
}
}
53 changes: 53 additions & 0 deletions docs/index.md
@@ -0,0 +1,53 @@
Documentation
====

This package integrates [symfony/event-dispatcher](https://github.com/symfony/event-dispatcher) to Nette Framework.

Installation
----

The best way to install Arachne/EventDispatcher is using [Composer](http://getcomposer.org/).

```sh
$ composer require arachne/event-dispatcher
```

Now add these extensions to your config.neon.

```yml
extensions:
arachne.containeradapter: Arachne/ContainerAdapter/DI/ContainerAdapterExtension
arachne.eventdispatcher: Arachne/EventDispatcher/DI/EventDispatcherExtension
```

See the documentation of [symfony/event-dispatcher](http://symfony.com/doc/current/components/event_dispatcher/index.html) for details.

Subscribers
----

To add register an event subscriber, add it to your config.neon.

```
services:
subscriber:
class: App\AdminModule\Event\Subscriber
tags:
- arachne.eventDispatcher.subscriber
```

You can also simplify it using the DecoratorExtension from Nette.

```
decorator:
Symfony\Component\EventDispatcher\EventSubscriberInterface:
tags:
- arachne.eventDispatcher.subscriber
services:
subscriber: App\AdminModule\Event\Subscriber
```

Application events
----

If you're using [nette/application](https://github.com/nette/application) the events in `Nette/Application/Application` will be fired by the event dispatcher as well. See the [ApplicationEvents](../src/ApplicationEvents.php) class for details.
10 changes: 10 additions & 0 deletions license.md
@@ -0,0 +1,10 @@
The MIT License (MIT)
====

Copyright (c) 2016 Jáchym Toušek (enumag@gmail.com)

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.
24 changes: 24 additions & 0 deletions readme.md
@@ -0,0 +1,24 @@
Arachne/EventDispatcher
====

[![Build Status](https://img.shields.io/travis/Arachne/EventDispatcher.svg?style=flat-square)](https://travis-ci.org/Arachne/EventDispatcher)
[![Coverage Status](https://img.shields.io/coveralls/Arachne/EventDispatcher.svg?style=flat-square)](https://coveralls.io/github/Arachne/EventDispatcher)
[![Latest stable](https://img.shields.io/packagist/v/arachne/event-dispatcher.svg?style=flat-square)](https://packagist.org/packages/arachne/event-dispatcher)
[![Downloads this Month](https://img.shields.io/packagist/dm/arachne/event-dispatcher.svg?style=flat-square)](https://packagist.org/packages/arachne/event-dispatcher)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/Arachne/EventDispatcher/blob/master/license.md)

Integration of symfony/event-dispatcher to Nette framework.

Installation
----

The best way to install Arachne/EventDispatcher is using [Composer](http://getcomposer.org/).

```sh
$ composer require arachne/event-dispatcher
```

Documentation
----

Learn more in the [documentation](docs/index.md).
67 changes: 67 additions & 0 deletions src/ApplicationEvents.php
@@ -0,0 +1,67 @@
<?php

/**
* This file is part of the Arachne
*
* Copyright (c) Jáchym Toušek (enumag@gmail.com)
*
* For the full copyright and license information, please view the file license.md that was distributed with this source code.
*/

namespace Arachne\EventDispatcher;

/**
* Events in Nette\Application\Application life cycle.
*
* @author Jáchym Toušek <enumag@gmail.com>
*/
class ApplicationEvents
{
/**
* The STARTUP event occurs before the application loads presenter.
*
* @see Nette\Application\Application::$onStartup
* @Event("Arachne\EventDispatcher\Event\ApplicationEvent")
*/
const STARTUP = 'nette.application.startup';

/**
* The SHUTDOWN event occurs before the application shuts down.
*
* @see Nette\Application\Application::$onShutdown
* @Event("Arachne\EventDispatcher\Event\ApplicationShutdownEvent")
*/
const SHUTDOWN = 'nette.application.shutdown';

/**
* The REQUEST event occurs when a new request is received.
*
* @see Nette\Application\Application::$onRequest
* @Event("Arachne\EventDispatcher\Event\ApplicationRequestEvent")
*/
const REQUEST = 'nette.application.request';

/**
* The PRESENTER event when a presenter is created.
*
* @see Nette\Application\Application::$onPresenter
* @Event("Arachne\EventDispatcher\Event\ApplicationPresenterEvent")
*/
const PRESENTER = 'nette.application.presenter';

/**
* The RESPONSE event occurs when a new response is ready for dispatch.
*
* @see Nette\Application\Application::$onResponse
* @Event("Arachne\EventDispatcher\Event\ApplicationResponseEvent")
*/
const RESPONSE = 'nette.application.response';

/**
* The ERROR event when an unhandled exception occurs in the application.
*
* @see Nette\Application\Application::$onError
* @Event("Arachne\EventDispatcher\Event\ApplicationErrorEvent")
*/
const ERROR = 'nette.application.error';
}
125 changes: 125 additions & 0 deletions src/DI/EventDispatcherExtension.php
@@ -0,0 +1,125 @@
<?php

/**
* This file is part of the Arachne
*
* Copyright (c) Jáchym Toušek (enumag@gmail.com)
*
* For the full copyright and license information, please view the file license.md that was distributed with this source code.
*/

namespace Arachne\EventDispatcher\DI;

use Arachne\EventDispatcher\ApplicationEvents;
use Nette\DI\CompilerExtension;
use Nette\DI\ServiceDefinition;
use Nette\Utils\AssertionException;

/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
class EventDispatcherExtension extends CompilerExtension
{
/**
* Subscribers with this tag are added to the event dispatcher.
*
* @var string
*/
const TAG_SUBSCRIBER = 'arachne.eventDispatcher.subscriber';

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('eventDispatcher'))
->setClass('Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher');
}

public function beforeCompile()
{
$builder = $this->getContainerBuilder();

// Process event subscribers.
$dispatcher = $builder->getDefinition($this->prefix('eventDispatcher'));
foreach ($builder->findByTag(self::TAG_SUBSCRIBER) as $name => $attributes) {
$class = $builder->getDefinition($name)->getClass();

if (!is_subclass_of($class, 'Symfony\Component\EventDispatcher\EventSubscriberInterface')) {
throw new AssertionException("Subscriber '$name' doesn't implement 'Symfony\Component\EventDispatcher\EventSubscriberInterface'.");
}

$this->registerSubscriber($dispatcher, $name, $class);
}

// Bind dispatcher to Nette\Application\Application events.
foreach ($builder->findByType('Nette\Application\Application') as $application) {
$application->addSetup('$dispatcher = ?', ['@' . $this->prefix('eventDispatcher')]);
$this->bindApplicationEvent($application, ApplicationEvents::STARTUP, 'Arachne\EventDispatcher\Event\ApplicationEvent', 'onStartup');
$this->bindApplicationEvent($application, ApplicationEvents::SHUTDOWN, 'Arachne\EventDispatcher\Event\ApplicationShutdownEvent', 'onShutdown', 'exception');
$this->bindApplicationEvent($application, ApplicationEvents::REQUEST, 'Arachne\EventDispatcher\Event\ApplicationRequestEvent', 'onRequest', 'request');
$this->bindApplicationEvent($application, ApplicationEvents::PRESENTER, 'Arachne\EventDispatcher\Event\ApplicationPresenterEvent', 'onPresenter', 'presenter');
$this->bindApplicationEvent($application, ApplicationEvents::RESPONSE, 'Arachne\EventDispatcher\Event\ApplicationResponseEvent', 'onResponse', 'response');
$this->bindApplicationEvent($application, ApplicationEvents::ERROR, 'Arachne\EventDispatcher\Event\ApplicationErrorEvent', 'onError', 'exception');
}

// Bind dispatcher to console.
foreach ($builder->findByType('Symfony\Component\Console\Application') as $console) {
$console->addSetup('setDispatcher');
}
}

/**
* Emulates ContainerAwareEventDispatcher::addSubscriberService() to prevent autoloading of all subscribers in runtime.
*
* @param ServiceDefinition $dispatcher
* @param string $service
* @param string $class
*/
private function registerSubscriber(ServiceDefinition $dispatcher, $service, $class)
{
foreach ($class::getSubscribedEvents() as $event => $params) {
if (is_string($params)) {
$dispatcher->addSetup('?->addListenerService(?, ?)', [
'@self',
$event,
[$service, $params], //callback
]);
} elseif (is_string($params[0])) {
$dispatcher->addSetup('?->addListenerService(?, ?, ?)', [
'@self',
$event,
[$service, $params[0]], //callback
isset($params[1]) ? $params[1] : 0, // priority
]);
} else {
foreach ($params as $listener) {
$dispatcher->addSetup('?->addListenerService(?, ?, ?)', [
'@self',
$event,
[$service, $listener[0]], //callback
isset($listener[1]) ? $listener[1] : 0, // priority
]);
}
}
}
}

/**
* Binds dispatcher to Nette\Application\Application event.
*
* @param ServiceDefinition $application
* @param string $event
* @param string $class
* @param string $property
* @param string $argument
*/
private function bindApplicationEvent(ServiceDefinition $application, $event, $class, $property, $argument = null)
{
$argument = $argument ? ', $' . $argument : '';
$application->addSetup('?->?[] = function ($application' . $argument . ') use ($dispatcher) { $dispatcher->dispatch(?, new ' . $class . '($application' . $argument . ')); }', [
'@self',
$property,
$event,
]);
}
}

0 comments on commit 6a4c8f2

Please sign in to comment.