Просмотр исходного кода

Implement includeSystemCollections behavior

Andreas Braun 10 лет назад
Родитель
Сommit
6fcf2dbbe6
3 измененных файлов с 60 добавлено и 9 удалено
  1. 0 5
      README.md
  2. 28 4
      lib/Mongo/MongoDB.php
  3. 32 0
      tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

+ 0 - 5
README.md

@@ -81,11 +81,6 @@ return `0` as connection ID.
  - The [authenticate](https://secure.php.net/manual/en/mongodb.authenticate.php)
  method is not supported. To connect to a database with authentication, please
  supply the credentials using the connection string.
- - The `includeSystemCollections` parameter used in the [getCollectionInfo](https://php.net/manual/en/mongodb.getcollectioninfo.php]),
- [getCollectionNames](https://php.net/manual/en/mongodb.getcollectionnames.php]),
- and [listCollections](https://php.net/manual/en/mongodb.listcollections.php)
- methods is ignored. These methods do not return information about system
- collections.
 
 ## MongoCollection
 

+ 28 - 4
lib/Mongo/MongoDB.php

@@ -125,8 +125,10 @@ class MongoDB
      */
     public function getCollectionInfo(array $options = [])
     {
-        // The includeSystemCollections option is no longer supported
+        $includeSystemCollections = false;
+        // The includeSystemCollections option is no longer supported in the command
         if (isset($options['includeSystemCollections'])) {
+            $includeSystemCollections = $options['includeSystemCollections'];
             unset($options['includeSystemCollections']);
         }
 
@@ -143,7 +145,12 @@ class MongoDB
             ];
         };
 
-        return array_map($getCollectionInfo, iterator_to_array($collections));
+        $eligibleCollections = array_filter(
+            iterator_to_array($collections),
+            $this->getSystemCollectionFilterClosure($includeSystemCollections)
+        );
+
+        return array_map($getCollectionInfo, $eligibleCollections);
     }
 
     /**
@@ -155,8 +162,10 @@ class MongoDB
      */
     public function getCollectionNames(array $options = [])
     {
-        // The includeSystemCollections option is no longer supported
+        $includeSystemCollections = false;
+        // The includeSystemCollections option is no longer supported in the command
         if (isset($options['includeSystemCollections'])) {
+            $includeSystemCollections = $options['includeSystemCollections'];
             unset($options['includeSystemCollections']);
         }
 
@@ -170,7 +179,12 @@ class MongoDB
             return $collectionInfo->getName();
         };
 
-        return array_map($getCollectionName, iterator_to_array($collections));
+        $eligibleCollections = array_filter(
+            iterator_to_array($collections),
+            $this->getSystemCollectionFilterClosure($includeSystemCollections)
+        );
+
+        return array_map($getCollectionName, $eligibleCollections);
     }
 
     /**
@@ -521,4 +535,14 @@ class MongoDB
         }
 
     }
+
+    /**
+     * @param bool $includeSystemCollections
+     * @return Closure
+     */
+    private function getSystemCollectionFilterClosure($includeSystemCollections = false) {
+        return function (CollectionInfo $collectionInfo) use ($includeSystemCollections) {
+            return $includeSystemCollections || ! preg_match('#^system\.#', $collectionInfo->getName());
+        };
+    }
 }

+ 32 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php

@@ -283,6 +283,38 @@ class MongoDBTest extends TestCase
         $this->fail('The test collection was not found');
     }
 
+    public function testGetCollectionNamesDoesNotListSystemCollections()
+    {
+        // Enable profiling to ensure we have a system.profile collection
+        $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
+
+        try {
+            $document = ['foo' => 'bar'];
+            $this->getCollection()->insert($document);
+
+            $collectionNames = $this->getDatabase()->getCollectionNames();
+            $this->assertNotContains('system.profile', $collectionNames);
+        } finally {
+            $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
+        }
+    }
+
+    public function testGetCollectionNamesWithSystemCollections()
+    {
+        // Enable profiling to ensure we have a system.profile collection
+        $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_ON);
+
+        try {
+            $document = ['foo' => 'bar'];
+            $this->getCollection()->insert($document);
+
+            $collectionNames = $this->getDatabase()->getCollectionNames(['includeSystemCollections' => true]);
+            $this->assertContains('system.profile', $collectionNames);
+        } finally {
+            $this->getDatabase()->setProfilingLevel(\MongoDB::PROFILING_OFF);
+        }
+    }
+
     public function testListCollectionsExecutionTimeoutException()
     {
         $this->failMaxTimeMS();