diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServices/Tags.php b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServices/Tags.php index 68f501fc..12c3db72 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServices/Tags.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServices/Tags.php @@ -96,6 +96,25 @@ public function getIterator(): \Traversable return new \ArrayIterator($this->tags); } + public function resetOnEachRequest(): bool + { + if (!$this->hasStatefulServiceTag() && !$this->hasSafeStatefulServiceTag()) { + return false; + } + + if ($this->hasSafeStatefulServiceTag()) { + return true; + } + + $tag = $this->findStatefulServiceTag(); + + if ($tag->getResetOnEachRequest()) { + return true; + } + + return false; + } + /** * @return array>> */ diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServicesPass.php b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServicesPass.php index ae16ed91..b91f8fe7 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServicesPass.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/CompilerPass/StatefulServicesPass.php @@ -240,13 +240,8 @@ private function reduceServiceResetters(ContainerBuilder $container): void /** @var class-string $classString */ $classString = $valueDef->getClass(); $tags = new Tags($classString, $valueDef->getTags()); - $tag = $tags->findStatefulServiceTag(); - if (null === $tag) { - continue; - } - - if (!$tag->getResetOnEachRequest()) { + if (!$tags->resetOnEachRequest()) { continue; } diff --git a/tests/Feature/SwooleServerCoroutinesTest.php b/tests/Feature/SwooleServerCoroutinesTest.php index ed3d8b19..26a83831 100644 --- a/tests/Feature/SwooleServerCoroutinesTest.php +++ b/tests/Feature/SwooleServerCoroutinesTest.php @@ -65,6 +65,8 @@ public function testCoroutinesWithDebugOn(): void $this->assertStringContainsString('Service2 was proxified.', $response['body']); $this->assertStringContainsString('Service2 limit is 10.', $response['body']); $this->assertStringContainsString('Always reset works.', $response['body']); + $this->assertStringContainsString('Safe always reseter is not a proxy.', $response['body']); + $this->assertStringContainsString('Safe Always reset works.', $response['body']); $this->assertStringContainsString('TmpRepo was proxified.', $response['body']); $this->assertStringContainsString('TmpRepo limit is 15.', $response['body']); $this->assertStringContainsString('Connection limit is 12.', $response['body']); @@ -138,6 +140,8 @@ public function testCoroutinesWithDebugOff(): void $this->assertStringContainsString('Service2 was proxified.', $response['body']); $this->assertStringContainsString('Service2 limit is 10.', $response['body']); $this->assertStringContainsString('Always reset works.', $response['body']); + $this->assertStringContainsString('Safe always reseter is not a proxy.', $response['body']); + $this->assertStringContainsString('Safe Always reset works.', $response['body']); $this->assertStringContainsString('TmpRepo was proxified.', $response['body']); $this->assertStringContainsString('TmpRepo limit is 15.', $response['body']); $this->assertStringContainsString('Connection limit is 12.', $response['body']); diff --git a/tests/Fixtures/Symfony/TestBundle/Controller/SleepController.php b/tests/Fixtures/Symfony/TestBundle/Controller/SleepController.php index 32cac553..fb85a241 100644 --- a/tests/Fixtures/Symfony/TestBundle/Controller/SleepController.php +++ b/tests/Fixtures/Symfony/TestBundle/Controller/SleepController.php @@ -75,6 +75,10 @@ public function index() $limitProperty->setAccessible(true); $limit = $limitProperty->getValue($servicePool); $alwaysResetWorks = $this->shouldBeProxified->wasDummyReset(); + $alwaysResetSafe = $this->shouldBeProxified->getSafeDummy(); + /** @phpstan-ignore-next-line */ + $safeDummyIsProxy = $alwaysResetSafe instanceof VirtualProxyInterface ? 'IS' : 'is not'; + $safeAlwaysResetWorks = $alwaysResetSafe->getWasReset(); $rc2 = new \ReflectionClass(DefaultDummyService::class); $tmpRepoProperty = $rc2->getProperty('tmpRepository'); @@ -99,6 +103,8 @@ public function index() ."Checks: {$checks}. " ."Service {$isProxified} proxified. Service2 {$isProxified2} proxified. " .'Always reset '.($alwaysResetWorks ? 'works' : 'did not work').'. ' + ."Safe always reseter {$safeDummyIsProxy} a proxy. " + .'Safe Always reset '.($safeAlwaysResetWorks ? 'works' : 'did not work').'. ' ."Service2 limit is {$limit}. TmpRepo {$isProxified3} proxified. " ."TmpRepo limit is {$limit2}. " ."Connection limit is {$connlimit}." diff --git a/tests/Fixtures/Symfony/TestBundle/Service/AlwaysResetSafe.php b/tests/Fixtures/Symfony/TestBundle/Service/AlwaysResetSafe.php new file mode 100644 index 00000000..75437362 --- /dev/null +++ b/tests/Fixtures/Symfony/TestBundle/Service/AlwaysResetSafe.php @@ -0,0 +1,22 @@ +wasReset = true; + } + + public function getWasReset(): bool + { + return $this->wasReset; + } +} diff --git a/tests/Fixtures/Symfony/TestBundle/Service/ShouldBeProxified.php b/tests/Fixtures/Symfony/TestBundle/Service/ShouldBeProxified.php index 2df7490f..df3d0760 100644 --- a/tests/Fixtures/Symfony/TestBundle/Service/ShouldBeProxified.php +++ b/tests/Fixtures/Symfony/TestBundle/Service/ShouldBeProxified.php @@ -8,13 +8,21 @@ final class ShouldBeProxified { private AlwaysReset $dummy; - public function __construct(AlwaysReset $dummy) + private AlwaysResetSafe $safeDummy; + + public function __construct(AlwaysReset $dummy, AlwaysResetSafe $safeDummy) { $this->dummy = $dummy; + $this->safeDummy = $safeDummy; } public function wasDummyReset(): bool { return $this->dummy->getWasReset(); } + + public function getSafeDummy(): AlwaysResetSafe + { + return $this->safeDummy; + } } diff --git a/tests/Fixtures/Symfony/app/config/coroutines/swoole.yaml b/tests/Fixtures/Symfony/app/config/coroutines/swoole.yaml index 9327ed72..ec441b3d 100644 --- a/tests/Fixtures/Symfony/app/config/coroutines/swoole.yaml +++ b/tests/Fixtures/Symfony/app/config/coroutines/swoole.yaml @@ -52,3 +52,7 @@ services: K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysReset: tags: - { name: 'swoole_bundle.stateful_service', reset_on_each_request: true } + + K911\Swoole\Tests\Fixtures\Symfony\TestBundle\Service\AlwaysResetSafe: + tags: + - { name: 'swoole_bundle.safe_stateful_service' }