Paul Klimov
11 years ago
7 changed files with 305 additions and 0 deletions
@ -0,0 +1,90 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\mongo; |
||||||
|
|
||||||
|
use yii\base\Component; |
||||||
|
use yii\base\InvalidConfigException; |
||||||
|
use yii\db\Exception; |
||||||
|
use Yii; |
||||||
|
|
||||||
|
/** |
||||||
|
* Class Connection |
||||||
|
* |
||||||
|
* @author Paul Klimov <klimov.paul@gmail.com> |
||||||
|
* @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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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. |
@ -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. |
@ -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\\": "" } |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\extensions\mongo; |
||||||
|
|
||||||
|
|
||||||
|
class ConnectionTest extends MongoTestCase |
||||||
|
{ |
||||||
|
public function testConstruct() |
||||||
|
{ |
||||||
|
$connection = $this->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); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,91 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace yiiunit\extensions\mongo; |
||||||
|
|
||||||
|
use yii\helpers\FileHelper; |
||||||
|
use yii\mongo\Connection; |
||||||
|
use Yii; |
||||||
|
use yiiunit\TestCase; |
||||||
|
|
||||||
|
class MongoTestCase extends TestCase |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var array Mongo connection configuration. |
||||||
|
*/ |
||||||
|
protected $mongoConfig = [ |
||||||
|
'dsn' => '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; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue