diff --git a/src/Events/Dispatcher.php b/src/Events/Dispatcher.php index 788606829..9cca58a08 100644 --- a/src/Events/Dispatcher.php +++ b/src/Events/Dispatcher.php @@ -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]; + } } diff --git a/tests/Events/DispatcherTest.php b/tests/Events/DispatcherTest.php index 2cf08f419..8cbb8648d 100644 --- a/tests/Events/DispatcherTest.php +++ b/tests/Events/DispatcherTest.php @@ -1,4 +1,4 @@ -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; + } }