Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced Hooks with EventEmitter trait.
- Loading branch information
Showing
16 changed files
with
116 additions
and
100 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 | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,3 @@ phpunit.xml | |
composer.lock | ||
vendor | ||
node_modules | ||
package-lock.json |
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
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* | ||
* Event Emitter trait. | ||
* | ||
*/ | ||
namespace CssCrush; | ||
|
||
trait EventEmitter { | ||
|
||
private $eventEmitterStorage = []; | ||
private $eventEmitterUid = 0; | ||
|
||
public function on($event, callable $function) | ||
{ | ||
if (! isset($this->eventEmitterStorage[$event])) { | ||
$this->eventEmitterStorage[$event] = []; | ||
} | ||
|
||
$id = ++$this->eventEmitterUid; | ||
$this->eventEmitterStorage[$event][$id] = $function; | ||
|
||
return function () use ($event, $id) { | ||
unset($this->eventEmitterStorage[$event][$id]); | ||
}; | ||
} | ||
|
||
public function emit($event, $data = null) | ||
{ | ||
if (isset($this->eventEmitterStorage[$event])) { | ||
foreach ($this->eventEmitterStorage[$event] as $function) { | ||
$function($data); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace CssCrush\UnitTest; | ||
|
||
use CssCrush\EventEmitter; | ||
|
||
class EventEmitterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAll() | ||
{ | ||
$emitter = new EventEmitterHost(); | ||
|
||
$foo = null; | ||
$cancelEvent = $emitter->on('foo', function ($data) use (&$foo) { | ||
$foo = $data; | ||
}); | ||
|
||
$this->assertEquals($foo, null); | ||
|
||
$emitter->emit('foo', 10); | ||
|
||
$this->assertEquals($foo, 10); | ||
|
||
$cancelEvent(); | ||
|
||
$emitter->emit('foo', 20); | ||
|
||
$this->assertEquals($foo, 10); | ||
} | ||
} | ||
|
||
class EventEmitterHost { use EventEmitter; } |
This file was deleted.
Oops, something went wrong.