Skip to content

Commit 3820d20

Browse files
authored
Merge branch '2.10.x' into proxy-var-exporter-2
2 parents 4d4c864 + d726f05 commit 3820d20

File tree

13 files changed

+96
-35
lines changed

13 files changed

+96
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"doctrine/collections": "^1.5 || ^2.0",
2828
"doctrine/event-manager": "^1.0 || ^2.0",
2929
"doctrine/instantiator": "^1.1 || ^2",
30-
"doctrine/persistence": "^3.2",
30+
"doctrine/persistence": "^3.2 || ^4",
3131
"jean85/pretty-package-versions": "^1.3.0 || ^2.0.1",
3232
"mongodb/mongodb": "^1.17.0",
3333
"psr/cache": "^1.0 || ^2.0 || ^3.0",

lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77
use Doctrine\ODM\MongoDB\DocumentManager;
88
use Doctrine\ODM\MongoDB\Iterator\CachingIterator;
99
use Doctrine\ODM\MongoDB\Iterator\HydratingIterator;
10+
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
1011
use Doctrine\ODM\MongoDB\Iterator\Iterator;
1112
use Doctrine\ODM\MongoDB\Iterator\UnrewindableIterator;
1213
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1314
use Iterator as SPLIterator;
14-
use IteratorAggregate;
1515
use MongoDB\Collection;
1616
use MongoDB\Driver\CursorInterface;
1717

1818
use function array_merge;
1919
use function assert;
2020

2121
/** @phpstan-import-type PipelineExpression from Builder */
22-
final class Aggregation implements IteratorAggregate
22+
final class Aggregation implements IterableResult
2323
{
2424
/**
2525
* @param array<string, mixed> $pipeline
@@ -30,6 +30,26 @@ public function __construct(private DocumentManager $dm, private ?ClassMetadata
3030
{
3131
}
3232

33+
public function execute(): Iterator
34+
{
35+
return $this->getIterator();
36+
}
37+
38+
/**
39+
* Execute the query and return the first result.
40+
*
41+
* @return array<string, mixed>|object|null
42+
*/
43+
public function getSingleResult(): mixed
44+
{
45+
$clone = clone $this;
46+
47+
// Limit the pipeline to a single result for efficiency
48+
$this->pipeline[] = ['$limit' => 1];
49+
50+
return $clone->getIterator()->current() ?: null;
51+
}
52+
3353
public function getIterator(): Iterator
3454
{
3555
// Force cursor to be used

lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
88
use Doctrine\ODM\MongoDB\DocumentManager;
9+
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
910
use Doctrine\ODM\MongoDB\Iterator\Iterator;
1011
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1112
use Doctrine\ODM\MongoDB\Persisters\DocumentPersister;
@@ -246,8 +247,10 @@ public function geoNear($x, $y = null): Stage\GeoNear
246247
* Returns an aggregation object for the current pipeline
247248
*
248249
* @param array<string, mixed> $options
250+
*
251+
* @return Aggregation
249252
*/
250-
public function getAggregation(array $options = []): Aggregation
253+
public function getAggregation(array $options = []): IterableResult
251254
{
252255
$class = $this->hydrationClass ? $this->dm->getClassMetadata($this->hydrationClass) : null;
253256

lib/Doctrine/ODM/MongoDB/DocumentManager.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\ODM\MongoDB\Repository\ViewRepository;
2424
use Doctrine\Persistence\Mapping\ProxyClassNameResolver;
2525
use Doctrine\Persistence\ObjectManager;
26+
use Doctrine\Persistence\ObjectRepository;
2627
use InvalidArgumentException;
2728
use Jean85\PrettyVersions;
2829
use MongoDB\Client;
@@ -224,12 +225,8 @@ public function getClient(): Client
224225
return $this->client;
225226
}
226227

227-
/**
228-
* Gets the metadata factory used to gather the metadata of classes.
229-
*
230-
* @return ClassMetadataFactoryInterface
231-
*/
232-
public function getMetadataFactory()
228+
/** Gets the metadata factory used to gather the metadata of classes. */
229+
public function getMetadataFactory(): ClassmetadataFactoryInterface
233230
{
234231
return $this->metadataFactory;
235232
}
@@ -241,16 +238,20 @@ public function getMetadataFactory()
241238
*
242239
* @param object $obj
243240
*/
244-
public function initializeObject($obj)
241+
public function initializeObject($obj): void
245242
{
246243
$this->unitOfWork->initializeObject($obj);
247244
}
248245

249246
/**
250247
* Helper method to check whether a lazy loading proxy or persistent collection has been initialized.
251248
*/
252-
public function isUninitializedObject(object $obj): bool
249+
public function isUninitializedObject(mixed $obj): bool
253250
{
251+
if (! is_object($obj)) {
252+
return false;
253+
}
254+
254255
return $this->unitOfWork->isUninitializedObject($obj);
255256
}
256257

@@ -443,7 +444,7 @@ public function createAggregationBuilder(string $documentName): Aggregation\Buil
443444
*
444445
* @throws InvalidArgumentException When the given $object param is not an object.
445446
*/
446-
public function persist($object)
447+
public function persist($object): void
447448
{
448449
if (! is_object($object)) {
449450
throw new InvalidArgumentException(gettype($object));
@@ -463,7 +464,7 @@ public function persist($object)
463464
*
464465
* @throws InvalidArgumentException When the $object param is not an object.
465466
*/
466-
public function remove($object)
467+
public function remove($object): void
467468
{
468469
if (! is_object($object)) {
469470
throw new InvalidArgumentException(gettype($object));
@@ -481,7 +482,7 @@ public function remove($object)
481482
*
482483
* @throws InvalidArgumentException When the given $object param is not an object.
483484
*/
484-
public function refresh($object)
485+
public function refresh($object): void
485486
{
486487
if (! is_object($object)) {
487488
throw new InvalidArgumentException(gettype($object));
@@ -502,7 +503,7 @@ public function refresh($object)
502503
*
503504
* @throws InvalidArgumentException When the $object param is not an object.
504505
*/
505-
public function detach($object)
506+
public function detach($object): void
506507
{
507508
if (! is_object($object)) {
508509
throw new InvalidArgumentException(gettype($object));
@@ -562,7 +563,7 @@ public function unlock(object $document): void
562563
*
563564
* @template T of object
564565
*/
565-
public function getRepository($className)
566+
public function getRepository($className): ObjectRepository
566567
{
567568
return $this->repositoryFactory->getRepository($this, $className);
568569
}
@@ -578,7 +579,7 @@ public function getRepository($className)
578579
* @throws MongoDBException
579580
* @throws Throwable From event listeners.
580581
*/
581-
public function flush(array $options = [])
582+
public function flush(array $options = []): void
582583
{
583584
$this->errorIfClosed();
584585
$this->unitOfWork->commit($options);
@@ -686,7 +687,7 @@ public function find($className, $id, $lockMode = LockMode::NONE, $lockVersion =
686687
*
687688
* @param string|null $objectName if given, only documents of this type will get detached
688689
*/
689-
public function clear($objectName = null)
690+
public function clear($objectName = null): void
690691
{
691692
if ($objectName !== null) {
692693
trigger_deprecation(
@@ -722,7 +723,7 @@ public function close()
722723
*
723724
* @throws InvalidArgumentException When the $object param is not an object.
724725
*/
725-
public function contains($object)
726+
public function contains($object): bool
726727
{
727728
if (! is_object($object)) {
728729
throw new InvalidArgumentException(gettype($object));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Iterator;
6+
7+
use BadMethodCallException;
8+
use IteratorAggregate;
9+
10+
interface IterableResult extends IteratorAggregate
11+
{
12+
/**
13+
* Executes the operation and returns a result if available; null otherwise
14+
*/
15+
public function execute(): mixed;
16+
17+
/**
18+
* Executes the operation and returns a result iterator. If the operation
19+
* did not yield an iterator, this method will throw
20+
*
21+
* @throws BadMethodCallException if the operation did not yield an iterator.
22+
*/
23+
public function getIterator(): Iterator;
24+
25+
/**
26+
* Returns the first result only from the operation
27+
*
28+
* Note that the behaviour of fetching the first result is dependent on the
29+
* implementation. A separate operation might be involved.
30+
*/
31+
public function getSingleResult(): mixed;
32+
}

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2246,7 +2246,7 @@ public function isAssociationInverseSide($assocName): bool
22462246
}
22472247

22482248
/** @param string $assocName */
2249-
public function getAssociationMappedByTargetField($assocName)
2249+
public function getAssociationMappedByTargetField($assocName): string
22502250
{
22512251
throw new BadMethodCallException(__METHOD__ . '() is not implemented yet.');
22522252
}

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
*/
4141
final class ClassMetadataFactory extends AbstractClassMetadataFactory implements ClassMetadataFactoryInterface
4242
{
43-
/** @var string */
44-
protected $cacheSalt = '$MONGODBODMCLASSMETADATA';
45-
4643
/** @var DocumentManager The DocumentManager instance */
4744
private DocumentManager $dm;
4845

@@ -55,6 +52,11 @@ final class ClassMetadataFactory extends AbstractClassMetadataFactory implements
5552
/** @var EventManager The event manager instance */
5653
private EventManager $evm;
5754

55+
public function __construct()
56+
{
57+
$this->cacheSalt = '$MONGODBODMCLASSMETADATA';
58+
}
59+
5860
public function setDocumentManager(DocumentManager $dm): void
5961
{
6062
$this->dm = $dm;
@@ -82,7 +84,7 @@ protected function initialize(): void
8284
}
8385

8486
/** @param string $className */
85-
protected function onNotFoundMetadata($className)
87+
protected function onNotFoundMetadata($className): ?ClassMetadata
8688
{
8789
if (! $this->evm->hasListeners(Events::onClassMetadataNotFound)) {
8890
return null;

lib/Doctrine/ODM/MongoDB/Mapping/Driver/AttributeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct($paths = null, ?Reader $reader = null)
6262
$this->addPaths((array) $paths);
6363
}
6464

65-
public function isTransient($className)
65+
public function isTransient($className): bool
6666
{
6767
$classAttributes = $this->getClassAttributes(new ReflectionClass($className));
6868

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENS
8181
}
8282

8383
// phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
84-
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata)
84+
public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\ClassMetadata $metadata): void
8585
{
8686
assert($metadata instanceof ClassMetadata);
8787
$xmlRoot = $this->getElement($className);

lib/Doctrine/ODM/MongoDB/Query/Builder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BadMethodCallException;
88
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
99
use Doctrine\ODM\MongoDB\DocumentManager;
10+
use Doctrine\ODM\MongoDB\Iterator\IterableResult;
1011
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1112
use GeoJson\Geometry\Geometry;
1213
use GeoJson\Geometry\Point;
@@ -659,8 +660,10 @@ public function getNewObj(): array
659660
* Gets the Query executable.
660661
*
661662
* @param array<string, mixed> $options
663+
*
664+
* @return Query
662665
*/
663-
public function getQuery(array $options = []): Query
666+
public function getQuery(array $options = []): IterableResult
664667
{
665668
$documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name);
666669

0 commit comments

Comments
 (0)