-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24282 from totten/master-hookstyle-svc
CiviEventDispatcher - Fix pass-by-reference of hook-style arguments for service-based listeners
- Loading branch information
Showing
5 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Civi\Core\Event; | ||
|
||
/** | ||
* A hook-style service-listener is a callable with two properties: | ||
* | ||
* - The parameters are given in hook style. | ||
* - The callback is a method in a service-class. | ||
* | ||
* It is comparable to running: | ||
* | ||
* Civi::service('foo')->hook_civicrm_foo($arg1, $arg2, ...); | ||
*/ | ||
class HookStyleServiceListener extends ServiceListener { | ||
|
||
public function __invoke(...$args) { | ||
if ($this->liveCb === NULL) { | ||
$c = $this->container ?: \Civi::container(); | ||
$this->liveCb = [$c->get($this->inertCb[0]), $this->inertCb[1]]; | ||
} | ||
|
||
$result = call_user_func_array($this->liveCb, $args[0]->getHookValues()); | ||
$args[0]->addReturnValues($result); | ||
} | ||
|
||
public function __toString() { | ||
$class = $this->getServiceClass(); | ||
if ($class) { | ||
return sprintf('$(%s)->%s(...$args) [%s]', $this->inertCb[0], $this->inertCb[1], $class); | ||
} | ||
else { | ||
return sprintf('\$(%s)->%s(...$args)', $this->inertCb[0], $this->inertCb[1]); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/phpunit/Civi/Core/Event/HookStyleServiceListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Civi\Core\Event; | ||
|
||
use Civi\Core\Container; | ||
use Civi\Core\HookInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Register a service (eg 'test.hssvlt') with a hook method (eg `function hook_civicrm_hssvlt()`). | ||
* Ensure that the hook method can alter data. | ||
*/ | ||
class HookStyleServiceListenerTest extends \CiviUnitTestCase { | ||
|
||
public function tearDown(): void { | ||
HookStyleServiceListenerTestExample::$notes = []; | ||
parent::tearDown(); | ||
} | ||
|
||
public function testDispatch() { | ||
$changeMe = $rand = rand(0, 16384); | ||
|
||
$this->useCustomContainer(function (ContainerBuilder $container) use ($rand) { | ||
$container->register('test.hssvlt', HookStyleServiceListenerTestExample::class) | ||
->addArgument($rand) | ||
->addTag('event_subscriber') | ||
->setPublic(TRUE); | ||
}); | ||
|
||
$d = \Civi::dispatcher(); | ||
|
||
// Baseline | ||
$this->assertEquals([], HookStyleServiceListenerTestExample::$notes); | ||
$this->assertEquals($changeMe, $rand); | ||
|
||
// First call - instantiate and run | ||
$d->dispatch('hook_civicrm_hssvlt', GenericHookEvent::create(['foo' => &$changeMe])); | ||
$this->assertEquals($changeMe, 1 + $rand); | ||
$this->assertEquals(["construct($rand)", "fired($rand)"], HookStyleServiceListenerTestExample::$notes); | ||
|
||
// Second call - reuse and run | ||
$d->dispatch('hook_civicrm_hssvlt', GenericHookEvent::create(['foo' => &$changeMe])); | ||
$this->assertEquals($changeMe, 2 + $rand); | ||
$this->assertEquals(["construct($rand)", "fired($rand)", "fired(" . ($rand + 1) . ")"], HookStyleServiceListenerTestExample::$notes); | ||
} | ||
|
||
/** | ||
* Create and activate a custom service-container. | ||
* | ||
* @param callable $callback | ||
* Callback function which will modify the container. | ||
* function(ContainerBuilder $container) | ||
*/ | ||
protected function useCustomContainer(callable $callback) { | ||
$container = (new Container())->createContainer(); | ||
$callback($container); | ||
$container->compile(); | ||
Container::useContainer($container); | ||
} | ||
|
||
} | ||
|
||
class HookStyleServiceListenerTestExample implements HookInterface { | ||
|
||
/** | ||
* Free-form list of strings. | ||
* | ||
* @var array | ||
*/ | ||
public static $notes = []; | ||
|
||
public function __construct($rand) { | ||
self::$notes[] = "construct($rand)"; | ||
} | ||
|
||
public function hook_civicrm_hssvlt(&$foo) { | ||
self::$notes[] = "fired({$foo})"; | ||
$foo++; | ||
} | ||
|
||
} |