Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,31 @@ protected function callQueueMethodOnHandler($class, $method, $arguments)
'class' => $class, 'method' => $method, 'data' => serialize($arguments),
]);
}

/**
* Create the class based event callable.
*
* @param array|string $listener
* @return callable
*/
protected function createClassCallable($listener)
{
[$class, $method] = is_array($listener)
? $listener
: $this->parseClassCallable($listener);

$listener = $this->container->make($class);

if (! method_exists($listener, $method)) {
$method = '__invoke';
}

if ($this->handlerShouldBeQueued($class)) {
return $this->createQueuedHandlerCallable($class, $method);
}

return $this->handlerShouldBeDispatchedAfterDatabaseTransactions($listener)
? $this->createCallbackForListenerRunningAfterCommits($listener, $method)
: [$listener, $method];
}
}
53 changes: 52 additions & 1 deletion tests/Events/DispatcherTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Events;

use Winter\Storm\Foundation\Application;
use Winter\Storm\Events\Dispatcher;
Expand All @@ -10,6 +10,9 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Bus;

use EventTest;
use TestCase;

class DispatcherTest extends TestCase
{

Expand Down Expand Up @@ -157,4 +160,52 @@ public function testQueuedClosureListen()
$dispatcher->dispatch(new EventTest());
$this->assertTrue($magic_value);
}

/**
* Test [$classInstance, 'method'] event listener format
*/
public function testInstanceMethodListen()
{
$dispatcher = new Dispatcher();
$classInstance = new TestClass;

$dispatcher->listen('test.test', [$classInstance, 'instanceMethodHandler']);
$dispatcher->fire('test.test');

$this->assertTrue($classInstance->getMagicValue());
}

/**
* Test 'ClassName@method' event listener format
*/
public function testClassMethodListen()
{
$magic_value = false;
$this->app->bind('TestClass', TestClass::class);

Event::listen('test.test', 'TestClass@classMethodHandler');
Event::fire('test.test', [&$magic_value]);

$this->assertTrue($magic_value);
}
}

class TestClass
{
protected $magic_value = false;

public function instanceMethodHandler()
{
$this->magic_value = true;
}

public function classMethodHandler(&$value)
{
$value = true;
}

public function getMagicValue()
{
return $this->magic_value;
}
}