1111
1212namespace Overblog \DataLoaderBundle \DependencyInjection ;
1313
14+ use Overblog \DataLoader \DataLoader ;
15+ use Overblog \DataLoaderBundle \Attribute \AsDataLoader ;
1416use Symfony \Component \Config \FileLocator ;
17+ use Symfony \Component \DependencyInjection \ChildDefinition ;
1518use Symfony \Component \DependencyInjection \ContainerBuilder ;
1619use Symfony \Component \DependencyInjection \Extension \Extension ;
1720use Symfony \Component \DependencyInjection \Loader \YamlFileLoader ;
18- use Symfony \Component \DependencyInjection \Reference ;
1921
2022final class OverblogDataLoaderExtension extends Extension
2123{
@@ -27,89 +29,32 @@ public function load(array $configs, ContainerBuilder $container): void
2729 $ configuration = $ this ->getConfiguration ($ configs , $ container );
2830 $ config = $ this ->processConfiguration ($ configuration , $ configs );
2931
30- foreach ($ config ['loaders ' ] as $ name => $ loaderConfig ) {
31- $ loaderConfig = array_replace ($ config ['defaults ' ], $ loaderConfig );
32- $ dataLoaderServiceID = $ this ->generateDataLoaderServiceIDFromName ($ name , $ container );
33- $ OptionServiceID = $ this ->generateDataLoaderOptionServiceIDFromName ($ name , $ container );
34- $ batchLoadFn = $ this ->buildCallableFromScalar ($ loaderConfig ['batch_load_fn ' ]);
35-
36- $ container ->register ($ OptionServiceID , 'Overblog \\DataLoader \\Option ' )
37- ->setPublic (false )
38- ->setArguments ([$ this ->buildOptionsParams ($ loaderConfig ['options ' ])]);
39-
40- $ definition = $ container ->register ($ dataLoaderServiceID , 'Overblog \\DataLoader \\DataLoader ' )
41- ->setPublic (true )
42- ->addTag ('kernel.reset ' , ['method ' => 'clearAll ' ])
43- ->setArguments ([
44- $ batchLoadFn ,
45- new Reference ($ loaderConfig ['promise_adapter ' ]),
46- new Reference ($ OptionServiceID ),
47- ])
48- ;
49-
50- if (isset ($ loaderConfig ['factory ' ])) {
51- $ definition ->setFactory ($ this ->buildCallableFromScalar ($ loaderConfig ['factory ' ]));
32+ $ container ->registerAttributeForAutoconfiguration (AsDataLoader::class, function (ChildDefinition $ definition , AsDataLoader $ attribute , \ReflectionClass |\ReflectionMethod $ reflector ) use ($ config ) {
33+ if ($ reflector instanceof \ReflectionMethod && null !== $ attribute ->method ) {
34+ throw new \LogicException (sprintf ('Parameter "method" for attribute "%s" must be NULL when applied on a method. ' , AsDataLoader::class));
5235 }
5336
54- if (isset ($ loaderConfig ['alias ' ])) {
55- $ container ->setAlias ($ loaderConfig ['alias ' ], $ dataLoaderServiceID );
56- $ container ->getAlias ($ loaderConfig ['alias ' ])->setPublic (true );
57- }
37+ $ definition ->addTag ('overblog.dataloader ' , array_merge ($ config ['defaults ' ], [
38+ 'name ' => $ attribute ->name ,
39+ 'alias ' => $ attribute ->alias ,
40+ 'method ' => $ reflector instanceof \ReflectionMethod ? $ reflector ->getName () : ($ attribute ->method ?? '__invoke ' ),
41+ 'options ' => array_merge ($ config ['defaults ' ]['options ' ], $ attribute ->options ?? []),
42+ ]));
43+ });
44+
45+ foreach ($ config ['loaders ' ] as $ name => $ loaderConfig ) {
46+ $ container ->register (Support::generateDataLoaderServiceIDFromName ($ name , $ container ), DataLoader::class)
47+ ->addTag ('overblog.dataloader ' , array_merge ($ config ['defaults ' ], [
48+ 'name ' => $ name ,
49+ 'alias ' => $ loaderConfig ['alias ' ] ?? null ,
50+ 'batch_load_fn ' => $ loaderConfig ['batch_load_fn ' ],
51+ 'options ' => array_merge ($ config ['defaults ' ]['options ' ], $ loaderConfig ['options ' ] ?? []),
52+ ]));
5853 }
5954 }
6055
6156 public function getAlias (): string
6257 {
63- return 'overblog_dataloader ' ;
64- }
65-
66- private function generateDataLoaderServiceIDFromName ($ name , ContainerBuilder $ container ): string
67- {
68- return sprintf ('%s.%s_loader ' , $ this ->getAlias (), $ container ->underscore ($ name ));
69- }
70-
71- private function generateDataLoaderOptionServiceIDFromName ($ name , ContainerBuilder $ container ): string
72- {
73- return sprintf ('%s_option ' , $ this ->generateDataLoaderServiceIDFromName ($ name , $ container ));
74- }
75-
76- private function buildOptionsParams (array $ options ): array
77- {
78- $ optionsParams = [];
79-
80- $ optionsParams ['batch ' ] = $ options ['batch ' ];
81- $ optionsParams ['cache ' ] = $ options ['cache ' ];
82- $ optionsParams ['maxBatchSize ' ] = $ options ['max_batch_size ' ];
83- $ optionsParams ['cacheMap ' ] = new Reference ($ options ['cache_map ' ]);
84- $ optionsParams ['cacheKeyFn ' ] = $ this ->buildCallableFromScalar ($ options ['cache_key_fn ' ]);
85-
86- return $ optionsParams ;
87- }
88-
89- private function buildCallableFromScalar ($ scalar ): mixed
90- {
91- $ matches = null ;
92-
93- if (null === $ scalar ) {
94- return null ;
95- }
96-
97- if (preg_match (Configuration::SERVICE_CALLABLE_NOTATION_REGEX , $ scalar , $ matches )) {
98- $ function = new Reference ($ matches ['service_id ' ]);
99- if (empty ($ matches ['method ' ])) {
100- return $ function ;
101- } else {
102- return [$ function , $ matches ['method ' ]];
103- }
104- } elseif (preg_match (Configuration::PHP_CALLABLE_NOTATION_REGEX , $ scalar , $ matches )) {
105- $ function = $ matches ['function ' ];
106- if (empty ($ matches ['method ' ])) {
107- return $ function ;
108- } else {
109- return [$ function , $ matches ['method ' ]];
110- }
111- }
112-
113- return null ;
58+ return Support::getAlias ();
11459 }
11560}
0 commit comments