1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Minor hooks optimization in Wire class

This commit is contained in:
Ryan Cramer
2021-01-27 14:54:08 -05:00
parent 201dc4155e
commit 2557929f08

View File

@@ -352,6 +352,22 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/
protected $localHooks = array();
/**
* @var WireHooks|null
*
*/
private $_wireHooks = null;
/**
* @return WireHooks|null
* @since 3.0.171
*
*/
protected function _wireHooks() {
if($this->_wireHooks === null) $this->_wireHooks = $this->wire()->hooks;
return $this->_wireHooks;
}
/**
* Return all local hooks for this instance
*
@@ -423,7 +439,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
return $this->_callMethod($method, $arguments);
}
/** @var WireHooks $hooks */
$hooks = $this->wire('hooks');
$hooks = $this->_wireHooks();
if($hooks && $hooks->isMethodHooked($this, $method)) {
$result = $hooks->runHooks($this, $method, $arguments);
return $result['return'];
@@ -460,7 +476,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
if($val !== null) return $val;
}
}
$hooks = $this->wire('hooks'); /** @var WireHooks $hooks */
$hooks = $this->_wireHooks();
if($hooks) {
$result = $hooks->runHooks($this, $method, $arguments);
if(!$result['methodExists'] && !$result['numHooksRun']) {
@@ -539,7 +555,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/
protected function ___callUnknown($method, $arguments) {
if($arguments) {} // intentional to avoid unused argument notice
$config = $this->wire('config');
$config = $this->wire()->config;
if($config && $config->disableUnknownMethodException) return null;
throw new WireException("Method " . $this->className() . "::$method does not exist or is not callable in this context");
}
@@ -563,7 +579,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
*/
public function runHooks($method, $arguments, $type = 'method') {
return $this->wire('hooks')->runHooks($this, $method, $arguments, $type);
return $this->_wireHooks()->runHooks($this, $method, $arguments, $type);
}
/**
@@ -580,7 +596,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
*/
public function getHooks($method = '', $type = 0) {
return $this->wire('hooks')->getHooks($this, $method, $type);
return $this->_wireHooks()->getHooks($this, $method, $type);
}
/**
@@ -610,7 +626,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
static public function isHooked($method, Wire $instance = null) {
/** @var ProcessWire $wire */
$wire = $instance ? $instance->wire() : ProcessWire::getCurrentInstance();
if($instance) return $instance->wire('hooks')->hasHook($instance, $method);
if($instance) return $instance->wire()->hooks->hasHook($instance, $method);
return $wire->hooks->isHooked($method);
}
@@ -630,17 +646,15 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
* #pw-group-hooks
*
* @param string $method Method() or property name:
* @param string $name Method() name or property name:
* - If checking for a hooked method, it should be in the form `method()`.
* - If checking for a hooked property, it should be in the form `property`.
* @return bool True if this class instance has the hook, false if not.
* @throws WireException When you try to call it with a Class::something() type method, which is not supported.
*
*/
public function hasHook($method) {
// Accomplishes the same thing as the static isHooked() method, but this is non-static, more accruate,
// potentially slower than isHooked(). Less for optimization use, more for accuracy use.
return $this->wire('hooks')->hasHook($this, $method);
public function hasHook($name) {
return $this->_wireHooks()->hasHook($this, $name);
}
/**
@@ -684,7 +698,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
*/
public function addHook($method, $toObject, $toMethod = null, $options = array()) {
return $this->wire('hooks')->addHook($this, $method, $toObject, $toMethod, $options);
return $this->_wireHooks()->addHook($this, $method, $toObject, $toMethod, $options);
}
/**
@@ -736,7 +750,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
// This is the same as calling addHook with the 'before' option set the $options array.
$options['before'] = true;
if(!isset($options['after'])) $options['after'] = false;
return $this->wire('hooks')->addHook($this, $method, $toObject, $toMethod, $options);
return $this->_wireHooks()->addHook($this, $method, $toObject, $toMethod, $options);
}
/**
@@ -785,7 +799,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
public function addHookAfter($method, $toObject, $toMethod = null, $options = array()) {
$options['after'] = true;
if(!isset($options['before'])) $options['before'] = false;
return $this->wire('hooks')->addHook($this, $method, $toObject, $toMethod, $options);
return $this->_wireHooks()->addHook($this, $method, $toObject, $toMethod, $options);
}
/**
@@ -832,7 +846,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
// This is the same as calling addHook with the 'type' option set to 'property' in the $options array.
// Note that descending classes that override __get must call getHook($property) and/or runHook($property).
$options['type'] = 'property';
return $this->wire('hooks')->addHook($this, $property, $toObject, $toMethod, $options);
return $this->_wireHooks()->addHook($this, $property, $toObject, $toMethod, $options);
}
/**
@@ -892,7 +906,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
*/
public function addHookMethod($method, $toObject, $toMethod = null, $options = array()) {
return $this->wire('hooks')->addHook($this, $method, $toObject, $toMethod, $options);
return $this->_wireHooks()->addHook($this, $method, $toObject, $toMethod, $options);
}
/**
@@ -925,7 +939,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*
*/
public function removeHook($hookId) {
return $this->wire('hooks')->removeHook($this, $hookId);
return $this->_wireHooks()->removeHook($this, $hookId);
}
@@ -1043,7 +1057,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
if(is_null($old) || is_null($new) || $lastValue !== $new) {
/** @var WireHooks $hooks */
$hooks = $this->wire('hooks');
$hooks = $this->_wireHooks();
if(($hooks && $hooks->isHooked('changed()')) || !$hooks) {
$this->changed($what, $old, $new); // triggers ___changed hook
} else {
@@ -1225,7 +1239,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
$notice = $this->wire(new $class($text, $flags));
$notice->class = $this->className();
if($this->_notices[$name] === null) $this->_notices[$name] = $this->wire(new Notices());
$notices = $this->wire('notices'); /** @var Notices $notices */
$notices = $this->wire()->notices;
if($notices) $notices->add($notice); // system wide
if(!($notice->flags & Notice::logOnly)) $this->_notices[$name]->add($notice); // local only
return $this;
@@ -1620,6 +1634,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
$wired = $this->_wire;
if($wired === $wire) return;
$this->_wire = $wire;
if($this->_wireHooks) $this->_wireHooks = $wire->wire()->hooks;
if($wired) return;
$this->getInstanceNum();
$this->wired();
@@ -1818,7 +1833,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
if($value !== null) return $value;
}
$hooks = $this->wire('hooks'); /** @var WireHooks $hooks */
$hooks = $this->_wireHooks(); /** @var WireHooks $hooks */
if($hooks && $hooks->isHooked($name)) { // potential property hook
$result = $hooks->runHooks($this, $name, array(), 'property');
return $result['return'];