Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit da5849e
Showing
9 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
[submodule "vendor/symfony/Component/ClassLoader"] | |||
path = vendor/symfony/Component/ClassLoader | |||
url = https://github.com/symfony/ClassLoader.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,19 @@ | |||
Copyright (c) 2011 Igor Wiedler | |||
|
|||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,29 @@ | |||
Événement | |||
========= | |||
|
|||
Événement is a very simple event dispatching library for PHP 5.3. | |||
|
|||
It has the same design goals as [Silex](http://silex-project.org) and | |||
[Pimple](http://pimple-project.org), to empower the user while staying concise | |||
and simple. | |||
|
|||
It is very strongly inspired by the EventEmitter API found in | |||
[node.js](http://nodejs.org). | |||
|
|||
Creating an Emitter | |||
------------------- | |||
|
|||
require __DIR__.'/vendor/evenement/src/Evenement/EventEmitter.php'; | |||
$emitter = new Evenement\EventEmitter(); | |||
|
|||
Adding Listeners | |||
---------------- | |||
|
|||
$emitter->on('user.create', function (User $user) use ($logger) { | |||
$logger->log(sprintf("User '%s' was created.", $user->getLogin())); | |||
}); | |||
|
|||
Emitting Events | |||
--------------- | |||
|
|||
$emitter->emit('user.create', array($user)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,19 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
|
|||
<phpunit backupGlobals="false" | |||
backupStaticAttributes="false" | |||
colors="true" | |||
convertErrorsToExceptions="true" | |||
convertNoticesToExceptions="true" | |||
convertWarningsToExceptions="true" | |||
processIsolation="false" | |||
stopOnFailure="false" | |||
syntaxCheck="false" | |||
bootstrap="tests/bootstrap.php" | |||
> | |||
<testsuites> | |||
<testsuite name="Evenement Test Suite"> | |||
<directory>./tests/Evenement/</directory> | |||
</testsuite> | |||
</testsuites> | |||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,88 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of Evenement. | |||
* | |||
* Copyright (c) 2011 Igor Wiedler | |||
* | |||
* 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. | |||
*/ | |||
|
|||
namespace Evenement; | |||
|
|||
class EventEmitter | |||
{ | |||
private $listeners = array(); | |||
private $modified = array(); | |||
|
|||
public function on($event, $listener) | |||
{ | |||
if (!is_callable($listener)) { | |||
throw new \InvalidArgumentException('The provided listener was not a valid callable.'); | |||
} | |||
|
|||
if (!isset($this->listeners[$event])) { | |||
$this->listeners[$event] = array(); | |||
} | |||
|
|||
$this->listeners[$event][] = $listener; | |||
|
|||
$this->modified[$event] = true; | |||
} | |||
|
|||
public function removeListener($event, $listener) | |||
{ | |||
if (isset($this->listeners[$event])) { | |||
if (false !== $index = array_search($listener, $this->listeners[$event], true)) { | |||
unset($this->listeners[$event][$index]); | |||
} | |||
} | |||
} | |||
|
|||
public function removeAllListeners($event) | |||
{ | |||
unset($this->listeners[$event]); | |||
} | |||
|
|||
public function listeners($event) | |||
{ | |||
if (!isset($this->listeners[$event])) { | |||
return array(); | |||
} | |||
|
|||
$this->sortListenersEventIfModified($event); | |||
|
|||
return $this->listeners[$event]; | |||
} | |||
|
|||
public function emit($event, array $arguments = array()) | |||
{ | |||
foreach ($this->listeners($event) as $listener) { | |||
call_user_func_array($listener, $arguments); | |||
} | |||
} | |||
|
|||
private function sortListenersEventIfModified($event) | |||
{ | |||
if ($this->modified[$event]) { | |||
krsort($this->listeners[$event]); | |||
unset($this->modified[$event]); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,144 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of Evenement. | |||
* | |||
* Copyright (c) 2011 Igor Wiedler | |||
* | |||
* 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. | |||
*/ | |||
|
|||
namespace Evenement\Tests; | |||
|
|||
use Evenement\EventEmitter; | |||
|
|||
class EventEmitterTest extends \PHPUnit_Framework_TestCase | |||
{ | |||
private $emitter; | |||
|
|||
public function setUp() | |||
{ | |||
$this->emitter = new EventEmitter(); | |||
} | |||
|
|||
public function testAddListenerWithLambda() | |||
{ | |||
|
|||
$this->emitter->on('foo', function () {}); | |||
} | |||
|
|||
public function testAddListenerWithMethod() | |||
{ | |||
$listener = new Listener(); | |||
$this->emitter->on('foo', array($listener, 'onFoo')); | |||
} | |||
|
|||
public function testAddListenerWithStaticMethod() | |||
{ | |||
$this->emitter->on('bar', array('Evenement\Tests\Listener', 'onBar')); | |||
} | |||
|
|||
/** | |||
* @expectedException InvalidArgumentException | |||
*/ | |||
public function testAddListenerWithInvalidListener() | |||
{ | |||
$this->emitter->on('foo', 'not a callable'); | |||
} | |||
|
|||
public function testEmitWithoutArguments() | |||
{ | |||
$listenerCalled = false; | |||
|
|||
$this->emitter->on('foo', function () use (&$listenerCalled) { | |||
$listenerCalled = true; | |||
}); | |||
|
|||
$this->assertSame(false, $listenerCalled); | |||
|
|||
$this->emitter->emit('foo'); | |||
|
|||
$this->assertSame(true, $listenerCalled); | |||
} | |||
|
|||
public function testEmitWithOneArgument() | |||
{ | |||
$test = $this; | |||
|
|||
$listenerCalled = false; | |||
|
|||
$this->emitter->on('foo', function ($value) use (&$listenerCalled, $test) { | |||
$listenerCalled = true; | |||
|
|||
$test->assertSame('bar', $value); | |||
}); | |||
|
|||
$this->assertSame(false, $listenerCalled); | |||
|
|||
$this->emitter->emit('foo', array('bar')); | |||
|
|||
$this->assertSame(true, $listenerCalled); | |||
} | |||
|
|||
public function testEmitWithTwoArguments() | |||
{ | |||
$test = $this; | |||
|
|||
$listenerCalled = false; | |||
|
|||
$this->emitter->on('foo', function ($arg1, $arg2) use (&$listenerCalled, $test) { | |||
$listenerCalled = true; | |||
|
|||
$test->assertSame('bar', $arg1); | |||
$test->assertSame('baz', $arg2); | |||
}); | |||
|
|||
$this->assertSame(false, $listenerCalled); | |||
|
|||
$this->emitter->emit('foo', array('bar', 'baz')); | |||
|
|||
$this->assertSame(true, $listenerCalled); | |||
} | |||
|
|||
public function testEmitWithNoListeners() | |||
{ | |||
$this->emitter->emit('foo'); | |||
$this->emitter->emit('foo', array('bar')); | |||
$this->emitter->emit('foo', array('bar', 'baz')); | |||
} | |||
|
|||
public function testEmitWithTwoListeners() | |||
{ | |||
$listenersCalled = 0; | |||
|
|||
$this->emitter->on('foo', function () use (&$listenersCalled) { | |||
$listenersCalled++; | |||
}); | |||
|
|||
$this->emitter->on('foo', function () use (&$listenersCalled) { | |||
$listenersCalled++; | |||
}); | |||
|
|||
$this->assertSame(0, $listenersCalled); | |||
|
|||
$this->emitter->emit('foo'); | |||
|
|||
$this->assertSame(2, $listenersCalled); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of Evenement. | |||
* | |||
* Copyright (c) 2011 Igor Wiedler | |||
* | |||
* 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. | |||
*/ | |||
|
|||
namespace Evenement\Tests; | |||
|
|||
class Listener | |||
{ | |||
public function onFoo() | |||
{ | |||
} | |||
|
|||
static public function onBar() | |||
{ | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of the Silex framework. | |||
* | |||
* (c) Fabien Potencier <fabien@symfony.com> | |||
* | |||
* For the full copyright and license information, please view the LICENSE | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
require_once __DIR__.'/../vendor/symfony/Component/ClassLoader/UniversalClassLoader.php'; | |||
|
|||
$loader = new Symfony\Component\ClassLoader\UniversalClassLoader(); | |||
$loader->registerNamespace('Evenement', __DIR__.'/../src'); | |||
$loader->registerNamespace('Evenement\Tests', __DIR__); | |||
$loader->register(); |
Submodule ClassLoader
added at
33066c