Skip to content

Commit

Permalink
Use SplHeap for handler sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Szurovecz committed Jan 11, 2015
1 parent 095adaf commit 9983c3c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 115 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.4",
"precore/precore": "~1.8",
"precore/precore": "~1.9@dev",
"doctrine/annotations": "~1.1",
"doctrine/cache": "~1.0",
"lf4php/lf4php": "~4.2",
Expand Down
85 changes: 0 additions & 85 deletions src/predaddy/messagehandling/FunctionDescriptors.php

This file was deleted.

17 changes: 5 additions & 12 deletions src/predaddy/messagehandling/InterceptableMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Iterator;
use precore\lang\Object;
use predaddy\messagehandling\util\MessageCallbackClosures;
use SplFixedArray;

/**
* @author Janos Szurovecz <szjani@szjani.hu>
Expand All @@ -41,7 +42,7 @@ abstract class InterceptableMessageBus extends Object implements MessageBus
private static $emptyCallback;

/**
* @var DispatchInterceptor[]
* @var SplFixedArray
*/
private $interceptors;

Expand All @@ -55,7 +56,7 @@ public static function init()

public function __construct(array $interceptors = [])
{
$this->interceptors = $interceptors;
$this->interceptors = SplFixedArray::fromArray($interceptors);
}

/**
Expand Down Expand Up @@ -103,24 +104,16 @@ final protected static function emptyCallback()
return self::$emptyCallback;
}

/**
* @return Iterator of Interceptor
*/
protected function createInterceptorIterator()
{
return new ArrayIterator($this->interceptors);
}

/**
* @param $message
* @param Closure $dispatchClosure
* @return InterceptorChain
*/
protected function createChain($message, Closure $dispatchClosure)
private function createChain($message, Closure $dispatchClosure)
{
return new DefaultInterceptorChain(
$message,
$this->createInterceptorIterator(),
$this->interceptors,
$dispatchClosure
);
}
Expand Down
44 changes: 27 additions & 17 deletions src/predaddy/messagehandling/SimpleMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Closure;
use Exception;
use precore\lang\ObjectClass;
use precore\util\Collections;
use precore\util\Objects;
use SplObjectStorage;

Expand Down Expand Up @@ -67,7 +68,7 @@ class SimpleMessageBus extends InterceptableMessageBus implements HandlerFactory
private $exceptionHandler;

/**
* @var FunctionDescriptors
* @var SplObjectStorage
*/
private $functionDescriptors;

Expand All @@ -84,7 +85,7 @@ public function __construct(SimpleMessageBusBuilder $builder = null)
$this->exceptionHandler = $builder->getExceptionHandler();
$this->handlerDescriptorFactory = $builder->getHandlerDescriptorFactory();
$this->closureDescriptorFactory = $builder->getHandlerDescriptorFactory()->getFunctionDescriptorFactory();
$this->functionDescriptors = new FunctionDescriptors();
$this->functionDescriptors = new SplObjectStorage();
$this->factories = new SplObjectStorage();
}

Expand Down Expand Up @@ -113,8 +114,8 @@ public function unregisterHandlerFactory(Closure $factory)
public function register($handler)
{
$descriptor = $this->handlerDescriptorFactory->create($handler);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptors) {
$this->functionDescriptors->add($functionDescriptors);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptor) {
$this->functionDescriptors->attach($functionDescriptor);
}
}

Expand All @@ -124,8 +125,8 @@ public function register($handler)
public function unregister($handler)
{
$descriptor = $this->handlerDescriptorFactory->create($handler);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptors) {
$this->functionDescriptors->remove($functionDescriptors);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptor) {
$this->functionDescriptors->detach($functionDescriptor);
}
}

Expand All @@ -136,7 +137,7 @@ public function unregister($handler)
public function registerClosure(Closure $closure, $priority = self::DEFAULT_PRIORITY)
{
$descriptor = $this->closureDescriptorFactory->create(new ClosureWrapper($closure), $priority);
$this->functionDescriptors->add($descriptor);
$this->functionDescriptors->attach($descriptor);
}

/**
Expand All @@ -146,7 +147,13 @@ public function registerClosure(Closure $closure, $priority = self::DEFAULT_PRIO
public function unregisterClosure(Closure $closure, $priority = self::DEFAULT_PRIORITY)
{
$descriptor = $this->closureDescriptorFactory->create(new ClosureWrapper($closure), $priority);
$this->functionDescriptors->remove($descriptor);
$this->functionDescriptors->detach($descriptor);
foreach ($this->functionDescriptors as $key => $value) {
if ($value->equals($descriptor)) {
$this->functionDescriptors->offsetUnset($value);
break;
}
}
}

/**
Expand Down Expand Up @@ -204,13 +211,19 @@ protected function dispatch($message, MessageCallback $callback)

/**
* @param $message
* @return \Traversable|\Countable
* @return ArrayObject
*/
protected function callableWrappersFor($message)
{
$objectClass = ObjectClass::forName(get_class($message));
/* @var $functionDescriptors FunctionDescriptors */
$functionDescriptors = clone $this->functionDescriptors;
$heap = Collections::createHeap(Collections::reverseOrder());

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

foreach ($this->factories as $factory) {
/* @var $factoryDescriptor FunctionDescriptor */
Expand All @@ -219,17 +232,14 @@ protected function callableWrappersFor($message)
$handler = call_user_func($factory, $message);
$descriptor = $this->handlerDescriptorFactory->create($handler);
foreach ($descriptor->getFunctionDescriptors() as $functionDescriptor) {
$functionDescriptors->add($functionDescriptor);
$heap->insert($functionDescriptor);
}
}
}

$res = new ArrayObject();
/* @var $functionDescriptor FunctionDescriptor */
foreach ($functionDescriptors as $functionDescriptor) {
if ($functionDescriptor->isHandlerFor($objectClass)) {
$res->append($functionDescriptor->getCallableWrapper());
}
foreach ($heap as $functionDescriptor) {
$res->append($functionDescriptor->getCallableWrapper());
}
return $res;
}
Expand Down

0 comments on commit 9983c3c

Please sign in to comment.