Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
oanhnn committed Jan 22, 2016
1 parent d1edeaf commit 879bad2
Show file tree
Hide file tree
Showing 14 changed files with 398 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.php_cs export-ignore
/phpunit.xml export-ignore
/CHANGELOG.md export-ignore
22 changes: 21 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# User specific & automatically generated files #
#################################################
/composer.lock
/vendor
/vendor
/build/logs

# IDE and editor specific files #
#################################
/nbproject
.idea

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
*.mo
31 changes: 31 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

$header = <<<EOF
This file is part of `lemon/event` project.
(c) 2015-2016 LemonPHP Team
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);

return Symfony\CS\Config\Config::create()
// use default PSR-2 and extra fixers:
->level('psr2')
->fixers(array(
'header_comment',
'short_array_syntax',
'ordered_use',
'php_unit_construct',
'php_unit_strict',
'strict',
'strict_param',
))
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->exclude('tmp')
->in(__DIR__)
)
;
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

sudo: false

before_install:
- composer self-update

install:
- composer install
- composer require satooshi/php-coveralls '~1.0'

script:
- php vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
lemonphp/event
Package lemonphp/event
===
[![Build Status](https://travis-ci.org/lemonphp/event.svg?branch=master)](https://travis-ci.org/lemonphp/event)
[![Coverage Status](https://coveralls.io/repos/github/lemonphp/event/badge.svg?branch=master)](https://coveralls.io/github/lemonphp/event?branch=master)

A simple event dispatcher

Usage
---

```
use Lemonphp\Event\Event;
use Lemonphp\Event\Dispatcher;
use Lemon\Event\Event;
use Lemon\Event\Dispatcher;
$dispatcher = new Dispatcher();
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lemonphp/event",
"version": "1.0.0",
"type": "library",
"description": "Simple event dispatcher",
"keywords": ["event", "event-dispatcher"],
"license": "MIT",
Expand All @@ -24,6 +24,6 @@
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
"phpunit/phpunit": "~4.8"
}
}
9 changes: 6 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
<!-- Add any additional test suites you want to run here -->
<testsuites>
<testsuite name="Test Source">
<directory>./tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<!-- Ignore vendor tests in code coverage reports -->
<filter>
<blacklist>
<directory suffix=".php">./vendor/</directory>
<directory>bin/</directory>
<directory suffix=".php">vendor</directory>
<directory>bin</directory>
</blacklist>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
29 changes: 21 additions & 8 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of `lemon/event` project.
*
* (c) 2015-2016 LemonPHP Team
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Lemon\Event;

class Dispatcher implements DispatcherInterface
Expand All @@ -17,14 +26,14 @@ class Dispatcher implements DispatcherInterface
/**
* {@inheritdoc}
*/
public function trigger($eventName, Event $event = null)
public function trigger($event)
{
if (null === $event) {
$event = new Event($eventName);
if (!($event instanceof Event)) {
$event = new Event($event);
}

if ($listeners = $this->getListeners($eventName)) {
$this->doDispatch($listeners, $eventName, $event);
if ($listeners = $this->getListeners($event->getEventName())) {
$this->doDispatch($listeners, $event);
}

return $event;
Expand All @@ -48,6 +57,11 @@ public function off($eventName, $listener = null)
return;
}

if (is_null($listener)) {
unset($this->listeners[$eventName], $this->sorted[$eventName]);
return;
}

foreach ($this->listeners[$eventName] as $priority => $listeners) {
if (false !== ($key = array_search($listener, $listeners, true))) {
unset($this->listeners[$eventName][$priority][$key], $this->sorted[$eventName]);
Expand Down Expand Up @@ -119,13 +133,12 @@ public function getListenerPriority($eventName, $listener)
* for each listener.
*
* @param callable[] $listeners The event listeners.
* @param string $eventName The name of the event to dispatch.
* @param Event $event The event object to pass to the event handlers/listeners.
*/
protected function doDispatch($listeners, $eventName, Event $event)
protected function doDispatch($listeners, Event $event)
{
foreach ($listeners as $listener) {
call_user_func($listener, $event, $eventName, $this);
call_user_func($listener, $event);
if ($event->isPropagationStopped()) {
break;
}
Expand Down
19 changes: 12 additions & 7 deletions src/DispatcherInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of `lemon/event` project.
*
* (c) 2015-2016 LemonPHP Team
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Lemon\Event;

interface DispatcherInterface
Expand All @@ -8,20 +17,16 @@ interface DispatcherInterface
/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch. The name of
* the event is the name of the method that is
* invoked on listeners.
* @param Event $event The event to pass to the event handlers/listeners.
* If not supplied, an empty Event instance is created.
* @param Event|string $event The event or event name to pass to the event handlers/listeners.
* @return Event
*/
public function trigger($eventName, Event $event = null);
public function trigger($event);

/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName The event to listen on
* @param callable $listener The listener
* @param callable $listener The listener. It passed Event object is first argument
* @param int $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
*/
Expand Down
17 changes: 13 additions & 4 deletions src/Event.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

/*
* This file is part of `lemon/event` project.
*
* (c) 2015-2016 LemonPHP Team
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Lemon\Event;

class Event
{
/**
* @var string Read only property
*/
protected $name;
protected $eventName;

/**
* @var bool Whether no further event listeners should be triggered
Expand All @@ -19,7 +28,7 @@ class Event
*/
public function __construct($name)
{
$this->name = (string) $name;
$this->eventName = (string) $name;
}

/**
Expand Down Expand Up @@ -48,8 +57,8 @@ public function stopPropagation()
/**
* @return string
*/
public function getName()
public function getEventName()
{
return $this->name;
return $this->eventName;
}
}
Loading

0 comments on commit 879bad2

Please sign in to comment.