Skip to content

Commit 430a136

Browse files
committed
Command options compatibility checking functionality refactoring
1 parent fb92cb1 commit 430a136

38 files changed

+414
-671
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2016 Tequila Project
1+
Copyright (c) 2016-2017 Petro Buchyn
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": ">=5.6",
77
"ext-mongodb": ">=1.1.0",
8-
"symfony/options-resolver": ">=2.8",
8+
"tequila/options-resolver": "dev-master",
99
"tequila/mongodb-driver-wrapper": "dev-master"
1010
},
1111
"autoload": {

composer.lock

Lines changed: 57 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function listDatabases()
9797
new SimpleCommand(['listDatabases' => 1]),
9898
new ReadPreference(ReadPreference::RP_PRIMARY)
9999
);
100-
$cursor->setTypeMap(TypeMapResolver::getDefault());
100+
$cursor->setTypeMap(TypeMapResolver::resolveStatic([]));
101101
$result = $cursor->current();
102102

103103
if (!isset($result['databases']) || !is_array($result['databases'])) {

src/Command.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Tequila\MongoDB;
44

5-
use Tequila\MongoDB\OptionsResolver\Command\CompatibilityResolverInterface;
5+
use Tequila\MongoDB\OptionsResolver\Command\CompatibilityResolver;
66

77
class Command implements CommandInterface
88
{
@@ -12,7 +12,7 @@ class Command implements CommandInterface
1212
private $options;
1313

1414
/**
15-
* @var CompatibilityResolverInterface
15+
* @var CompatibilityResolver
1616
*/
1717
private $compatibilityResolver;
1818

@@ -30,20 +30,16 @@ public function __construct(array $options)
3030
public function getOptions(Server $server)
3131
{
3232
if (null !== $this->compatibilityResolver) {
33-
$options = new CommandOptions($this->options);
34-
$options->setServer($server);
35-
$this->compatibilityResolver->resolveCompatibilities($options);
36-
37-
$this->options = $options->toArray();
33+
$this->compatibilityResolver->resolveCompatibilities($server, $this->options);
3834
}
3935

4036
return $this->options;
4137
}
4238

4339
/**
44-
* @param CompatibilityResolverInterface $resolver
40+
* @param CompatibilityResolver $resolver
4541
*/
46-
public function setCompatibilityResolver(CompatibilityResolverInterface $resolver)
42+
public function setCompatibilityResolver(CompatibilityResolver $resolver)
4743
{
4844
$this->compatibilityResolver = $resolver;
4945
}

src/CommandExecutor.php

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tequila\MongoDB\Exception\InvalidArgumentException;
99
use Tequila\MongoDB\Exception\LogicException;
1010
use Tequila\MongoDB\OptionsResolver\Command\AggregateResolver;
11+
use Tequila\MongoDB\OptionsResolver\Command\CompatibilityResolver;
1112
use Tequila\MongoDB\OptionsResolver\Command\CountResolver;
1213
use Tequila\MongoDB\OptionsResolver\Command\CreateCollectionResolver;
1314
use Tequila\MongoDB\OptionsResolver\Command\CreateIndexesResolver;
@@ -17,9 +18,6 @@
1718
use Tequila\MongoDB\OptionsResolver\Command\DropIndexesResolver;
1819
use Tequila\MongoDB\OptionsResolver\Command\FindAndModifyResolver;
1920
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;
2321
use Tequila\MongoDB\OptionsResolver\OptionsResolver;
2422
use Tequila\MongoDB\OptionsResolver\TypeMapResolver;
2523

@@ -41,11 +39,6 @@ class CommandExecutor
4139
'listCollections' => ListCollectionsResolver::class,
4240
];
4341

44-
/**
45-
* @var array
46-
*/
47-
private $cache = [];
48-
4942
/**
5043
* @var ReadConcern
5144
*/
@@ -83,25 +76,43 @@ public function executeCommand(ManagerInterface $manager, $databaseName, array $
8376
}
8477

8578
$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+
8688
$options = $resolver->resolve($options);
8789

8890
if ($resolver->isDefined('readPreference') && isset($options['readPreference'])) {
8991
$readPreference = $options['readPreference'];
9092
unset($options['readPreference']);
9193
} else {
92-
$readPreference = $this->readPreference;
94+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
9395
}
9496

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));
10199

102100
/** @var CursorInterface $cursor */
103101
$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+
}
105116

106117
return $cursor;
107118
}
@@ -114,26 +125,13 @@ private function getResolver(array $command)
114125
{
115126
$commandName = key($command);
116127

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+
);
135132
}
133+
$resolverClass = self::$resolverClassesByCommandName[$commandName];
136134

137-
return $this->cache[$commandName];
135+
return OptionsResolver::get($resolverClass);
138136
}
139137
}

0 commit comments

Comments
 (0)