1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-23 23:02:58 +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(); 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 * Return all local hooks for this instance
* *
@@ -423,7 +439,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
return $this->_callMethod($method, $arguments); return $this->_callMethod($method, $arguments);
} }
/** @var WireHooks $hooks */ /** @var WireHooks $hooks */
$hooks = $this->wire('hooks'); $hooks = $this->_wireHooks();
if($hooks && $hooks->isMethodHooked($this, $method)) { if($hooks && $hooks->isMethodHooked($this, $method)) {
$result = $hooks->runHooks($this, $method, $arguments); $result = $hooks->runHooks($this, $method, $arguments);
return $result['return']; return $result['return'];
@@ -460,7 +476,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
if($val !== null) return $val; if($val !== null) return $val;
} }
} }
$hooks = $this->wire('hooks'); /** @var WireHooks $hooks */ $hooks = $this->_wireHooks();
if($hooks) { if($hooks) {
$result = $hooks->runHooks($this, $method, $arguments); $result = $hooks->runHooks($this, $method, $arguments);
if(!$result['methodExists'] && !$result['numHooksRun']) { if(!$result['methodExists'] && !$result['numHooksRun']) {
@@ -539,7 +555,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
*/ */
protected function ___callUnknown($method, $arguments) { protected function ___callUnknown($method, $arguments) {
if($arguments) {} // intentional to avoid unused argument notice if($arguments) {} // intentional to avoid unused argument notice
$config = $this->wire('config'); $config = $this->wire()->config;
if($config && $config->disableUnknownMethodException) return null; if($config && $config->disableUnknownMethodException) return null;
throw new WireException("Method " . $this->className() . "::$method does not exist or is not callable in this context"); 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') { 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) { 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) { static public function isHooked($method, Wire $instance = null) {
/** @var ProcessWire $wire */ /** @var ProcessWire $wire */
$wire = $instance ? $instance->wire() : ProcessWire::getCurrentInstance(); $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); return $wire->hooks->isHooked($method);
} }
@@ -630,17 +646,15 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
* *
* #pw-group-hooks * #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 method, it should be in the form `method()`.
* - If checking for a hooked property, it should be in the form `property`. * - 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. * @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. * @throws WireException When you try to call it with a Class::something() type method, which is not supported.
* *
*/ */
public function hasHook($method) { public function hasHook($name) {
// Accomplishes the same thing as the static isHooked() method, but this is non-static, more accruate, return $this->_wireHooks()->hasHook($this, $name);
// potentially slower than isHooked(). Less for optimization use, more for accuracy use.
return $this->wire('hooks')->hasHook($this, $method);
} }
/** /**
@@ -684,7 +698,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
* *
*/ */
public function addHook($method, $toObject, $toMethod = null, $options = array()) { 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. // This is the same as calling addHook with the 'before' option set the $options array.
$options['before'] = true; $options['before'] = true;
if(!isset($options['after'])) $options['after'] = false; 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()) { public function addHookAfter($method, $toObject, $toMethod = null, $options = array()) {
$options['after'] = true; $options['after'] = true;
if(!isset($options['before'])) $options['before'] = false; 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. // 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). // Note that descending classes that override __get must call getHook($property) and/or runHook($property).
$options['type'] = '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()) { 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) { 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) { if(is_null($old) || is_null($new) || $lastValue !== $new) {
/** @var WireHooks $hooks */ /** @var WireHooks $hooks */
$hooks = $this->wire('hooks'); $hooks = $this->_wireHooks();
if(($hooks && $hooks->isHooked('changed()')) || !$hooks) { if(($hooks && $hooks->isHooked('changed()')) || !$hooks) {
$this->changed($what, $old, $new); // triggers ___changed hook $this->changed($what, $old, $new); // triggers ___changed hook
} else { } else {
@@ -1225,7 +1239,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
$notice = $this->wire(new $class($text, $flags)); $notice = $this->wire(new $class($text, $flags));
$notice->class = $this->className(); $notice->class = $this->className();
if($this->_notices[$name] === null) $this->_notices[$name] = $this->wire(new Notices()); 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($notices) $notices->add($notice); // system wide
if(!($notice->flags & Notice::logOnly)) $this->_notices[$name]->add($notice); // local only if(!($notice->flags & Notice::logOnly)) $this->_notices[$name]->add($notice); // local only
return $this; return $this;
@@ -1620,6 +1634,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
$wired = $this->_wire; $wired = $this->_wire;
if($wired === $wire) return; if($wired === $wire) return;
$this->_wire = $wire; $this->_wire = $wire;
if($this->_wireHooks) $this->_wireHooks = $wire->wire()->hooks;
if($wired) return; if($wired) return;
$this->getInstanceNum(); $this->getInstanceNum();
$this->wired(); $this->wired();
@@ -1818,7 +1833,7 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable {
if($value !== null) return $value; 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 if($hooks && $hooks->isHooked($name)) { // potential property hook
$result = $hooks->runHooks($this, $name, array(), 'property'); $result = $hooks->runHooks($this, $name, array(), 'property');
return $result['return']; return $result['return'];