Skip to content

Commit 8865f21

Browse files
committed
refactor(aibundle): improvement on alias for store
1 parent 68b6181 commit 8865f21

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
697697
->setArguments($arguments);
698698

699699
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
700-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
700+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
701+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
701702
}
702703
}
703704

@@ -729,7 +730,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
729730
->setArguments($arguments);
730731

731732
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
732-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
733+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
734+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
733735
}
734736
}
735737

@@ -744,7 +746,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
744746
->addTag('ai.store');
745747

746748
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
747-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
749+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
750+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
748751
}
749752
}
750753

@@ -771,7 +774,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
771774
;
772775

773776
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
774-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
777+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
778+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
775779
}
776780
}
777781

@@ -802,6 +806,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
802806
->setArguments($arguments);
803807

804808
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
809+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
810+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
805811
}
806812
}
807813

@@ -832,7 +838,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
832838
->setArguments($arguments);
833839

834840
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
835-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
841+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
842+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
836843
}
837844
}
838845

@@ -857,7 +864,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
857864
->setArguments($arguments);
858865

859866
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
860-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
867+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
868+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
861869
}
862870
}
863871

@@ -889,7 +897,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
889897
->setArguments($arguments);
890898

891899
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
892-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
900+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
901+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
893902
}
894903
}
895904

@@ -916,7 +925,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
916925
->setArguments($arguments);
917926

918927
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
919-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
928+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
929+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
920930
}
921931
}
922932

@@ -954,7 +964,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
954964
->setArguments($arguments);
955965

956966
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
957-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
967+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
968+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
958969
}
959970
}
960971

@@ -979,7 +990,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
979990
->setArguments($arguments);
980991

981992
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
982-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
993+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
994+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
983995
}
984996
}
985997

@@ -1006,7 +1018,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10061018
->setArguments($arguments);
10071019

10081020
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1009-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1021+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1022+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10101023
}
10111024
}
10121025

@@ -1047,7 +1060,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10471060
->setArguments($arguments);
10481061

10491062
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1050-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1063+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1064+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10511065
}
10521066
}
10531067

@@ -1074,7 +1088,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10741088
->setArguments($arguments);
10751089

10761090
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1077-
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, (new Target($name.'Store'))->getParsedName());
1091+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1092+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10781093
}
10791094
}
10801095

@@ -1093,6 +1108,8 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
10931108
->setArguments($arguments);
10941109

10951110
$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
1111+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $name);
1112+
$container->registerAliasForArgument('ai.store.'.$name, StoreInterface::class, $type.'_'.$name);
10961113
}
10971114
}
10981115
}

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\AI\Store\Document\Loader\InMemoryLoader;
3030
use Symfony\AI\Store\Document\Transformer\TextTrimTransformer;
3131
use Symfony\AI\Store\Document\Vectorizer;
32+
use Symfony\AI\Store\StoreInterface;
3233
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
3334
use Symfony\Component\DependencyInjection\ContainerBuilder;
3435
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -100,6 +101,37 @@ public function testInjectionAgentAliasIsRegistered()
100101
$this->assertTrue($container->hasAlias(AgentInterface::class.' $myAgentAgent'));
101102
}
102103

104+
public function testInjectionStoreAliasIsRegistered()
105+
{
106+
$container = $this->buildContainer([
107+
'ai' => [
108+
'store' => [
109+
'memory' => [
110+
'main' => [
111+
'strategy' => 'cosine',
112+
],
113+
'secondary_with_custom_strategy' => [
114+
'strategy' => 'manhattan',
115+
],
116+
],
117+
'weaviate' => [
118+
'main' => [
119+
'endpoint' => 'http://localhost:8080',
120+
'api_key' => 'bar',
121+
'collection' => 'my_weaviate_collection',
122+
],
123+
],
124+
],
125+
],
126+
]);
127+
128+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $main'));
129+
$this->assertTrue($container->hasAlias('.'.StoreInterface::class.' $secondary_with_custom_strategy'));
130+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $secondaryWithCustomStrategy'));
131+
$this->assertTrue($container->hasAlias('.'.StoreInterface::class.' $weaviate_main'));
132+
$this->assertTrue($container->hasAlias(StoreInterface::class.' $weaviateMain'));
133+
}
134+
103135
public function testAgentHasTag()
104136
{
105137
$container = $this->buildContainer([

0 commit comments

Comments
 (0)