From d5fac047713addfe9ec87bf7c3aafc328564cd59 Mon Sep 17 00:00:00 2001 From: Aydin Hassan Date: Fri, 23 Oct 2026 09:28:53 +0200 Subject: [PATCH] Make containerListener return a compilable definition containerListener() returned an arrow function, which captures its arguments. PHP-DI cannot compile closures that capture variables, so any app that enabled container compilation failed to boot. It now returns a create() definition that resolves to the same ContainerListenerHelper. --- src/Event/functions.php | 11 ++++++++-- test/ContainerCompilationTest.php | 23 +++++++++++++++++++++ test/Factory/EventDispatcherFactoryTest.php | 11 +++++----- 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 test/ContainerCompilationTest.php diff --git a/src/Event/functions.php b/src/Event/functions.php index 5f9436f4..e8e7411c 100644 --- a/src/Event/functions.php +++ b/src/Event/functions.php @@ -4,10 +4,17 @@ namespace PhpSchool\PhpWorkshop\Event; +use DI\Definition\Helper\CreateDefinitionHelper; + +use function DI\create; + if (!function_exists('PhpSchool\PhpWorkshop\Event\containerListener')) { - function containerListener(string $service, string $method = '__invoke'): callable + /** + * A container definition rather than a closure, so containers that use it can be compiled. + */ + function containerListener(string $service, string $method = '__invoke'): CreateDefinitionHelper { - return fn() => new ContainerListenerHelper($service, $method); + return create(ContainerListenerHelper::class)->constructor($service, $method); } } diff --git a/test/ContainerCompilationTest.php b/test/ContainerCompilationTest.php new file mode 100644 index 00000000..d19afe12 --- /dev/null +++ b/test/ContainerCompilationTest.php @@ -0,0 +1,23 @@ +addDefinitions(['basePath' => __DIR__ . '/..']); + $containerBuilder->addDefinitions(require __DIR__ . '/../app/config.php'); + $containerBuilder->enableCompilation($this->getTemporaryDirectory(), 'CompiledWorkshopContainer'); + + $container = $containerBuilder->build(); + + $this->assertInstanceOf(EventDispatcher::class, $container->get(EventDispatcher::class)); + } +} diff --git a/test/Factory/EventDispatcherFactoryTest.php b/test/Factory/EventDispatcherFactoryTest.php index d0e14014..fc4201b3 100644 --- a/test/Factory/EventDispatcherFactoryTest.php +++ b/test/Factory/EventDispatcherFactoryTest.php @@ -6,13 +6,12 @@ use PhpSchool\PhpWorkshop\Event\Event; use Psr\Container\ContainerInterface; use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException; +use PhpSchool\PhpWorkshop\Event\ContainerListenerHelper; use PhpSchool\PhpWorkshop\Factory\EventDispatcherFactory; use PhpSchool\PhpWorkshop\ResultAggregator; use PhpSchool\PhpWorkshopTest\Asset\CallableStub; use PHPUnit\Framework\TestCase; -use function PhpSchool\PhpWorkshop\Event\containerListener; - class EventDispatcherFactoryTest extends TestCase { public function testCreateWithNoConfig(): void @@ -113,7 +112,7 @@ public function testExceptionIsThrownIfEventsListenerContainerEntryNotExist(): v { $eventConfig = [ 'my-group' => [ - 'someEvent' => [containerListener('nonExistingContainerEntry')()], + 'someEvent' => [new ContainerListenerHelper('nonExistingContainerEntry')], ], ]; @@ -172,7 +171,7 @@ public function testListenerFromContainerIsNotFetchedDuringAttaching(): void { $eventConfig = [ 'my-group' => [ - 'someEvent' => [containerListener('containerEntry')()], + 'someEvent' => [new ContainerListenerHelper('containerEntry')], ], ]; @@ -199,7 +198,7 @@ public function testListenerFromContainerIsFetchedWhenEventDispatched(): void { $eventConfig = [ 'my-group' => [ - 'someEvent' => [containerListener('containerEntry')()], + 'someEvent' => [new ContainerListenerHelper('containerEntry')], ], ]; @@ -232,7 +231,7 @@ public function testExceptionIsThrownIfMethodDoesNotExistOnContainerEntry(): voi { $eventConfig = [ 'my-group' => [ - 'someEvent' => [containerListener('containerEntry', 'notHere')()], + 'someEvent' => [new ContainerListenerHelper('containerEntry', 'notHere')], ], ];