From dc39f49b310cb7d12e1e8887531a719040150e54 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 18 Jul 2017 20:25:49 +0200 Subject: [PATCH 1/4] Fix BC break when converting expression objects --- lib/Doctrine/MongoDB/Aggregation/Expr.php | 26 ++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/Doctrine/MongoDB/Aggregation/Expr.php b/lib/Doctrine/MongoDB/Aggregation/Expr.php index 27de0a84..7838021f 100644 --- a/lib/Doctrine/MongoDB/Aggregation/Expr.php +++ b/lib/Doctrine/MongoDB/Aggregation/Expr.php @@ -115,7 +115,7 @@ public function addAnd($expression /*, $expression2, ... */) $this->expr['$and'] = []; } - $this->expr['$and'] = array_merge($this->expr['$and'], array_map(['static', 'convertExpression'], func_get_args())); + $this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'ensureArray'], func_get_args())); return $this; } @@ -133,7 +133,7 @@ public function addOr($expression /*, $expression2, ... */) $this->expr['$or'] = []; } - $this->expr['$or'] = array_merge($this->expr['$or'], array_map(['static', 'convertExpression'], func_get_args())); + $this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'ensureArray'], func_get_args())); return $this; } @@ -359,7 +359,13 @@ public static function convertExpression($expression) */ protected function ensureArray($expression) { - return static::convertExpression($expression); + if (is_array($expression)) { + return array_map([$this, 'ensureArray'], $expression); + } elseif ($expression instanceof self) { + return $expression->getExpression(); + } + + return $expression; } /** @@ -442,9 +448,9 @@ protected function defaultInternal($expression) $this->requiresSwitchStatement(static::class . '::default'); if ($this->currentField) { - $this->expr[$this->currentField]['$switch']['default'] = static::convertExpression($expression); + $this->expr[$this->currentField]['$switch']['default'] = $this->ensureArray($expression); } else { - $this->expr['$switch']['default'] = static::convertExpression($expression); + $this->expr['$switch']['default'] = $this->ensureArray($expression); } return $this; @@ -518,7 +524,7 @@ public function expr() public function expression($value) { $this->requiresCurrentField(__METHOD__); - $this->expr[$this->currentField] = static::convertExpression($value); + $this->expr[$this->currentField] = $this->ensureArray($value); return $this; } @@ -1121,9 +1127,9 @@ public function not($expression) private function operator($operator, $expression) { if ($this->currentField) { - $this->expr[$this->currentField][$operator] = static::convertExpression($expression); + $this->expr[$this->currentField][$operator] = $this->ensureArray($expression); } else { - $this->expr[$operator] = static::convertExpression($expression); + $this->expr[$operator] = $this->ensureArray($expression); } return $this; @@ -1619,9 +1625,9 @@ protected function thenInternal($expression) $this->switchBranch['then'] = $expression; if ($this->currentField) { - $this->expr[$this->currentField]['$switch']['branches'][] = static::convertExpression($this->switchBranch); + $this->expr[$this->currentField]['$switch']['branches'][] = $this->ensureArray($this->switchBranch); } else { - $this->expr['$switch']['branches'][] = static::convertExpression($this->switchBranch); + $this->expr['$switch']['branches'][] = $this->ensureArray($this->switchBranch); } $this->switchBranch = null; From 78e866d1b925629c90f591b0650215c95caf2028 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 2 Aug 2017 20:23:31 +0200 Subject: [PATCH 2/4] Allow overriding expression handling in aggregation stages --- .../Aggregation/Stage/AbstractBucket.php | 17 ++++++++++++++++- .../MongoDB/Aggregation/Stage/GraphLookup.php | 17 ++++++++++++++++- .../MongoDB/Aggregation/Stage/ReplaceRoot.php | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/MongoDB/Aggregation/Stage/AbstractBucket.php b/lib/Doctrine/MongoDB/Aggregation/Stage/AbstractBucket.php index 1566995e..70969de5 100644 --- a/lib/Doctrine/MongoDB/Aggregation/Stage/AbstractBucket.php +++ b/lib/Doctrine/MongoDB/Aggregation/Stage/AbstractBucket.php @@ -45,7 +45,7 @@ public function getExpression() { $stage = [ '$bucket' => [ - 'groupBy' => Expr::convertExpression($this->groupBy), + 'groupBy' => $this->convertExpression($this->groupBy), ] + $this->getExtraPipelineFields(), ]; @@ -56,6 +56,21 @@ public function getExpression() return $stage; } + /** + * Converts an expression object into an array, recursing into nested items + * + * This method is meant to be overwritten by extending classes to apply + * custom conversions (e.g. field name translation in MongoDB ODM) to the + * expression object. + * + * @param mixed|self $expression + * @return string|array + */ + protected function convertExpression($expression) + { + return Expr::convertExpression($expression); + } + /** * @return array */ diff --git a/lib/Doctrine/MongoDB/Aggregation/Stage/GraphLookup.php b/lib/Doctrine/MongoDB/Aggregation/Stage/GraphLookup.php index 772c7568..a049c729 100644 --- a/lib/Doctrine/MongoDB/Aggregation/Stage/GraphLookup.php +++ b/lib/Doctrine/MongoDB/Aggregation/Stage/GraphLookup.php @@ -229,7 +229,7 @@ public function getExpression() { $graphLookup = [ 'from' => $this->from, - 'startWith' => Expr::convertExpression($this->startWith), + 'startWith' => $this->convertExpression($this->startWith), 'connectFromField' => $this->connectFromField, 'connectToField' => $this->connectToField, 'as' => $this->as, @@ -246,4 +246,19 @@ public function getExpression() return ['$graphLookup' => $graphLookup]; } + + /** + * Converts an expression object into an array, recursing into nested items + * + * This method is meant to be overwritten by extending classes to apply + * custom conversions (e.g. field name translation in MongoDB ODM) to the + * expression object. + * + * @param mixed|self $expression + * @return string|array + */ + protected function convertExpression($expression) + { + return Expr::convertExpression($expression); + } } diff --git a/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php b/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php index 74344caf..39c55624 100644 --- a/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php +++ b/lib/Doctrine/MongoDB/Aggregation/Stage/ReplaceRoot.php @@ -53,7 +53,22 @@ public function __construct(Builder $builder, $expression = null) public function getExpression() { return [ - '$replaceRoot' => $this->expression !== null ? Expr::convertExpression($this->expression) : $this->expr->getExpression() + '$replaceRoot' => $this->expression !== null ? $this->convertExpression($this->expression) : $this->expr->getExpression() ]; } + + /** + * Converts an expression object into an array, recursing into nested items + * + * This method is meant to be overwritten by extending classes to apply + * custom conversions (e.g. field name translation in MongoDB ODM) to the + * expression object. + * + * @param mixed|self $expression + * @return string|array + */ + protected function convertExpression($expression) + { + return Expr::convertExpression($expression); + } } From 46e50291c2185a46dfb8449b0b2668c59d678dd7 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Sun, 6 Aug 2017 16:25:18 +0200 Subject: [PATCH 3/4] Update branch-alias to 1.6 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fd127643..e99951da 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.5.x-dev" + "dev-master": "1.6.x-dev" } } } From 0f7764c40f3cd0e838e79b99cfe5cfdbe22c8b3d Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Wed, 9 Aug 2017 15:49:23 +0200 Subject: [PATCH 4/4] Add changelog and upgrade docs for 1.6 --- CHANGELOG-1.6.md | 11 +++++++++++ UPGRADE-1.6.md | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 CHANGELOG-1.6.md create mode 100644 UPGRADE-1.6.md diff --git a/CHANGELOG-1.6.md b/CHANGELOG-1.6.md new file mode 100644 index 00000000..b088c327 --- /dev/null +++ b/CHANGELOG-1.6.md @@ -0,0 +1,11 @@ +CHANGELOG for 1.6.x +=================== + +This changelog references the relevant changes (bug and security fixes) done +in 1.6.x patch versions. + +1.6.0 (2017-08-09) +------------------ + + * [#298](https://github.com/doctrine/mongodb/pull/298): Fix BC break when converting expression objects + * [#301](https://github.com/doctrine/mongodb/pull/301): Test against PHP 7.2 diff --git a/UPGRADE-1.6.md b/UPGRADE-1.6.md new file mode 100644 index 00000000..9760d957 --- /dev/null +++ b/UPGRADE-1.6.md @@ -0,0 +1,21 @@ +Upgrade from 1.5 to 1.6 +======================= + +Aggregation builder +------------------- + + * Fixes a BC break when converting expression objects in the + `Doctrine\MongoDB\Aggregation\Expr` class. + * Adds extension points to customize expression conversion in the following stages: + * `$bucket` + * `$bucketAuto` + * `$graphLookup` + * `$replaceRoot` + +Version support +--------------- + * Support for MongoDB server versions below 3.0 has been dropped. These are no + longer supported by MongoDB. We recommend you upgrade to a recent version + * The 1.5 version of the legacy driver is no longer supported. This library now + requires at least version 1.6.7 of the legacy driver. + * PHP 7.2 is also supported using [mongo-php-adapter](https://github.com/alcaeus/mongo-php-adapter)