Skip to content

Commit

Permalink
Add listeners to events
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Aug 31, 2015
1 parent cf9d01a commit 5c3aaf4
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/DataCollector/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,54 @@

class EventCollector extends TimeDataCollector
{
protected $events = null;
/** @var Dispatcher */
protected $events;

/** @var ValueExporter */
protected $exporter;

public function __construct($requestStartTime = null){
public function __construct($requestStartTime = null)
{
parent::__construct($requestStartTime);

$this->exporter = new ValueExporter();
}

public function onWildcardEvent()
{
$args = func_get_args();
$name = $this->getCurrentEvent($args);
$name = $this->events->firing();
$time = microtime(true);
$this->addMeasure($name, $time, $time, $this->prepareParams($args) );

$params = $this->prepareParams(func_get_args());

foreach($this->events->getListeners($name) as $i => $listener) {
if (is_array($listener) && count($listener) > 1 && is_object($listener[0])) {
list($class, $method) = $listener;

// Skip this class itself
if ($class instanceof static) {
continue;
}

// Format thet listener to readable format
$listener = get_class($class) . '@' . $method;

} elseif ($listener instanceof \Closure) {
$reflector = new \ReflectionFunction($listener);

if($reflector->getNamespaceName() == 'Barryvdh\Debugbar') {
continue;
}

$filename = ltrim(str_replace(base_path(), '', $reflector->getFileName()), '/');
$listener = $reflector->getName() . ' ('.$filename . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine() . ')';
} else {
$listener = $this->formatVar($listener);
}

$params['listeners.'.$i] = $listener;
}
$this->addMeasure($name, $time, $time, $params);
}

public function subscribe(Dispatcher $events)
Expand All @@ -29,16 +63,6 @@ public function subscribe(Dispatcher $events)
$events->listen('*', array($this, 'onWildcardEvent'));
}

protected function getCurrentEvent($args)
{
if(method_exists($this->events, 'firing')){
$event = $this->events->firing();
}else{
$event = end($args);
}
return $event;
}

protected function prepareParams($params)
{
$data = array();
Expand Down

0 comments on commit 5c3aaf4

Please sign in to comment.