Kaynağa Gözat

Merge pull request #19 from alcaeus/implement-functions

Implement functions
Andreas 10 yıl önce
ebeveyn
işleme
75be8cc63c

+ 13 - 3
README.md

@@ -6,10 +6,20 @@ between applications relying on ext-mongo and the new driver (ext-mongodb).
 It provides the API of ext-mongo built on top of mongo-php-library, thus being
 compatible with PHP7.
 
+# Goal
+
+This library aims to provide a compatibility layer for applications that rely on
+on libraries using ext-mongo (e.g. [Doctrine ODM](https://github.com/doctrine/mongodb-odm))
+but want to migrate to PHP 7 or HHVM on which ext-mongo will not run.
+
+You should not be using this library if you do not rely on a library using
+ext-mongo. If you are starting a new project, please check out [mongodb/mongodb](https://github.com/mongodb/mongo-php-library).
+
 # Stability
 
-This library is not yet stable enough to be used in production. Use at your own
-risk.
+This library is still in development and not stable enough to be used in
+production. In addition to the known issues outlined below, other issues or
+fatal errors may occur. Please use at your own risk.
 
 # Installation
 
@@ -24,7 +34,7 @@ root:
 
 # Known issues
 
-## [Mongo](https://secure.php.net/manual/en/class.mongo.php)
+## Mongo
 
  - The Mongo class is deprecated and was not implemented in this library. If you
  are still using it please update your code to use the new classes.

+ 2 - 1
composer.json

@@ -22,7 +22,8 @@
         "psr-4": {
             "": "lib/Mongo",
             "Alcaeus\\MongoDbAdapter\\": "lib/Alcaeus/MongoDbAdapter"
-        }
+        },
+        "files": [ "lib/Mongo/functions.php" ]
     },
     "autoload-dev": {
         "psr-4": { "Alcaeus\\MongoDbAdapter\\Tests\\": "tests/Alcaeus/MongoDbAdapter" }

+ 23 - 31
lib/Mongo/MongoPool.php

@@ -13,53 +13,45 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-class MongoPool {
+/**
+ * @deprecated The current (1.3.0+) releases of the driver no longer implements pooling. This class and its methods are therefore deprecated and should not be used.
+ */
+class MongoPool
+{
     /**
      * Returns an array of information about all connection pools.
      *
      * @link http://php.net/manual/en/mongopool.info.php
-     * @static
-     * @return array Each connection pool has an identifier, which starts with the host. For
-     *         each pool, this function shows the following fields: $in use The number of
-     *         connections currently being used by Mongo instances. $in pool The number of
-     *         connections currently in the pool (not being used). $remaining The number of
-     *         connections that could be created by this pool. For example, suppose a pool had
-     *         5 connections remaining and 3 connections in the pool. We could create 8 new
-     *         instances of Mongo before we exhausted this pool (assuming no instances of Mongo
-     *         went out of scope, returning their connections to the pool). A negative number
-     *         means that this pool will spawn unlimited connections. Before a pool is created,
-     *         you can change the max number of connections by calling Mongo::setPoolSize. Once
-     *         a pool is showing up in the output of this function, its size cannot be changed.
-     *         $total The total number of connections allowed for this pool. This should be
-     *         greater than or equal to "in use" + "in pool" (or -1). $timeout The socket
-     *         timeout for connections in this pool. This is how long connections in this pool
-     *         will attempt to connect to a server before giving up. $waiting If you have
-     *         capped the pool size, workers requesting connections from the pool may block
-     *         until other workers return their connections. This field shows how many
-     *         milliseconds workers have blocked for connections to be released. If this number
-     *         keeps increasing, you may want to use MongoPool::setSize to add more connections
-     *         to your pool
+     * @return array
      */
-    public static function info() {}
+    public static function info()
+    {
+        trigger_error('Function MongoPool::info() is deprecated', E_DEPRECATED);
+        return [];
+    }
 
     /**
      * Sets the max number of connections new pools will be able to create.
      *
      * @link http://php.net/manual/en/mongopool.setsize.php
-     * @static
-     * @param int $size The max number of connections future pools will be able to
-     *        create. Negative numbers mean that the pool will spawn an infinite number of
-     *        connections
+     * @param int $size
      * @return boolean Returns the former value of pool size
      */
-    public static function setSize($size) {}
+    public static function setSize($size)
+    {
+        trigger_error('Function MongoPool::info() is deprecated', E_DEPRECATED);
+        return 1;
+    }
 
     /**
-     * .
+     * Get pool size for connection pools
      *
      * @link http://php.net/manual/en/mongopool.getsize.php
-     * @static
      * @return int Returns the current pool size
      */
-    public static function getSize() {}
+    public static function getSize()
+    {
+        trigger_error('Function MongoPool::info() is deprecated', E_DEPRECATED);
+        return 1;
+    }
 }

+ 38 - 0
lib/Mongo/functions.php

@@ -0,0 +1,38 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+use Alcaeus\MongoDbAdapter\TypeConverter;
+
+/**
+ * Deserializes a BSON object into a PHP array
+ *
+ * @param string $bson The BSON to be deserialized.
+ * @return array Returns the deserialized BSON object.
+ */
+function bson_decode($bson)
+{
+    return TypeConverter::toLegacy(\MongoDB\BSON\toPHP($bson));
+}
+
+/**
+ * Serializes a PHP variable into a BSON string
+ *
+ * @param mixed $anything The variable to be serialized.
+ * @return string Returns the serialized string.
+ */
+function bson_encode($anything)
+{
+    return \MongoDB\BSON\fromPHP(TypeConverter::fromLegacy($anything));
+}

+ 43 - 0
tests/Alcaeus/MongoDbAdapter/FunctionsTest.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace Alcaeus\MongoDbAdapter\Tests;
+
+/**
+ * @author alcaeus <alcaeus@alcaeus.org>
+ */
+class FunctionsTest extends TestCase
+{
+    /**
+     * @return array Returns tupels: [$encoded, $decoded]
+     */
+    public static function data()
+    {
+        // The encoded values were retrieved by encoding data with the legacy driver and encoding them base64
+        $simpleArray = ['foo' => 'bar'];
+        $simpleArrayEncoded = "EgAAAAJmb28ABAAAAGJhcgAA";
+
+        $arrayWithObjectId = ['_id' => new \MongoId('1234567890abcdef12345678')];
+        $arrayWithObjectIdEncoded = "FgAAAAdfaWQAEjRWeJCrze8SNFZ4AA==";
+
+        return [
+            'simpleArray' => [base64_decode($simpleArrayEncoded), $simpleArray],
+            'arrayWithObjectId' => [base64_decode($arrayWithObjectIdEncoded), $arrayWithObjectId],
+        ];
+    }
+
+    /**
+     * @dataProvider data
+     */
+    public function testEncode($encoded, $decoded)
+    {
+        $this->assertEquals($encoded, bson_encode($decoded));
+    }
+
+    /**
+     * @dataProvider data
+     */
+    public function testDecode($encoded, $decoded)
+    {
+        $this->assertEquals($decoded, bson_decode($encoded));
+    }
+}