Skip to content

Commit

Permalink
Merge pull request #2 from Bambuk/master
Browse files Browse the repository at this point in the history
Optimizing researching of events
  • Loading branch information
bwoester committed Jan 2, 2015
2 parents c996d2a + b185ea3 commit 8a62304
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions EventInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
*/
class EventInterceptor extends CComponent
{


/**
* @var array Class-level cache of event names
*/
private static $eventNamesCache = [];

/**
* Attach a closure to every event defined by $subject.
* The closure forwards the event including its name to the intercept method,
Expand All @@ -59,15 +64,15 @@ public function initialize( CComponent $subject, array $events=array('*') )
$forwarder = new EventForwarder();
$forwarder->eventName = $eventName;
$forwarder->interceptor = $this;
$subject->$eventName = array($forwarder,'forward');

$subject->attachEventHandler($eventName, array($forwarder, 'forward'));
}
}

/**
* Will be called whenever $subject raises an event.
* Everything this method does is raising an event itself, which contains:
*
*
* 1) the name of the intercepted event
* 2) the intercepted event object
*
Expand All @@ -89,19 +94,21 @@ public function onEventIntercepted( InterceptionEvent $event )

protected function getEventNames( CComponent $subject )
{
$aEventNames = array();
$aMethodNames = get_class_methods( $subject );
$className = get_class($subject);
if (!isset(self::$eventNamesCache[$className])) {
$aEventNames = array();
$aMethodNames = get_class_methods($subject);

foreach ($aMethodNames as $methodName)
{
if (strtolower(substr($methodName,0,2)) === 'on') {
$aEventNames[] = $methodName;
foreach ($aMethodNames as $methodName) {
if (strtolower(substr($methodName, 0, 2)) === 'on') {
$aEventNames[] = $methodName;
}
}
self::$eventNamesCache[$className] = $aEventNames;
}

return $aEventNames;
return self::$eventNamesCache[$className];
}

}


Expand Down

0 comments on commit 8a62304

Please sign in to comment.