-
-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CiviEventDispatcher - Fix pass-by-reference of hook-style arguments for service-based listeners #24282
Conversation
(Standard links)
|
…or service-based listeners Suppose you are firing `hook_civicrm_foo` to a serivce-based listener taht uses hook-style arguments. Conceptually, this is a call like: Civi::service('foo')->hook_civicrm_foo($arg1, &$arg2, $arg3); Before this patch, all values are pass-by-value. Changes to `&$arg2` are not propagated back out. The patch ensures that `&$arg2` propagates back out.
d006748
to
6551756
Compare
I tried testing this but I might not be setting it up right. In the test here, it explicitly registers the class. In the docs it says it will automatically find it, so I tried that: class CRM_Abc_Hook implements \Civi\Core\HookInterface {
public function hook_civicrm_check(&$msg, $statusNames, $includeDisabled) {
$msg[] = new CRM_Utils_Check_Message(
'abcext',
ts('Abc Ext'),
ts('Abc abc abc'),
\Psr\Log\LogLevel::WARNING,
'fa-flag'
);
}
} but it doesn't seem to pick up. Clearing cache didn't change anything. It makes some sense that I might need to register it, but then do the docs need updating? If I implement that same hook the "normal" way in the extension's main abc.php file it does work. |
Quite right... There's a limitation -- which isn't super visible, but it's mentioned in the docs under "Which classes support declarative listeners?". Basically, up to current The aim of #24276 is to open up a broader auto-registration (which can work with any class). There's a test that relies on auto-registration in that PR: totten@0067e4a. But... the test was failing... because of this bug. That branch is already fairly big, so I just extracted this piece (smaller/more readable PR). This PR only fixes the bug in |
And yeah... it is a bit cumbersome to reproduce the bug without autoregistration... You basically have to write a service, register it manually, and then check that the service gets notifications. (Which is what the test 6551756 does.) |
Aha. So |
Overview
Suppose you have a hook-listener class:
And suppose this class is registered as a service. Conceptually, when firing
hook_civicrm_foo
, it should run the equivalent ofThis fixes a bug in handling the
&
above.(Note: Includes/depends on a commit from #24283.)Before
It does run, but the data is passed to the hook by-value. References (eg
&$arg2
) are lost. Parameters cannot be altered.After
References are preserved. Parameters can be altered. There's a test to show this.
Technical Details
This includes a couple other commits from Autoload services based on interfaces/annotations. Convert APIv4 services. #24276. These other commits touch code that already gets a lot of testing, and they enable additional testing of this bug.
There are two existing adapters,
HookStyleListener
andServiceListener
. Before, they were stacked on top of each other. Separately, they work with alterable params -- but something about the combination doesn't work. I fiddled for a while and couldn't find a dainty solution. So this just adds a third adapter which does the same thing (ieHookStyleServiceListener =~ HookStyleListener + ServiceListener
).