8
8
use Tequila \MongoDB \Exception \InvalidArgumentException ;
9
9
use Tequila \MongoDB \Exception \LogicException ;
10
10
use Tequila \MongoDB \OptionsResolver \Command \AggregateResolver ;
11
+ use Tequila \MongoDB \OptionsResolver \Command \CompatibilityResolver ;
11
12
use Tequila \MongoDB \OptionsResolver \Command \CountResolver ;
12
13
use Tequila \MongoDB \OptionsResolver \Command \CreateCollectionResolver ;
13
14
use Tequila \MongoDB \OptionsResolver \Command \CreateIndexesResolver ;
17
18
use Tequila \MongoDB \OptionsResolver \Command \DropIndexesResolver ;
18
19
use Tequila \MongoDB \OptionsResolver \Command \FindAndModifyResolver ;
19
20
use Tequila \MongoDB \OptionsResolver \Command \ListCollectionsResolver ;
20
- use Tequila \MongoDB \OptionsResolver \Command \ReadConcernAwareInterface ;
21
- use Tequila \MongoDB \OptionsResolver \Command \WriteConcernAwareInterface ;
22
- use Tequila \MongoDB \OptionsResolver \Command \CompatibilityResolverInterface ;
23
21
use Tequila \MongoDB \OptionsResolver \OptionsResolver ;
24
22
use Tequila \MongoDB \OptionsResolver \TypeMapResolver ;
25
23
@@ -41,11 +39,6 @@ class CommandExecutor
41
39
'listCollections ' => ListCollectionsResolver::class,
42
40
];
43
41
44
- /**
45
- * @var array
46
- */
47
- private $ cache = [];
48
-
49
42
/**
50
43
* @var ReadConcern
51
44
*/
@@ -83,25 +76,43 @@ public function executeCommand(ManagerInterface $manager, $databaseName, array $
83
76
}
84
77
85
78
$ resolver = $ this ->getResolver ($ command );
79
+
80
+ // Command should inherit readConcern, readPreference and writeConcern from Client, Database or Collection
81
+ // instance, from which it is called, due to MongoDB Driver Specifications
82
+ foreach (['readConcern ' , 'readPreference ' , 'writeConcern ' ] as $ optionToBeInherited ) {
83
+ if ($ resolver ->isDefined ($ optionToBeInherited ) && !$ resolver ->hasDefault ($ optionToBeInherited )) {
84
+ $ resolver ->setDefault ($ optionToBeInherited , $ this ->$ optionToBeInherited );
85
+ }
86
+ }
87
+
86
88
$ options = $ resolver ->resolve ($ options );
87
89
88
90
if ($ resolver ->isDefined ('readPreference ' ) && isset ($ options ['readPreference ' ])) {
89
91
$ readPreference = $ options ['readPreference ' ];
90
92
unset($ options ['readPreference ' ]);
91
93
} else {
92
- $ readPreference = $ this -> readPreference ;
94
+ $ readPreference = new ReadPreference (ReadPreference:: RP_PRIMARY ) ;
93
95
}
94
96
95
- $ commandOptions = $ command + $ options ;
96
- $ command = new Command ($ commandOptions );
97
-
98
- if ($ resolver instanceof CompatibilityResolverInterface) {
99
- $ command ->setCompatibilityResolver ($ resolver );
100
- }
97
+ $ command = new Command ($ command + $ options );
98
+ $ command ->setCompatibilityResolver (new CompatibilityResolver ($ resolver ));
101
99
102
100
/** @var CursorInterface $cursor */
103
101
$ cursor = $ manager ->executeCommand ($ databaseName , $ command , $ readPreference );
104
- $ cursor ->setTypeMap (TypeMapResolver::getDefault ());
102
+ $ cursor ->setTypeMap (TypeMapResolver::resolveStatic ([]));
103
+
104
+ // Clean OptionsResolver from default readConcern, readPreference and writeConcern.
105
+ // This allows to reuse the same resolver in other commands, that may be called from different
106
+ // Client, Database or Collection instances with different default readConcern, readPreference and writeConcern
107
+ foreach (['readConcern ' , 'readPreference ' , 'writeConcern ' ] as $ inheritedOption ) {
108
+ if (
109
+ $ resolver ->isDefined ($ inheritedOption )
110
+ && $ resolver ->hasDefault ($ inheritedOption )
111
+ && $ this ->$ inheritedOption === $ resolver ->getDefault ($ inheritedOption )
112
+ ) {
113
+ $ resolver ->removeDefault ($ inheritedOption );
114
+ }
115
+ }
105
116
106
117
return $ cursor ;
107
118
}
@@ -114,26 +125,13 @@ private function getResolver(array $command)
114
125
{
115
126
$ commandName = key ($ command );
116
127
117
- if (!isset ($ this ->cache [$ commandName ])) {
118
- if (!isset (self ::$ resolverClassesByCommandName [$ commandName ])) {
119
- throw new LogicException (
120
- sprintf ('OptionsResolver for command "%s" does not exist. ' , $ commandName )
121
- );
122
- }
123
- $ resolverClass = self ::$ resolverClassesByCommandName [$ commandName ];
124
- $ resolver = clone OptionsResolver::get ($ resolverClass );
125
-
126
- if ($ resolver instanceof ReadConcernAwareInterface) {
127
- $ resolver ->setDefaultReadConcern ($ this ->readConcern );
128
- }
129
-
130
- if ($ resolver instanceof WriteConcernAwareInterface) {
131
- $ resolver ->setDefaultWriteConcern ($ this ->writeConcern );
132
- }
133
-
134
- $ this ->cache [$ commandName ] = $ resolver ;
128
+ if (!isset (self ::$ resolverClassesByCommandName [$ commandName ])) {
129
+ throw new LogicException (
130
+ sprintf ('OptionsResolver for command "%s" does not exist. ' , $ commandName )
131
+ );
135
132
}
133
+ $ resolverClass = self ::$ resolverClassesByCommandName [$ commandName ];
136
134
137
- return $ this -> cache [ $ commandName ] ;
135
+ return OptionsResolver:: get ( $ resolverClass ) ;
138
136
}
139
137
}
0 commit comments