Skip to content
Browse files

hello Evenement

  • Loading branch information...
0 parents commit da5849e65d9c5f7f7b5245c7eabde4c8e1a70138 @igorw committed Aug 16, 2011
3 .gitmodules
@@ -0,0 +1,3 @@
+[submodule "vendor/symfony/Component/ClassLoader"]
+ path = vendor/symfony/Component/ClassLoader
+ url = https://github.com/symfony/ClassLoader.git
19 LICENSE
@@ -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.
29 README.rst
@@ -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));
19 phpunit.xml.dist
@@ -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>
88 src/Evenement/EventEmitter.php
@@ -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]);
+ }
+ }
+}
144 tests/Evenement/Tests/EventEmitterTest.php
@@ -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);
+ }
+}
38 tests/Evenement/Tests/Listener.php
@@ -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()
+ {
+ }
+}
17 tests/bootstrap.php
@@ -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();
1 vendor/symfony/Component/ClassLoader
@@ -0,0 +1 @@
+Subproject commit 33066c11cc23498409eb2a699474761b14deca37

0 comments on commit da5849e

Please sign in to comment.
Something went wrong with that request. Please try again.