1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Drupal\mongodb; |
6
|
|
|
|
7
|
|
|
use Drupal\Component\Render\FormattableMarkup; |
|
|
|
|
8
|
|
|
use Drupal\Core\Site\Settings; |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Helper class to construct a MongoDB Database with Drupal specific config. |
12
|
|
|
* |
13
|
|
|
* @package Drupal\mongodb |
14
|
|
|
*/ |
15
|
|
|
class DatabaseFactory { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The Client factory service. |
19
|
|
|
* |
20
|
|
|
* @var \Drupal\mongodb\ClientFactory |
21
|
|
|
*/ |
22
|
|
|
protected $clientFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The 'mongodb' database settings array. |
26
|
|
|
* |
27
|
|
|
* @var string[][] |
28
|
|
|
*/ |
29
|
|
|
protected $settings; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param \Drupal\mongodb\ClientFactory $clientFactory |
35
|
|
|
* The Client factory service. |
36
|
|
|
* @param \Drupal\Core\Site\Settings $settings |
37
|
|
|
* The settings service. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ClientFactory $clientFactory, Settings $settings) { |
40
|
|
|
$this->clientFactory = $clientFactory; |
41
|
|
|
$this->settings = $settings->get('mongodb')['databases']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return the MongoDB database matching an alias. |
46
|
|
|
* |
47
|
|
|
* @param string $dbAlias |
48
|
|
|
* The alias string, like "default". |
49
|
|
|
* |
50
|
|
|
* @return \MongoDB\Database|null |
51
|
|
|
* The selected database, or NULL if an error occurred. |
52
|
|
|
*/ |
53
|
|
|
public function get($dbAlias) { |
54
|
|
|
if (!isset($this->settings[$dbAlias])) { |
55
|
|
|
|
56
|
|
|
throw new \InvalidArgumentException((new FormattableMarkup('Nonexistent database alias: @alias', [ |
57
|
|
|
'@alias' => $dbAlias, |
58
|
|
|
]))->__toString()); |
59
|
|
|
} |
60
|
|
|
try { |
61
|
|
|
list($clientAlias, $database) = $this->settings[$dbAlias]; |
62
|
|
|
$client = $this->clientFactory->get($clientAlias); |
63
|
|
|
$result = $client->selectDatabase($database); |
64
|
|
|
} |
65
|
|
|
// Includes its descendant \MongoDb\Exception\InvalidArgumentException. |
66
|
|
|
catch (\InvalidArgumentException $e) { |
67
|
|
|
$result = NULL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return the next integer ID in a sequence. For numeric ids in collections. |
75
|
|
|
* |
76
|
|
|
* @param string $sequenceId |
77
|
|
|
* The name of the sequence, typically a collection name in the current |
78
|
|
|
* database. |
79
|
|
|
* @param int $value |
80
|
|
|
* Optional. If given, the result will be at least 1 more that this. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
* The next id. It will be greater than $value, possibly by more than 1. |
84
|
|
|
*/ |
85
|
|
|
public function nextId($sequenceId = 'sequences', $value = 0) { |
86
|
|
|
$collection = $this->get('default') |
87
|
|
|
->selectCollection('sequences'); |
88
|
|
|
$sequenceSelector = ['_id' => $sequenceId]; |
89
|
|
|
|
90
|
|
|
// Force the minimum if given. |
91
|
|
|
if ($value) { |
92
|
|
|
$selector = $sequenceSelector + [ |
93
|
|
|
'value' => ['$lt' => $value], |
94
|
|
|
]; |
95
|
|
|
$update = [ |
96
|
|
|
'$set' => ['value' => $value], |
97
|
|
|
]; |
98
|
|
|
$collection->updateOne($selector, $update); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Then increment it. |
102
|
|
|
$update = [ |
103
|
|
|
'$inc' => ['value' => 1], |
104
|
|
|
]; |
105
|
|
|
$options = [ |
106
|
|
|
'upsert' => TRUE, |
107
|
|
|
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, |
|
|
|
|
108
|
|
|
]; |
109
|
|
|
$document = $collection->findOneAndUpdate($sequenceSelector, $update, $options); |
110
|
|
|
$result = $document->value ?? 1; |
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths