From ae0f04be99b7e12099f0ec369e97927ed69e1a6a Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Mon, 25 Nov 2013 14:05:22 +0200 Subject: [PATCH] Mongo extension created as blank. --- extensions/mongo/Connection.php | 90 +++++++++++++++++++++++++ extensions/mongo/LICENSE.md | 32 +++++++++ extensions/mongo/README.md | 40 +++++++++++ extensions/mongo/composer.json | 28 ++++++++ tests/unit/data/config.php | 5 ++ tests/unit/extensions/mongo/ConnectionTest.php | 19 ++++++ tests/unit/extensions/mongo/MongoTestCase.php | 91 ++++++++++++++++++++++++++ 7 files changed, 305 insertions(+) create mode 100644 extensions/mongo/Connection.php create mode 100644 extensions/mongo/LICENSE.md create mode 100644 extensions/mongo/README.md create mode 100644 extensions/mongo/composer.json create mode 100644 tests/unit/extensions/mongo/ConnectionTest.php create mode 100644 tests/unit/extensions/mongo/MongoTestCase.php diff --git a/extensions/mongo/Connection.php b/extensions/mongo/Connection.php new file mode 100644 index 0000000..ff8093d --- /dev/null +++ b/extensions/mongo/Connection.php @@ -0,0 +1,90 @@ + + * @since 2.0 + */ +class Connection extends Component +{ + /** + * @var \MongoClient mongo client instance. + */ + public $client; + /** + * @var array connection options. + * for example: + * ~~~ + * [ + * 'persist' => true, // use persistent connection + * 'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out + * 'journal' => true // block write operations until the journal be flushed the to disk + * ] + * ~~~ + */ + public $options = []; + /** + * @var string host:port + * + * Correct syntax is: + * mongodb://[username:password@]host1[:port1][,host2[:port2:],...] + * For example: mongodb://localhost:27017 + */ + public $dsn; + /** + * @var string name of the Mongo database to use + */ + public $dbName; + + /** + * Establishes a Mongo connection. + * It does nothing if a Mongo connection has already been established. + * @throws Exception if connection fails + */ + public function open() + { + if ($this->client === null) { + if (empty($this->dsn)) { + throw new InvalidConfigException('Connection::dsn cannot be empty.'); + } + $token = 'Opening Mongo connection: ' . $this->dsn; + try { + Yii::trace($token, __METHOD__); + Yii::beginProfile($token, __METHOD__); + $options = $this->options; + $options['connect'] = true; + $this->client = new \MongoClient($this->dsn, $options); + $this->client->selectDB($this->dbName); + Yii::endProfile($token, __METHOD__); + } catch (\Exception $e) { + Yii::endProfile($token, __METHOD__); + throw new Exception($e->getMessage(), [], (int)$e->getCode(), $e); + } + } + } + + /** + * Closes the currently active DB connection. + * It does nothing if the connection is already closed. + */ + public function close() + { + if ($this->client !== null) { + Yii::trace('Closing Mongo connection: ' . $this->dsn, __METHOD__); + $this->client = null; + } + } +} \ No newline at end of file diff --git a/extensions/mongo/LICENSE.md b/extensions/mongo/LICENSE.md new file mode 100644 index 0000000..0bb1a8d --- /dev/null +++ b/extensions/mongo/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +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. diff --git a/extensions/mongo/README.md b/extensions/mongo/README.md new file mode 100644 index 0000000..b04e1b9 --- /dev/null +++ b/extensions/mongo/README.md @@ -0,0 +1,40 @@ +Yii 2.0 Public Preview - MongoDb Extension +========================================== + +Thank you for choosing Yii - a high-performance component-based PHP framework. + +If you are looking for a production-ready PHP framework, please use +[Yii v1.1](https://github.com/yiisoft/yii). + +Yii 2.0 is still under heavy development. We may make significant changes +without prior notices. **Yii 2.0 is not ready for production use yet.** + +[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) + +This is the yii2-sphinx extension. + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run +``` +php composer.phar require yiisoft/yii2-mongo "*" +``` + +or add +``` +"yiisoft/yii2-mongo": "*" +``` +to the require section of your composer.json. + + +*Note: You might have to run `php composer.phar selfupdate`* + + +Usage & Documentation +--------------------- + +This extension adds [MongoDb](http://www.mongodb.org/) data storage support for the Yii2 framework. diff --git a/extensions/mongo/composer.json b/extensions/mongo/composer.json new file mode 100644 index 0000000..324b497 --- /dev/null +++ b/extensions/mongo/composer.json @@ -0,0 +1,28 @@ +{ + "name": "yiisoft/yii2-mongo", + "description": "MongoDb extension for the Yii framework", + "keywords": ["yii", "mongo", "mongodb", "active-record"], + "type": "yii2-extension", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Paul Klimov", + "email": "klimov.paul@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*", + "ext-mongo": "*" + }, + "autoload": { + "psr-0": { "yii\\mongo\\": "" } + } +} diff --git a/tests/unit/data/config.php b/tests/unit/data/config.php index 28e5abe..1cd036d 100644 --- a/tests/unit/data/config.php +++ b/tests/unit/data/config.php @@ -48,5 +48,10 @@ return [ 'password' => '', 'fixture' => __DIR__ . '/sphinx/source.sql', ], + ], + 'mongo' => [ + 'dsn' => 'mongodb://localhost:27017', + 'dbName' => 'yii2test', + 'options' => [], ] ]; diff --git a/tests/unit/extensions/mongo/ConnectionTest.php b/tests/unit/extensions/mongo/ConnectionTest.php new file mode 100644 index 0000000..a5b246a --- /dev/null +++ b/tests/unit/extensions/mongo/ConnectionTest.php @@ -0,0 +1,19 @@ +getConnection(false); + $params = $this->mongoConfig; + + $connection->open(); + + $this->assertEquals($params['dsn'], $connection->dsn); + //$this->assertEquals($params['username'], $connection->username); + //$this->assertEquals($params['password'], $connection->password); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/mongo/MongoTestCase.php b/tests/unit/extensions/mongo/MongoTestCase.php new file mode 100644 index 0000000..1b11bd1 --- /dev/null +++ b/tests/unit/extensions/mongo/MongoTestCase.php @@ -0,0 +1,91 @@ + 'mongodb://localhost:27017', + 'dbName' => 'yii2test', + ]; + /** + * @var Connection Mongo connection instance. + */ + protected $mongo; + + public static function setUpBeforeClass() + { + static::loadClassMap(); + } + + protected function setUp() + { + parent::setUp(); + if (!extension_loaded('mongo')) { + $this->markTestSkipped('mongo extension required.'); + } + $config = $this->getParam('mongo'); + if (!empty($config)) { + $this->mongoConfig = $config; + } + $this->mockApplication(); + static::loadClassMap(); + } + + protected function tearDown() + { + if ($this->mongo) { + $this->mongo->close(); + } + $this->destroyApplication(); + } + + /** + * Adds sphinx extension files to [[Yii::$classPath]], + * avoiding the necessity of usage Composer autoloader. + */ + protected static function loadClassMap() + { + $baseNameSpace = 'yii/mongo'; + $basePath = realpath(__DIR__. '/../../../../extensions/mongo'); + $files = FileHelper::findFiles($basePath); + foreach ($files as $file) { + $classRelativePath = str_replace($basePath, '', $file); + $classFullName = str_replace(['/', '.php'], ['\\', ''], $baseNameSpace . $classRelativePath); + Yii::$classMap[$classFullName] = $file; + } + } + + /** + * @param boolean $reset whether to clean up the test database + * @param boolean $open whether to open test database + * @return \yii\mongo\Connection + */ + public function getConnection($reset = false, $open = true) + { + if (!$reset && $this->mongo) { + return $this->mongo; + } + $db = new Connection; + $db->dsn = $this->mongoConfig['dsn']; + if (isset($this->mongoConfig['dbName'])) { + $db->dbName = $this->mongoConfig['dbName']; + } + if (isset($this->mongoConfig['options'])) { + $db->options = $this->mongoConfig['options']; + } + if ($open) { + $db->open(); + } + $this->mongo = $db; + return $db; + } +} \ No newline at end of file