Repository: pheature-flags/pheature-flags (php)

Issue:
[Symfony-toggle] Create SegmentFactory Compiler pass
**Epic:** https://github.com/pheature-flags/pheature-flags/issues/97 Symfony Toggle
**Package:** https://github.com/pheature-flags/symfony-toggle 

**Description:**

Create SegmentFactory Compiler pass from existing PheatureFlagsExtension. And include the compiler pass in the extension class.

```php

        $segmentFactory = $container->register(SegmentFactory::class, SegmentFactory::class)
            ->setAutowired(false)
            ->setLazy(true)
            ->setClass(ChainSegmentFactory::class);

        foreach ($mergedConfig['segment_types'] as $segmentDefinition) {
            $container->register($segmentDefinition['type'], $segmentDefinition['factory_id'])
                ->setAutowired(false)
                ->setLazy(true);

            $segmentFactory->addArgument(new Reference($segmentDefinition['type']));
        }
```

Pull request:
[#170] create segment factory compiler pass
Closes #170.

Patch:
diff --git a/packages/symfony-toggle/src/DependencyInjection/PheatureFlagsExtension.php b/packages/symfony-toggle/src/DependencyInjection/PheatureFlagsExtension.php
index 991ede0..a296ad7 100644
--- a/packages/symfony-toggle/src/DependencyInjection/PheatureFlagsExtension.php
+++ b/packages/symfony-toggle/src/DependencyInjection/PheatureFlagsExtension.php
@@ -5,10 +5,8 @@ declare(strict_types=1);
 namespace Pheature\Community\Symfony\DependencyInjection;
 
 use Doctrine\DBAL\Connection;
-use Pheature\Core\Toggle\Read\ChainSegmentFactory;
 use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory;
 use Pheature\Core\Toggle\Read\FeatureFinder;
-use Pheature\Core\Toggle\Read\SegmentFactory;
 use Pheature\Core\Toggle\Read\Toggle;
 use Pheature\Core\Toggle\Write\FeatureRepository;
 use Pheature\Crud\Psr11\Toggle\FeatureFinderFactory;
@@ -60,19 +58,6 @@ final class PheatureFlagsExtension extends ConfigurableExtension
                 ->addArgument(new Reference(ChainToggleStrategyFactory::class));
         }
 
-        $segmentFactory = $container->register(SegmentFactory::class, SegmentFactory::class)
-            ->setAutowired(false)
-            ->setLazy(true)
-            ->setClass(ChainSegmentFactory::class);
-
-        foreach ($mergedConfig['segment_types'] as $segmentDefinition) {
-            $container->register($segmentDefinition['type'], $segmentDefinition['factory_id'])
-                ->setAutowired(false)
-                ->setLazy(true);
-
-            $segmentFactory->addArgument(new Reference($segmentDefinition['type']));
-        }
-
         $container->register(Toggle::class, Toggle::class)
             ->setAutowired(false)
             ->setLazy(true)
diff --git a/packages/symfony-toggle/src/DependencyInjection/SegmentFactoryPass.php b/packages/symfony-toggle/src/DependencyInjection/SegmentFactoryPass.php
new file mode 100644
index 0000000..2539d95
--- /dev/null
+++ b/packages/symfony-toggle/src/DependencyInjection/SegmentFactoryPass.php
@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Pheature\Community\Symfony\DependencyInjection;
+
+use Pheature\Core\Toggle\Read\ChainSegmentFactory;
+use Pheature\Core\Toggle\Read\SegmentFactory;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+final class SegmentFactoryPass implements CompilerPassInterface
+{
+    public function process(ContainerBuilder $container): void
+    {
+        /** @var array<array<mixed>> $pheatureFlagsConfig */
+        $pheatureFlagsConfig = $container->getExtensionConfig('pheature_flags');
+        $mergedConfig = array_merge(...$pheatureFlagsConfig);
+
+        $segmentFactory = $container->register(SegmentFactory::class, SegmentFactory::class)
+            ->setAutowired(false)
+            ->setLazy(true)
+            ->setClass(ChainSegmentFactory::class);
+
+        foreach ($mergedConfig['segment_types'] as $segmentDefinition) {
+            $container->register($segmentDefinition['type'], $segmentDefinition['factory_id'])
+                ->setAutowired(false)
+                ->setLazy(true);
+
+            $segmentFactory->addArgument(new Reference($segmentDefinition['type']));
+        }
+    }
+}
diff --git a/packages/symfony-toggle/src/PheatureFlagsBundle.php b/packages/symfony-toggle/src/PheatureFlagsBundle.php
index 361080d..6b710a1 100644
--- a/packages/symfony-toggle/src/PheatureFlagsBundle.php
+++ b/packages/symfony-toggle/src/PheatureFlagsBundle.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace Pheature\Community\Symfony;
 
+use Pheature\Community\Symfony\DependencyInjection\SegmentFactoryPass;
 use Pheature\Community\Symfony\DependencyInjection\ToggleStrategyFactoryPass;
 use Pheature\Model\Toggle\EnableByMatchingIdentityId;
 use Pheature\Model\Toggle\EnableByMatchingSegment;
@@ -46,7 +47,7 @@ final class PheatureFlagsBundle extends Bundle
     {
         $container->loadFromExtension('pheature_flags', self::DEFAULT_CONFIG);
 
-        $toggleStrategyFactoryPass = new ToggleStrategyFactoryPass();
-        $toggleStrategyFactoryPass->process($container);
+        $container->addCompilerPass(new SegmentFactoryPass());
+        $container->addCompilerPass(new ToggleStrategyFactoryPass());
     }
 }


Test patch:
diff --git a/packages/symfony-toggle/test/DependencyInjection/SegmentFactoryPassTest.php b/packages/symfony-toggle/test/DependencyInjection/SegmentFactoryPassTest.php
new file mode 100644
index 0000000..548c7f6
--- /dev/null
+++ b/packages/symfony-toggle/test/DependencyInjection/SegmentFactoryPassTest.php
@@ -0,0 +1,36 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Pheature\Test\Community\Symfony\DependencyInjection;
+
+use Pheature\Community\Symfony\DependencyInjection\SegmentFactoryPass;
+use Pheature\Core\Toggle\Read\ChainSegmentFactory;
+use Pheature\Core\Toggle\Read\SegmentFactory;
+use Pheature\Model\Toggle\IdentitySegment;
+use Pheature\Model\Toggle\StrictMatchingSegment;
+use PHPUnit\Framework\TestCase;
+
+final class SegmentFactoryPassTest extends TestCase
+{
+    public function testItShouldRegisterToggleStrategyFactoryInContainer(): void
+    {
+        $compilerPass = new SegmentFactoryPass();
+        $container = TestContainerFactory::create($compilerPass);
+
+        $segmentFactoryDefinition = $container->getDefinition(SegmentFactory::class);
+        self::assertFalse($segmentFactoryDefinition->isAutowired());
+        self::assertTrue($segmentFactoryDefinition->isLazy());
+        self::assertCount(2, $segmentFactoryDefinition->getArguments());
+
+        $segment1 = $container->getDefinition(IdentitySegment::NAME);
+        self::assertFalse($segment1->isAutowired());
+        self::assertTrue($segment1->isLazy());
+        $segment2 = $container->getDefinition(StrictMatchingSegment::NAME);
+        self::assertFalse($segment2->isAutowired());
+        self::assertTrue($segment2->isLazy());
+
+        $segmentFactory = $container->get(SegmentFactory::class);
+        self::assertInstanceOf(ChainSegmentFactory::class, $segmentFactory);
+    }
+}