Security update to Drupal 8.4.6
[yaffs-website] / vendor / doctrine / cache / lib / Doctrine / Common / Cache / MongoDBCache.php
index 75fe0ca113049f8b1278559f0e89f6d58c6c1d3d..238fde41c5ced7eb2b750734841b582573029fff 100644 (file)
 
 namespace Doctrine\Common\Cache;
 
-use MongoBinData;
 use MongoCollection;
-use MongoCursorException;
-use MongoDate;
+use MongoDB\Collection;
 
 /**
  * MongoDB cache provider.
@@ -55,26 +53,33 @@ class MongoDBCache extends CacheProvider
     const EXPIRATION_FIELD = 'e';
 
     /**
-     * @var MongoCollection
+     * @var CacheProvider
      */
-    private $collection;
+    private $provider;
 
     /**
      * Constructor.
      *
      * This provider will default to the write concern and read preference
-     * options set on the MongoCollection instance (or inherited from MongoDB or
+     * options set on the collection instance (or inherited from MongoDB or
      * MongoClient). Using an unacknowledged write concern (< 1) may make the
      * return values of delete() and save() unreliable. Reading from secondaries
      * may make contain() and fetch() unreliable.
      *
      * @see http://www.php.net/manual/en/mongo.readpreferences.php
      * @see http://www.php.net/manual/en/mongo.writeconcerns.php
-     * @param MongoCollection $collection
+     * @param MongoCollection|Collection $collection
      */
-    public function __construct(MongoCollection $collection)
+    public function __construct($collection)
     {
-        $this->collection = $collection;
+        if ($collection instanceof MongoCollection) {
+            @trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
+            $this->provider = new LegacyMongoDBCache($collection);
+        } elseif ($collection instanceof Collection) {
+            $this->provider = new ExtMongoDBCache($collection);
+        } else {
+            throw new \InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance');
+        }
     }
 
     /**
@@ -82,18 +87,7 @@ class MongoDBCache extends CacheProvider
      */
     protected function doFetch($id)
     {
-        $document = $this->collection->findOne(array('_id' => $id), array(self::DATA_FIELD, self::EXPIRATION_FIELD));
-
-        if ($document === null) {
-            return false;
-        }
-
-        if ($this->isExpired($document)) {
-            $this->doDelete($id);
-            return false;
-        }
-
-        return unserialize($document[self::DATA_FIELD]->bin);
+        return $this->provider->doFetch($id);
     }
 
     /**
@@ -101,18 +95,7 @@ class MongoDBCache extends CacheProvider
      */
     protected function doContains($id)
     {
-        $document = $this->collection->findOne(array('_id' => $id), array(self::EXPIRATION_FIELD));
-
-        if ($document === null) {
-            return false;
-        }
-
-        if ($this->isExpired($document)) {
-            $this->doDelete($id);
-            return false;
-        }
-
-        return true;
+        return $this->provider->doContains($id);
     }
 
     /**
@@ -120,20 +103,7 @@ class MongoDBCache extends CacheProvider
      */
     protected function doSave($id, $data, $lifeTime = 0)
     {
-        try {
-            $result = $this->collection->update(
-                array('_id' => $id),
-                array('$set' => array(
-                    self::EXPIRATION_FIELD => ($lifeTime > 0 ? new MongoDate(time() + $lifeTime) : null),
-                    self::DATA_FIELD => new MongoBinData(serialize($data), MongoBinData::BYTE_ARRAY),
-                )),
-                array('upsert' => true, 'multiple' => false)
-            );
-        } catch (MongoCursorException $e) {
-            return false;
-        }
-
-        return isset($result['ok']) ? $result['ok'] == 1 : true;
+        return $this->provider->doSave($id, $data, $lifeTime);
     }
 
     /**
@@ -141,9 +111,7 @@ class MongoDBCache extends CacheProvider
      */
     protected function doDelete($id)
     {
-        $result = $this->collection->remove(array('_id' => $id));
-
-        return isset($result['ok']) ? $result['ok'] == 1 : true;
+        return $this->provider->doDelete($id);
     }
 
     /**
@@ -151,10 +119,7 @@ class MongoDBCache extends CacheProvider
      */
     protected function doFlush()
     {
-        // Use remove() in lieu of drop() to maintain any collection indexes
-        $result = $this->collection->remove();
-
-        return isset($result['ok']) ? $result['ok'] == 1 : true;
+        return $this->provider->doFlush();
     }
 
     /**
@@ -162,36 +127,6 @@ class MongoDBCache extends CacheProvider
      */
     protected function doGetStats()
     {
-        $serverStatus = $this->collection->db->command(array(
-            'serverStatus' => 1,
-            'locks' => 0,
-            'metrics' => 0,
-            'recordStats' => 0,
-            'repl' => 0,
-        ));
-
-        $collStats = $this->collection->db->command(array('collStats' => 1));
-
-        return array(
-            Cache::STATS_HITS => null,
-            Cache::STATS_MISSES => null,
-            Cache::STATS_UPTIME => (isset($serverStatus['uptime']) ? (int) $serverStatus['uptime'] : null),
-            Cache::STATS_MEMORY_USAGE => (isset($collStats['size']) ? (int) $collStats['size'] : null),
-            Cache::STATS_MEMORY_AVAILABLE  => null,
-        );
-    }
-
-    /**
-     * Check if the document is expired.
-     *
-     * @param array $document
-     *
-     * @return bool
-     */
-    private function isExpired(array $document)
-    {
-        return isset($document[self::EXPIRATION_FIELD]) &&
-            $document[self::EXPIRATION_FIELD] instanceof MongoDate &&
-            $document[self::EXPIRATION_FIELD]->sec < time();
+        return $this->provider->doGetStats();
     }
 }