Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
runHooks() with echos
public function runHooks(Wire $object, $method, $arguments, $type = 'method') {
$hookTimer = self::___debug ? $this->hookTimer($object, $method, $arguments) : null;
$realMethod = "___$method";
$cancelHooks = false;
$profiler = $this->wire->wire('profiler');
$hooks = null;
if(is_array($type)) {
// array of hooks to run provided in $type argument
$hooks = $type;
$type = 'custom';
}
$result = array(
'return' => null,
'numHooksRun' => 0,
'methodExists' => ($type === 'method' ? method_exists($object, $realMethod) : false),
'replace' => false,
);
if($type === 'method' || $type === 'property') {
if(!$result['methodExists'] && !$this->isHookedOrParents($object, $method, $type)) {
return $result; // exit quickly when we can
}
}
echo '<br>';
echo '<br>';
echo $object . ' - ' . $method . ' - ';
if($hooks === null) $hooks = $this->getHooks($object, $method);
print_r($hooks);
echo '<br>';
echo 'type: ' . $type;
echo '<br>';
foreach(array('before', 'after') as $when) {
echo $when;
if($type === 'method') {
if($when === 'after' && $result['replace'] !== true) {
if($result['methodExists']) {
$result['return'] = $object->_callMethod($realMethod, $arguments);
} else {
$result['return'] = null;
}
}
} else if($type === 'after') {
if($when === 'before') continue;
} else if($type === 'before') {
if($when === 'after') break;
}
foreach($hooks as $priority => $hook) {
echo ' - <strong>prior:</strong>' . $priority;
if(!$hook['options'][$when]) continue;
if(!empty($hook['options']['objMatch'])) {
/** @var Selectors $objMatch */
$objMatch = $hook['options']['objMatch'];
// object match comparison to determine at runtime whether to execute the hook
if(is_object($objMatch)) {
if(!$objMatch->matches($object)) continue;
} else {
if(((string) $object) != $objMatch) continue;
}
}
if($type == 'method' && !empty($hook['options']['argMatch'])) {
// argument comparison to determine at runtime whether to execute the hook
$argMatches = $hook['options']['argMatch'];
$matches = true;
foreach($argMatches as $argKey => $argMatch) {
/** @var Selectors $argMatch */
$argVal = isset($arguments[$argKey]) ? $arguments[$argKey] : null;
if(is_object($argMatch)) {
// Selectors object
if(is_object($argVal)) {
$matches = $argMatch->matches($argVal);
} else {
// we don't work with non-object here
$matches = false;
}
} else {
if(is_array($argVal)) {
// match any array element
$matches = in_array($argMatch, $argVal);
} else {
// exact string match
$matches = $argMatch == $argVal;
}
}
if(!$matches) break;
}
if(!$matches) continue; // don't run hook
}
echo ' - create event - ';
$event = new HookEvent(array(
'object' => $object,
'method' => $method,
'arguments' => $arguments,
'when' => $when,
'return' => $result['return'],
'id' => $hook['id'],
'options' => $hook['options']
));
$this->wire->wire($event);
$toObject = $hook['toObject'];
$toMethod = $hook['toMethod'];
echo 'object: ' . $toObject . ' - method: ' . $toMethod . ' - ';
if($profiler) {
$profilerEvent = $profiler->start($hook['id'], $this, array(
'event' => $event,
'hook' => $hook,
));
} else {
$profilerEvent = false;
}
if(is_null($toObject)) {
$toMethodCallable = is_callable($toMethod);
if(!$toMethodCallable && strpos($toMethod, "\\") === false && __NAMESPACE__) {
$_toMethod = $toMethod;
$toMethod = "\\" . __NAMESPACE__ . "\\$toMethod";
$toMethodCallable = is_callable($toMethod);
if(!$toMethodCallable) {
$toMethod = "\\$_toMethod";
$toMethodCallable = is_callable($toMethod);
}
}
if($toMethodCallable) {
$returnValue = $toMethod($event);
} else {
// hook fail, not callable
$returnValue = null;
}
} else {
/** @var Wire $toObject */
if($hook['toPublic']) {
// public
$returnValue = $toObject->$toMethod($event);
} else {
// protected or private
$returnValue = $toObject->_callMethod($toMethod, array($event));
}
$toMethodCallable = true;
}
if($returnValue !== null) {
// hook method/func had an explicit return statement with a value
// allow for use of $returnValue as alternative to $event->return?
}
if($profilerEvent) $profiler->stop($profilerEvent);
if(!$toMethodCallable) continue;
$result['numHooksRun']++;
if($event->cancelHooks === true) $cancelHooks = true;
if($when == 'before') {
$arguments = $event->arguments;
$result['replace'] = $event->replace === true || $result['replace'] === true;
if($result['replace']) $result['return'] = $event->return;
}
if($when == 'after') $result['return'] = $event->return;
if($cancelHooks) break;
}
if($cancelHooks) break;
echo '<strong>THE END</strong>';
}
if($hookTimer) Debug::saveTimer($hookTimer);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment