Skip to content

Commit

Permalink
Ignore incompatible handlers created by factories
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Szurovecz committed Jan 11, 2015
1 parent 9983c3c commit 4ad6e99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/predaddy/messagehandling/SimpleMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use precore\lang\ObjectClass;
use precore\util\Collections;
use precore\util\Objects;
use SplHeap;
use SplObjectStorage;

/**
Expand Down Expand Up @@ -218,32 +219,36 @@ protected function callableWrappersFor($message)
$objectClass = ObjectClass::forName(get_class($message));
$heap = Collections::createHeap(Collections::reverseOrder());

/* @var $functionDescriptor FunctionDescriptor */
foreach ($this->functionDescriptors as $functionDescriptor) {
if ($functionDescriptor->isHandlerFor($objectClass)) {
$heap->insert($functionDescriptor);
}
}
self::insertAllHandlers($this->functionDescriptors, $heap, $objectClass);

foreach ($this->factories as $factory) {
/* @var $factoryDescriptor FunctionDescriptor */
$factoryDescriptor = $this->factories[$factory];
if ($factoryDescriptor->isHandlerFor($objectClass)) {
$handler = call_user_func($factory, $message);
$descriptor = $this->handlerDescriptorFactory->create($handler);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptor) {
$heap->insert($functionDescriptor);
}
self::insertAllHandlers($descriptor->getFunctionDescriptors(), $heap, $objectClass);
}
}

$res = new ArrayObject();
foreach ($heap as $functionDescriptor) {
$res->append($functionDescriptor->getCallableWrapper());
}

return $res;
}

private static function insertAllHandlers($functionDescriptors, SplHeap $heap, ObjectClass $objectClass)
{
/* @var $functionDescriptor FunctionDescriptor */
foreach ($functionDescriptors as $functionDescriptor) {
if ($functionDescriptor->isHandlerFor($objectClass)) {
$heap->insert($functionDescriptor);
}
}
}

public function toString()
{
return Objects::toStringHelper($this)
Expand Down
8 changes: 8 additions & 0 deletions tests/src/predaddy/messagehandling/SimpleMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public function handle(SimpleMessage $message)
{
$this->lastMessage = $message;
}

/**
* @Subscribe
* @param SimpleMessageHandler $handler
*/
public function handleSelf(SimpleMessageHandler $handler)
{
}
}

0 comments on commit 4ad6e99

Please sign in to comment.