This repository was archived by the owner on Jul 15, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +24
-14
lines changed
DependencyInjection/CompilerPass Expand file tree Collapse file tree 3 files changed +24
-14
lines changed Original file line number Diff line number Diff line change @@ -30,17 +30,12 @@ public function process(ContainerBuilder $container)
30
30
return ;
31
31
}
32
32
33
- $ clients = $ container ->findTaggedServiceIds ('csa_guzzle.client ' );
34
-
35
- if (!count ($ clients )) {
36
- return ;
37
- }
38
-
39
- foreach ($ clients as $ id => $ options ) {
40
- $ client = $ container ->findDefinition ($ id );
41
- foreach ($ subscribers as $ subscriber => $ options ) {
42
- $ client ->addMethodCall ('addSubscriber ' , [new Reference ($ subscriber )]);
43
- }
33
+ // Factory
34
+ $ factory = $ container ->findDefinition ('csa_guzzle.client_factory ' );
35
+ $ arg = [];
36
+ foreach ($ subscribers as $ subscriber => $ options ) {
37
+ $ arg [] = new Reference ($ subscriber );
44
38
}
39
+ $ factory ->replaceArgument (1 , $ arg );
45
40
}
46
41
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Csa \Bundle \GuzzleBundle \Factory ;
13
13
14
+ use GuzzleHttp \Event \HasEmitterInterface ;
15
+ use GuzzleHttp \Event \SubscriberInterface ;
16
+
14
17
/**
15
18
* Csa Guzzle client compiler pass
16
19
*
19
22
class ClientFactory
20
23
{
21
24
private $ class ;
25
+ private $ subscribers ;
22
26
23
27
/**
24
- * @param string $class The client's class
28
+ * @param string $class The client's class
29
+ * @param SubscriberInterface[] $subscribers A list of subscribers to attach to each client
25
30
*/
26
- public function __construct ($ class )
31
+ public function __construct ($ class, array $ subscribers = [] )
27
32
{
28
33
$ this ->class = $ class ;
34
+ $ this ->subscribers = $ subscribers ;
29
35
}
30
36
31
37
public function create (array $ options = [])
32
38
{
33
- return new $ this ->class ($ options );
39
+ $ client = new $ this ->class ($ options );
40
+
41
+ if ($ client instanceof HasEmitterInterface) {
42
+ foreach ($ this ->subscribers as $ subscriber ) {
43
+ $ client ->getEmitter ()->attach ($ subscriber );
44
+ }
45
+ }
46
+
47
+ return $ client ;
34
48
}
35
49
}
Original file line number Diff line number Diff line change 13
13
14
14
<service id =" csa_guzzle.client_factory" class =" %csa_guzzle.client_factory.class%" >
15
15
<argument >%csa_guzzle.client.class%</argument >
16
+ <argument />
16
17
</service >
17
18
18
19
</services >
You can’t perform that action at this time.
0 commit comments