Commit d151beff authored by Jens Segers's avatar Jens Segers

4 spaces

parent 7effee85
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
class Connection { class Connection {
/** /**
* The MongoDB database handler. * The MongoDB database handler.
* *
* @var resource * @var resource
...@@ -16,77 +16,77 @@ class Connection { ...@@ -16,77 +16,77 @@ class Connection {
*/ */
protected $connection; protected $connection;
/** /**
* The database connection configuration options. * The database connection configuration options.
* *
* @var string * @var string
*/ */
protected $config = array(); protected $config = array();
/** /**
* Create a new database connection instance. * Create a new database connection instance.
* *
* @param array $config * @param array $config
* @return void * @return void
*/ */
public function __construct(array $config) public function __construct(array $config)
{ {
if (!is_null($this->connection)) return; if (!is_null($this->connection)) return;
// Store configuration // Store configuration
$this->config = $config; $this->config = $config;
// Check for connection options // Check for connection options
$options = array_get($config, 'options', array()); $options = array_get($config, 'options', array());
// Create connection // Create connection
$this->connection = new \MongoClient($this->getDsn($config), $options); $this->connection = new \MongoClient($this->getDsn($config), $options);
// Select database // Select database
$this->db = $this->connection->{$config['database']}; $this->db = $this->connection->{$config['database']};
return $this; return $this;
} }
/** /**
* Get a mongodb collection. * Get a mongodb collection.
* *
* @param string $name * @param string $name
*/ */
public function getCollection($name) { public function getCollection($name) {
return $this->db->{$name}; return $this->db->{$name};
} }
/** /**
* Create a DSN string from a configuration. * Create a DSN string from a configuration.
* *
* @param array $config * @param array $config
* @return string * @return string
*/ */
protected function getDsn(array $config) protected function getDsn(array $config)
{ {
// First we will create the basic DSN setup as well as the port if it is in // 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 // in the configuration options. This will give us the basic DSN we will
// need to establish the MongoClient and return them back for use. // need to establish the MongoClient and return them back for use.
extract($config); extract($config);
$dsn = "mongodb://"; $dsn = "mongodb://";
if (isset($config['username']) and isset($config['password'])) if (isset($config['username']) and isset($config['password']))
{ {
$dsn .= "{$username}:{$password}@"; $dsn .= "{$username}:{$password}@";
} }
$dsn.= "{$host}"; $dsn.= "{$host}";
if (isset($config['port'])) if (isset($config['port']))
{ {
$dsn .= ":{$port}"; $dsn .= ":{$port}";
} }
$dsn.= "/{$database}"; $dsn.= "/{$database}";
return $dsn; return $dsn;
} }
} }
\ No newline at end of file
...@@ -5,95 +5,95 @@ use Jenssegers\Mongodb\Connection; ...@@ -5,95 +5,95 @@ use Jenssegers\Mongodb\Connection;
class ConnectionResolver implements ConnectionResolverInterface { class ConnectionResolver implements ConnectionResolverInterface {
/** /**
* The application instance. * The application instance.
* *
* @var Illuminate\Foundation\Application * @var Illuminate\Foundation\Application
*/ */
protected $app; protected $app;
/** /**
* All of the registered connections. * All of the registered connections.
* *
* @var array * @var array
*/ */
protected $connections = array(); protected $connections = array();
/** /**
* The default connection name. * The default connection name.
* *
* @var string * @var string
*/ */
protected $default = 'mongodb'; protected $default = 'mongodb';
/** /**
* Create a new database manager instance. * Create a new database manager instance.
* *
* @param Illuminate\Foundation\Application $app * @param Illuminate\Foundation\Application $app
* @return void * @return void
*/ */
public function __construct($app) public function __construct($app)
{ {
$this->app = $app; $this->app = $app;
} }
/** /**
* Get a database connection instance. * Get a database connection instance.
* *
* @param string $name * @param string $name
* @return \Illuminate\Database\Connection * @return \Illuminate\Database\Connection
*/ */
public function connection($name = null) public function connection($name = null)
{ {
if (is_null($name)) $name = $this->getDefaultConnection(); if (is_null($name)) $name = $this->getDefaultConnection();
// If we haven't created this connection, we'll create it based on the config // If we haven't created this connection, we'll create it based on the config
// provided in the application. // provided in the application.
if (!isset($this->connections[$name])) if (!isset($this->connections[$name]))
{ {
// Get connection configuration // Get connection configuration
$connections = $this->app['config']['database']['connections']; $connections = $this->app['config']['database']['connections'];
if (is_null($config = array_get($connections, $name))) if (is_null($config = array_get($connections, $name)))
{ {
throw new \InvalidArgumentException("MongoDB [$name] not configured."); throw new \InvalidArgumentException("MongoDB [$name] not configured.");
} }
// Make connection // Make connection
$this->connections[$name] = $this->makeConnection($connections[$name]); $this->connections[$name] = $this->makeConnection($connections[$name]);
} }
return $this->connections[$name]; return $this->connections[$name];
} }
/** /**
* Create a connection. * Create a connection.
* *
* @param array $config * @param array $config
*/ */
public function makeConnection($config) { public function makeConnection($config) {
return new Connection($config); return new Connection($config);
} }
/** /**
* Get the default connection name. * Get the default connection name.
* *
* @return string * @return string
*/ */
public function getDefaultConnection() public function getDefaultConnection()
{ {
return $this->default; return $this->default;
} }
/** /**
* Set the default connection name. * Set the default connection name.
* *
* @param string $name * @param string $name
* @return void * @return void
*/ */
public function setDefaultConnection($name) public function setDefaultConnection($name)
{ {
$this->default = $name; $this->default = $name;
} }
} }
\ No newline at end of file
...@@ -5,37 +5,37 @@ use Illuminate\Database\Eloquent\Collection; ...@@ -5,37 +5,37 @@ use Illuminate\Database\Eloquent\Collection;
abstract class Model extends \ArrayObject { abstract class Model extends \ArrayObject {
/** /**
* The connection name for the model. * The connection name for the model.
* *
* @var string * @var string
*/ */
protected $connection; protected $connection;
/** /**
* The collection associated with the model. * The collection associated with the model.
* *
* @var string * @var string
*/ */
protected $collection; protected $collection;
/** /**
* The primary key for the model. * The primary key for the model.
* *
* @var string * @var string
*/ */
protected $primaryKey = '_id'; protected $primaryKey = '_id';
/** /**
* The connection resolver instance. * The connection resolver instance.
* *
* @var Jenssegers\Mongodb\ConnectionResolverInterface * @var Jenssegers\Mongodb\ConnectionResolverInterface
*/ */
protected static $resolver; protected static $resolver;
/** /**
* Get properties from internal array * Get properties from internal array
* *
* @param string $name * @param string $name
...@@ -58,194 +58,194 @@ abstract class Model extends \ArrayObject { ...@@ -58,194 +58,194 @@ abstract class Model extends \ArrayObject {
} }
/** /**
* Handle dynamic method calls into the method. * Handle dynamic method calls into the method.
* *
* @param string $method * @param string $method
* @param array $parameters * @param array $parameters
* @return mixed * @return mixed
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
// Create a query // Create a query
$query = $this->newQuery(); $query = $this->newQuery();
return call_user_func_array(array($query, $method), $parameters); return call_user_func_array(array($query, $method), $parameters);
} }
/** /**
* Handle dynamic static method calls into the method. * Handle dynamic static method calls into the method.
* *
* @param string $method * @param string $method
* @param array $parameters * @param array $parameters
* @return mixed * @return mixed
*/ */
public static function __callStatic($method, $parameters) public static function __callStatic($method, $parameters)
{ {
$instance = new static; $instance = new static;
return call_user_func_array(array($instance, $method), $parameters); return call_user_func_array(array($instance, $method), $parameters);
} }
/** /**
* Get all of the models from the database. * Get all of the models from the database.
* *
* @param array $columns * @param array $columns
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public static function all($columns = array('*')) public static function all($columns = array('*'))
{ {
$instance = new static; $instance = new static;
return $instance->newQuery()->get($columns); return $instance->newQuery()->get($columns);
} }
/** /**
* Find a model by its primary key. * Find a model by its primary key.
* *
* @param mixed $id * @param mixed $id
* @param array $columns * @param array $columns
* @return \Jenssegers\Mongodb\Model|\Illuminate\Database\Eloquent\Collection * @return \Jenssegers\Mongodb\Model|\Illuminate\Database\Eloquent\Collection
*/ */
public static function find($id, $columns = array('*')) public static function find($id, $columns = array('*'))
{ {
$instance = new static; $instance = new static;
if (is_array($id)) if (is_array($id))
{ {
$id = array_map(function($value) $id = array_map(function($value)
{ {
return ($value instanceof MongoID) ? $value : new MongoID($value); return ($value instanceof MongoID) ? $value : new MongoID($value);
}, $id); }, $id);
return $instance->newQuery()->whereIn($instance->getKeyName(), $id)->get($columns); return $instance->newQuery()->whereIn($instance->getKeyName(), $id)->get($columns);
} }
return $instance->newQuery()->find($id, $columns); return $instance->newQuery()->find($id, $columns);
} }
/** /**
* Create a new instance of the given model. * Create a new instance of the given model.
* *
* @param array $attributes * @param array $attributes
* @param bool $exists * @param bool $exists
* @return \Jenssegers\Mongodb\Model * @return \Jenssegers\Mongodb\Model
*/ */
public function newInstance($attributes = array(), $exists = false) public function newInstance($attributes = array(), $exists = false)
{ {
// This method just provides a convenient way for us to generate fresh model // This method just provides a convenient way for us to generate fresh model
// instances of this current model. It is particularly useful during the // instances of this current model. It is particularly useful during the
// hydration of new objects via the Eloquent query builder instances. // hydration of new objects via the Eloquent query builder instances.
$model = new static((array) $attributes); $model = new static((array) $attributes);
$model->exists = $exists; $model->exists = $exists;
return $model; return $model;
} }
/** /**
* Get a new query for the model's table. * Get a new query for the model's table.
* *
* @return \Jenssegers\Mongodb\Query * @return \Jenssegers\Mongodb\Query
*/ */
public function newQuery() public function newQuery()
{ {
$query = new Query($this); $query = new Query($this);
return $query; return $query;
} }
/** /**
* Create a new Collection instance. * Create a new Collection instance.
* *
* @param array $models * @param array $models
* @return LMongo\Eloquent\Collection * @return LMongo\Eloquent\Collection
*/ */
public function newCollection(array $models = array()) public function newCollection(array $models = array())
{ {
return new Collection($models); return new Collection($models);
} }
/** /**
* Get the database collection for the model. * Get the database collection for the model.
* *
* @return \Jenssegers\Mongodb\Connection * @return \Jenssegers\Mongodb\Connection
*/ */
public function getCollection() public function getCollection()
{ {
return $this->collection; return $this->collection;
} }
/** /**
* Get the database connection for the model. * Get the database connection for the model.
* *
* @return \Jenssegers\Mongodb\Connection * @return \Jenssegers\Mongodb\Connection
*/ */
public function getConnection() public function getConnection()
{ {
return static::resolveConnection($this->connection); return static::resolveConnection($this->connection);
} }
/** /**
* Get the current connection name for the model. * Get the current connection name for the model.
* *
* @return string * @return string
*/ */
public function getConnectionName() public function getConnectionName()
{ {
return $this->connection; return $this->connection;
} }
/** /**
* Set the connection associated with the model. * Set the connection associated with the model.
* *
* @param string $name * @param string $name
* @return void * @return void
*/ */
public function setConnection($name) public function setConnection($name)
{ {
$this->connection = $name; $this->connection = $name;
} }
/** /**
* Get the primary key for the model. * Get the primary key for the model.
* *
* @return string * @return string
*/ */
public function getKeyName() public function getKeyName()
{ {
return $this->primaryKey; return $this->primaryKey;
} }
/** /**
* Resolve a connection instance by name. * Resolve a connection instance by name.
* *
* @param string $connection * @param string $connection
* @return \Jenssegers\Mongodb\Connection * @return \Jenssegers\Mongodb\Connection
*/ */
public static function resolveConnection($connection) public static function resolveConnection($connection)
{ {
return static::$resolver->connection($connection); return static::$resolver->connection($connection);
} }
/** /**
* Get the connection resolver instance. * Get the connection resolver instance.
* *
* @return \Jenssegers\Mongodb\ConnectionResolverInterface * @return \Jenssegers\Mongodb\ConnectionResolverInterface
*/ */
public static function getConnectionResolver() public static function getConnectionResolver()
{ {
return static::$resolver; return static::$resolver;
} }
/** /**
* Set the connection resolver instance. * Set the connection resolver instance.
* *
* @param Jenssegers\Mongodb\ConnectionResolverInterface $resolver * @param Jenssegers\Mongodb\ConnectionResolverInterface $resolver
* @return void * @return void
*/ */
public static function setConnectionResolver(Resolver $resolver) public static function setConnectionResolver(Resolver $resolver)
{ {
static::$resolver = $resolver; static::$resolver = $resolver;
} }
} }
\ No newline at end of file
...@@ -4,213 +4,213 @@ use MongoID; ...@@ -4,213 +4,213 @@ use MongoID;
class Query { class Query {
/** /**
* The model. * The model.
* *
* @var Jenssegers\Mongodb\Model * @var Jenssegers\Mongodb\Model
*/ */
protected $model; protected $model;
/** /**
* The database connection instance. * The database connection instance.
* *
* @var Jenssegers\Mongodb\Connection * @var Jenssegers\Mongodb\Connection
*/ */
protected $connection; protected $connection;
/** /**
* The database collection used for the current query. * The database collection used for the current query.
* *
* @var string $collection * @var string $collection
*/ */
protected $collection; protected $collection;
/** /**
* The where constraints for the query. * The where constraints for the query.
* *
* @var array * @var array
*/ */
public $wheres = array(); public $wheres = array();
/** /**
* The columns that should be returned. * The columns that should be returned.
* *
* @var array * @var array
*/ */
public $columns = array(); public $columns = array();
/** /**
* The orderings for the query. * The orderings for the query.
* *
* @var array * @var array
*/ */
public $orders = array(); public $orders = array();
/** /**
* The maximum number of documents to return. * The maximum number of documents to return.
* *
* @var int * @var int
*/ */
public $limit; public $limit;
/** /**
* All of the available operators. * All of the available operators.
* *
* @var array * @var array
*/ */
protected $operators = array( protected $operators = array(
'=' => '=', '=' => '=',
'!=' => '!=', '!=' => '!=',
'<' => '$lt', '<' => '$lt',
'<=' => '$le', '<=' => '$le',
'>' => '$gt', '>' => '$gt',
'>=' => '$ge' '>=' => '$ge'
); );
/** /**
* Create a new model query builder instance. * Create a new model query builder instance.
* *
* @param Jenssegers\Mongodb\Connection $connection * @param Jenssegers\Mongodb\Connection $connection
* @return void * @return void
*/ */
public function __construct(Model $model) public function __construct(Model $model)
{ {
$this->model = $model; $this->model = $model;
$this->connection = $model->getConnection(); $this->connection = $model->getConnection();
$this->collection = $this->connection->getCollection($model->getCollection()); $this->collection = $this->connection->getCollection($model->getCollection());
} }
/** /**
* Set the "limit" value of the query. * Set the "limit" value of the query.
* *
* @param int $value * @param int $value
* @return \Jenssegers\Mongodb\Query * @return \Jenssegers\Mongodb\Query
*/ */
public function take($value) public function take($value)
{ {
$this->limit = $value; $this->limit = $value;
return $this; return $this;
} }
/** /**
* Add an "order by" clause to the query. * Add an "order by" clause to the query.
* *
* @param string $column * @param string $column
* @param string $direction * @param string $direction
* @return \Illuminate\Database\Query\Builder * @return \Illuminate\Database\Query\Builder
*/ */
public function orderBy($column, $direction = 'asc') public function orderBy($column, $direction = 'asc')
{ {
$this->orders[$column] = ($direction == 'asc' ? 1 : -1); $this->orders[$column] = ($direction == 'asc' ? 1 : -1);
return $this; return $this;
} }
/** /**
* Execute a query for a single record by ID. * Execute a query for a single record by ID.
* *
* @param int $id * @param int $id
* @param array $columns * @param array $columns
* @return mixed * @return mixed
*/ */
public function find($id, $columns = array('*')) public function find($id, $columns = array('*'))
{ {
$id = new MongoID((string) $id); $id = new MongoID((string) $id);
return $this->where($this->model->getKeyName(), '=', $id)->first($columns); return $this->where($this->model->getKeyName(), '=', $id)->first($columns);
} }
/** /**
* Execute the query and get the first result. * Execute the query and get the first result.
* *
* @param array $columns * @param array $columns
* @return array * @return array
*/ */
public function first($columns = array()) public function first($columns = array())
{ {
return $this->take(1)->get($columns)->first(); return $this->take(1)->get($columns)->first();
} }
/** /**
* Add a basic where clause to the query. * Add a basic where clause to the query.
* *
* @param string $column * @param string $column
* @param string $operator * @param string $operator
* @param mixed $value * @param mixed $value
* @param string $boolean * @param string $boolean
* @return \Jenssegers\Mongodb\Query * @return \Jenssegers\Mongodb\Query
*/ */
public function where($column, $operator = null, $value = null, $boolean = 'and') public function where($column, $operator = null, $value = null, $boolean = 'and')
{ {
// If the given operator is not found in the list of valid operators we will // If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and // assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately. // we will set the operators to '=' and set the values appropriately.
if (!in_array(strtolower($operator), $this->operators, true)) if (!in_array(strtolower($operator), $this->operators, true))
{ {
list($value, $operator) = array($operator, '='); list($value, $operator) = array($operator, '=');
} }
if ($operator == '=') if ($operator == '=')
{ {
$this->wheres[$column] = $value; $this->wheres[$column] = $value;
} }
else else
{ {
$this->wheres[$column] = array($this->operators[$operator] => $value); $this->wheres[$column] = array($this->operators[$operator] => $value);
} }
return $this; return $this;
} }
/** /**
* Execute the query as a "select" statement. * Execute the query as a "select" statement.
* *
* @param array $columns * @param array $columns
* @return Collection * @return Collection
*/ */
public function get($columns = array()) public function get($columns = array())
{ {
// Merge with previous columns // Merge with previous columns
$this->columns = array_merge($this->columns, $columns); $this->columns = array_merge($this->columns, $columns);
// Drop all columns if * is present // Drop all columns if * is present
if (in_array('*', $this->columns)) $this->columns = array(); if (in_array('*', $this->columns)) $this->columns = array();
// Get Mongo cursor // Get Mongo cursor
$cursor = $this->collection->find($this->wheres, $this->columns); $cursor = $this->collection->find($this->wheres, $this->columns);
// Apply order // Apply order
if ($this->orders) if ($this->orders)
{ {
$cursor->sort($this->orders); $cursor->sort($this->orders);
} }
// Apply limit // Apply limit
if ($this->limit) if ($this->limit)
{ {
$cursor->limit($this->limit); $cursor->limit($this->limit);
} }
// Return collection of models // Return collection of models
return $this->toCollection($cursor); return $this->toCollection($cursor);
} }
/** /**
* Transform to model collection. * Transform to model collection.
* *
* @param array $columns * @param array $columns
* @return Collection * @return Collection
*/ */
public function toCollection($results) public function toCollection($results)
{ {
$models = array(); $models = array();
foreach ($results as $result) { foreach ($results as $result) {
$models[] = $this->model->newInstance($result); $models[] = $this->model->newInstance($result);
} }
return $this->model->newCollection($models); return $this->model->newCollection($models);
} }
} }
\ No newline at end of file
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