Commit 42b6cb95 authored by Jens Segers's avatar Jens Segers

Removed backup source folder

parent e80273b4
<?php namespace Jenssegers\Mongodb;
class Connection {
/**
* The MongoDB database handler.
*
* @var resource
*/
protected $db;
/**
* The MongoClient connection handler.
*
* @var resource
*/
protected $connection;
/**
* The database connection configuration options.
*
* @var string
*/
protected $config = array();
/**
* Create a new database connection instance.
*
* @param array $config
* @return void
*/
public function __construct(array $config)
{
if (!is_null($this->connection)) return;
// Store configuration
$this->config = $config;
// Check for connection options
$options = array_get($config, 'options', array());
// Create connection
$this->connection = new \MongoClient($this->getDsn($config), $options);
// Select database
$this->db = $this->connection->{$config['database']};
return $this;
}
/**
* Get a mongodb collection.
*
* @param string $name
* @return MongoDB
*/
public function getCollection($name)
{
return $this->db->{$name};
}
/**
* Get the mongodb database object.
*
* @return MongoDB
*/
public function getDb()
{
return $this->db;
}
/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the MongoClient and return them back for use.
extract($config);
$dsn = "mongodb://";
if (isset($config['username']) and isset($config['password']))
{
$dsn .= "{$username}:{$password}@";
}
$dsn.= "{$host}";
if (isset($config['port']))
{
$dsn .= ":{$port}";
}
$dsn.= "/{$database}";
return $dsn;
}
}
\ No newline at end of file
<?php namespace Jenssegers\Mongodb;
use Illuminate\Database\ConnectionResolverInterface;
use Jenssegers\Mongodb\Connection;
class ConnectionResolver implements ConnectionResolverInterface {
/**
* The application instance.
*
* @var Illuminate\Foundation\Application
*/
protected $app;
/**
* All of the registered connections.
*
* @var array
*/
protected $connections = array();
/**
* The default connection name.
*
* @var string
*/
protected $default = 'mongodb';
/**
* Create a new database manager instance.
*
* @param Illuminate\Foundation\Application $app
* @return void
*/
public function __construct($app)
{
$this->app = $app;
}
/**
* Get a database connection instance.
*
* @param string $name
* @return Connection
*/
public function connection($name = null)
{
if (is_null($name)) $name = $this->getDefaultConnection();
// If we haven't created this connection, we'll create it based on the config
// provided in the application.
if (!isset($this->connections[$name]))
{
// Get connection configuration
$connections = $this->app['config']['database']['connections'];
if (is_null($config = array_get($connections, $name)))
{
throw new \InvalidArgumentException("MongoDB [$name] not configured.");
}
// Make connection
$this->connections[$name] = $this->makeConnection($connections[$name]);
}
return $this->connections[$name];
}
/**
* Create a connection.
*
* @param array $config
*/
public function makeConnection($config)
{
return new Connection($config);
}
/**
* Get the default connection name.
*
* @return string
*/
public function getDefaultConnection()
{
return $this->default;
}
/**
* Set the default connection name.
*
* @param string $name
* @return void
*/
public function setDefaultConnection($name)
{
$this->default = $name;
}
}
\ No newline at end of file
<?php namespace Jenssegers\Mongodb;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Collection;
use Jenssegers\Mongodb\Query as QueryBuilder;
use DateTime;
use MongoDate;
abstract class Model extends \Illuminate\Database\Eloquent\Model {
/**
* The collection associated with the model.
*
* @var string
*/
protected $collection;
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = '_id';
/**
* Convert a DateTime to a storable string.
*
* @param DateTime $value
* @return MongoDate
*/
protected function fromDateTime(DateTime $value)
{
return new MongoDate($value->getTimestamp());
}
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return DateTime
*/
protected function asDateTime($value)
{
if ($value instanceof MongoDate)
{
$value = $value->sec;
}
if (is_int($value))
{
$value = "@$value";
}
return new DateTime($value);
}
/**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
if (isset($this->collection)) return $this->collection;
return parent::getTable();
}
/**
* Get a new query builder instance for the connection.
*
* @return Builder
*/
protected function newBaseQueryBuilder()
{
$connection = $this->getConnection();
return new QueryBuilder($connection);
}
}
\ No newline at end of file
<?php namespace Jenssegers\Mongodb;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\ConnectionResolver;
use Illuminate\Support\ServiceProvider;
class MongodbServiceProvider extends ServiceProvider {
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
Model::setConnectionResolver($this->app['db.mongodb']);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// The database manager is used to resolve various connections, since multiple
// connections might be managed. It also implements the connection resolver
// interface which may be used by other components requiring connections.
$this->app['db.mongodb'] = $this->app->share(function($app)
{
return new ConnectionResolver($app);
});
}
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment