Commit c76f333e authored by Jens Segers's avatar Jens Segers

Work on Laravel 5.4 compatibility

parent e3bea541
<?php namespace Jenssegers\Mongodb\Auth;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
use MongoDB\BSON\UTCDateTime;
use DateTime;
use DateTimeZone;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
use MongoDB\BSON\UTCDateTime;
class DatabaseTokenRepository extends BaseDatabaseTokenRepository
{
/**
* Build the record payload for the table.
*
* @param string $email
* @param string $token
* @return array
* @inheritdoc
*/
protected function getPayload($email, $token)
{
......@@ -20,10 +16,7 @@ class DatabaseTokenRepository extends BaseDatabaseTokenRepository
}
/**
* Determine if the token has expired.
*
* @param array $token
* @return bool
* @inheritdoc
*/
protected function tokenExpired($token)
{
......
......@@ -5,10 +5,7 @@ use Illuminate\Auth\Passwords\PasswordBrokerManager as BasePasswordBrokerManager
class PasswordBrokerManager extends BasePasswordBrokerManager
{
/**
* Create a token repository instance based on the given configuration.
*
* @param array $config
* @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
* @inheritdoc
*/
protected function createTokenRepository(array $config)
{
......
......@@ -28,9 +28,7 @@ class PasswordResetServiceProvider extends BasePasswordResetServiceProvider
}
/**
* Register the password broker instance.
*
* @return void
* @inheritdoc
*/
protected function registerPasswordBroker()
{
......
<?php namespace Jenssegers\Mongodb;
use Exception;
use MongoDB\Collection as MongoCollection;
use MongoDB\BSON\ObjectID;
use MongoDB\Collection as MongoCollection;
class Collection
{
......@@ -21,7 +21,8 @@ class Collection
protected $collection;
/**
* Constructor.
* @param Connection $connection
* @param MongoCollection $collection
*/
public function __construct(Connection $connection, MongoCollection $collection)
{
......@@ -65,7 +66,7 @@ class Collection
}
}
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
$queryString = $this->collection->getCollectionName().'.'.$method.'('.implode(',', $query).')';
$this->connection->logQuery($queryString, [], $time);
}
......
......@@ -45,9 +45,7 @@ class Connection extends \Illuminate\Database\Connection
}
/**
* Get the default post processor instance.
*
* @return Query\Processor
* @inheritdoc
*/
protected function getDefaultPostProcessor()
{
......@@ -92,9 +90,7 @@ class Connection extends \Illuminate\Database\Connection
}
/**
* Get a schema builder instance for the connection.
*
* @return Schema\Builder
* @inheritdoc
*/
public function getSchemaBuilder()
{
......@@ -139,10 +135,10 @@ class Connection extends \Illuminate\Database\Connection
}
// Check if the credentials are not already set in the options
if (!isset($options['username']) && !empty($config['username'])) {
if (! isset($options['username']) && ! empty($config['username'])) {
$options['username'] = $config['username'];
}
if (!isset($options['password']) && !empty($config['password'])) {
if (! isset($options['password']) && ! empty($config['password'])) {
$options['password'] = $config['password'];
}
......@@ -150,7 +146,7 @@ class Connection extends \Illuminate\Database\Connection
}
/**
* Disconnect from the underlying MongoDB connection.
* @inheritdoc
*/
public function disconnect()
{
......@@ -176,21 +172,18 @@ class Connection extends \Illuminate\Database\Connection
foreach ($hosts as &$host) {
// Check if we need to add a port to the host
if (strpos($host, ':') === false && ! empty($config['port'])) {
$host = $host . ':' . $config['port'];
$host = $host.':'.$config['port'];
}
}
// Check if we want to authenticate against a specific database.
$auth_database = isset($config['options']) && ! empty($config['options']['database']) ? $config['options']['database'] : null;
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
return 'mongodb://'.implode(',', $hosts).($auth_database ? '/'.$auth_database : '');
}
/**
* Get the elapsed time since a given starting point.
*
* @param int $start
* @return float
* @inheritdoc
*/
public function getElapsedTime($start)
{
......@@ -198,9 +191,7 @@ class Connection extends \Illuminate\Database\Connection
}
/**
* Get the PDO driver name.
*
* @return string
* @inheritdoc
*/
public function getDriverName()
{
......@@ -208,9 +199,7 @@ class Connection extends \Illuminate\Database\Connection
}
/**
* Get the default schema grammar instance.
*
* @return Schema\Grammar
* @inheritdoc
*/
protected function getDefaultSchemaGrammar()
{
......
......@@ -2,7 +2,6 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use MongoDB\Driver\Cursor;
use MongoDB\Model\BSONDocument;
......@@ -14,16 +13,22 @@ class Builder extends EloquentBuilder
* @var array
*/
protected $passthru = [
'toSql', 'insert', 'insertGetId', 'pluck',
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull',
'toSql',
'insert',
'insertGetId',
'pluck',
'count',
'min',
'max',
'avg',
'sum',
'exists',
'push',
'pull',
];
/**
* Update a record in the database.
*
* @param array $values
* @param array $options
* @return int
* @inheritdoc
*/
public function update(array $values, array $options = [])
{
......@@ -39,10 +44,7 @@ class Builder extends EloquentBuilder
}
/**
* Insert a new record into the database.
*
* @param array $values
* @return bool
* @inheritdoc
*/
public function insert(array $values)
{
......@@ -58,11 +60,7 @@ class Builder extends EloquentBuilder
}
/**
* Insert a new record and get the value of the primary key.
*
* @param array $values
* @param string $sequence
* @return int
* @inheritdoc
*/
public function insertGetId(array $values, $sequence = null)
{
......@@ -78,9 +76,7 @@ class Builder extends EloquentBuilder
}
/**
* Delete a record from the database.
*
* @return mixed
* @inheritdoc
*/
public function delete()
{
......@@ -96,12 +92,7 @@ class Builder extends EloquentBuilder
}
/**
* Increment a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
* @inheritdoc
*/
public function increment($column, $amount = 1, array $extra = [])
{
......@@ -126,12 +117,7 @@ class Builder extends EloquentBuilder
}
/**
* Decrement a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
* @inheritdoc
*/
public function decrement($column, $amount = 1, array $extra = [])
{
......@@ -154,14 +140,7 @@ class Builder extends EloquentBuilder
}
/**
* Add the "has" condition where clause to the query.
*
* @param \Illuminate\Database\Eloquent\Builder $hasQuery
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @param string $operator
* @param int $count
* @param string $boolean
* @return \Illuminate\Database\Eloquent\Builder
* @inheritdoc
*/
protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean)
{
......@@ -198,7 +177,7 @@ class Builder extends EloquentBuilder
// If we are comparing to 0, we need an additional $not flip.
if ($count == 0) {
$not = !$not;
$not = ! $not;
}
// All related ids.
......@@ -209,10 +188,7 @@ class Builder extends EloquentBuilder
}
/**
* Create a raw database expression.
*
* @param closure $expression
* @return mixed
* @inheritdoc
*/
public function raw($expression = null)
{
......@@ -223,15 +199,11 @@ class Builder extends EloquentBuilder
if ($results instanceof Cursor) {
$results = iterator_to_array($results, false);
return $this->model->hydrate($results);
}
// Convert Mongo BSONDocument to a single object.
} // Convert Mongo BSONDocument to a single object.
elseif ($results instanceof BSONDocument) {
$results = $results->getArrayCopy();
return $this->model->newFromBuilder((array) $results);
}
// The result is a single object.
} // The result is a single object.
elseif (is_array($results) and array_key_exists('_id', $results)) {
return $this->model->newFromBuilder((array) $results);
}
......
<?php namespace Jenssegers\Mongodb\Eloquent;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;
trait EmbedsRelations
{
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $localKey
* @param string $foreignKey
* @param string $relation
* @return \Jenssegers\Mongodb\Relations\EmbedsMany
*/
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
if (is_null($localKey)) {
$localKey = $relation;
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
}
$query = $this->newQuery();
$instance = new $related;
return new EmbedsMany($query, $this, $instance, $localKey, $foreignKey, $relation);
}
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $localKey
* @param string $foreignKey
* @param string $relation
* @return \Jenssegers\Mongodb\Relations\EmbedsOne
*/
protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
if (is_null($localKey)) {
$localKey = $relation;
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
}
$query = $this->newQuery();
$instance = new $related;
return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation);
}
}
......@@ -147,7 +147,7 @@ trait HybridRelations
// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey)) {
$foreignKey = Str::snake($relation) . '_id';
$foreignKey = Str::snake($relation).'_id';
}
$instance = new $related;
......@@ -233,11 +233,11 @@ trait HybridRelations
// First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query
// instances as well as the relationship instances we need for this.
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
$foreignKey = $foreignKey ?: $this->getForeignKey().'s';
$instance = new $related;
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
$otherKey = $otherKey ?: $instance->getForeignKey().'s';
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
......
......@@ -5,14 +5,12 @@ use DateTime;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
abstract class Model extends BaseModel
{
use HybridRelations;
use HybridRelations, EmbedsRelations;
/**
* The collection associated with the model.
......@@ -58,9 +56,7 @@ abstract class Model extends BaseModel
}
/**
* Get the table qualified key name.
*
* @return string
* @inheritdoc
*/
public function getQualifiedKeyName()
{
......@@ -68,80 +64,7 @@ abstract class Model extends BaseModel
}
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $localKey
* @param string $foreignKey
* @param string $relation
* @return \Jenssegers\Mongodb\Relations\EmbedsMany
*/
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if (is_null($relation)) {
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
if (is_null($localKey)) {
$localKey = $relation;
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
}
$query = $this->newQuery();
$instance = new $related;
return new EmbedsMany($query, $this, $instance, $localKey, $foreignKey, $relation);
}
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $localKey
* @param string $foreignKey
* @param string $relation
* @return \Jenssegers\Mongodb\Relations\EmbedsOne
*/
protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null)
{
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relationships.
if (is_null($relation)) {
list(, $caller) = debug_backtrace(false);
$relation = $caller['function'];
}
if (is_null($localKey)) {
$localKey = $relation;
}
if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
}
$query = $this->newQuery();
$instance = new $related;
return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation);
}
/**
* Convert a DateTime to a storable UTCDateTime object.
*
* @param DateTime|int $value
* @return UTCDateTime
* @inheritdoc
*/
public function fromDateTime($value)
{
......@@ -159,10 +82,7 @@ abstract class Model extends BaseModel
}
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return DateTime
* @inheritdoc
*/
protected function asDateTime($value)
{
......@@ -175,9 +95,7 @@ abstract class Model extends BaseModel
}
/**
* Get the format for database stored dates.
*
* @return string
* @inheritdoc
*/
protected function getDateFormat()
{
......@@ -185,9 +103,7 @@ abstract class Model extends BaseModel
}
/**
* Get a fresh timestamp for the model.
*
* @return UTCDateTime
* @inheritdoc
*/
public function freshTimestamp()
{
......@@ -195,9 +111,7 @@ abstract class Model extends BaseModel
}
/**
* Get the table associated with the model.
*
* @return string
* @inheritdoc
*/
public function getTable()
{
......@@ -205,10 +119,7 @@ abstract class Model extends BaseModel
}
/**
* Get an attribute from the model.
*
* @param string $key
* @return mixed
* @inheritdoc
*/
public function getAttribute($key)
{
......@@ -230,10 +141,7 @@ abstract class Model extends BaseModel
}
/**
* Get an attribute from the $attributes array.
*
* @param string $key
* @return mixed
* @inheritdoc
*/
protected function getAttributeFromArray($key)
{
......@@ -246,10 +154,7 @@ abstract class Model extends BaseModel
}
/**
* Set a given attribute on the model.
*
* @param string $key
* @param mixed $value
* @inheritdoc
*/
public function setAttribute($key, $value)
{
......@@ -258,9 +163,7 @@ abstract class Model extends BaseModel
$builder = $this->newBaseQueryBuilder();
$value = $builder->convertKey($value);
}
// Support keys in dot notation.
} // Support keys in dot notation.
elseif (str_contains($key, '.')) {
if (in_array($key, $this->getDates()) && $value) {
$value = $this->fromDateTime($value);
......@@ -275,9 +178,7 @@ abstract class Model extends BaseModel
}
/**
* Convert the model's attributes to an array.
*
* @return array
* @inheritdoc
*/
public function attributesToArray()
{
......@@ -304,9 +205,7 @@ abstract class Model extends BaseModel
}
/**
* Get the casts array.
*
* @return array
* @inheritdoc
*/
public function getCasts()
{
......@@ -314,10 +213,7 @@ abstract class Model extends BaseModel
}
/**
* Determine if the new and old values for a given key are numerically equivalent.
*
* @param string $key
* @return bool
* @inheritdoc
*/
protected function originalIsNumericallyEquivalent($key)
{
......@@ -357,9 +253,7 @@ abstract class Model extends BaseModel
}
/**
* Append one or more values to an array.
*
* @return mixed
* @inheritdoc
*/
public function push()
{
......@@ -477,10 +371,7 @@ abstract class Model extends BaseModel
}
/**
* Create a new Eloquent query builder for the model.
*
* @param \Jenssegers\Mongodb\Query\Builder $query
* @return \Jenssegers\Mongodb\Eloquent\Builder|static
* @inheritdoc
*/
public function newEloquentBuilder($query)
{
......@@ -488,9 +379,7 @@ abstract class Model extends BaseModel
}
/**
* Get a new query builder instance for the connection.
*
* @return Builder
* @inheritdoc
*/
protected function newBaseQueryBuilder()
{
......@@ -500,10 +389,7 @@ abstract class Model extends BaseModel
}
/**
* We just return original key here in order to support keys in dot-notation
*
* @param string $key
* @return string
* @inheritdoc
*/
protected function removeTableFromKey($key)
{
......@@ -511,11 +397,7 @@ abstract class Model extends BaseModel
}
/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
* @inheritdoc
*/
public function __call($method, $parameters)
{
......
......@@ -5,9 +5,7 @@ trait SoftDeletes
use \Illuminate\Database\Eloquent\SoftDeletes;
/**
* Get the fully qualified "deleted at" column.
*
* @return string
* @inheritdoc
*/
public function getQualifiedDeletedAtColumn()
{
......
......@@ -6,9 +6,7 @@ use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
class MongodbQueueServiceProvider extends QueueServiceProvider
{
/**
* Register the failed job services.
*
* @return void
* @inheritdoc
*/
protected function registerFailedJobServices()
{
......
This diff is collapsed.
<?php namespace Jenssegers\Mongodb\Queue;
use Illuminate\Queue\Jobs\DatabaseJob;
class MongoJob extends DatabaseJob
{
/**
* Indicates if the job has been reserved.
*
* @return bool
*/
public function isReserved()
{
return $this->job->reserved;
}
/**
* @return \DateTime
*/
public function reservedAt()
{
return $this->job->reserved_at;
}
}
......@@ -8,22 +8,19 @@ use MongoDB\Operation\FindOneAndUpdate;
class MongoQueue extends DatabaseQueue
{
/**
* Pop the next job off of the queue.
*
* @param string $queue
* @return \Illuminate\Contracts\Queue\Job|null
* @inheritdoc
*/
public function pop($queue = null)
{
$queue = $this->getQueue($queue);
if (!is_null($this->expire)) {
if (! is_null($this->retryAfter)) {
$this->releaseJobsThatHaveBeenReservedTooLong($queue);
}
if ($job = $this->getNextAvailableJobAndReserve($queue)) {
return new DatabaseJob(
$this->container, $this, $job, $queue
return new MongoJob(
$this->container, $this, $job, $this->connectionName, $queue
);
}
}
......@@ -49,13 +46,12 @@ class MongoQueue extends DatabaseQueue
[
'queue' => $this->getQueue($queue),
'reserved' => 0,
'available_at' => ['$lte' => $this->getTime()],
'available_at' => ['$lte' => Carbon::now()->getTimestamp()],
],
[
'$set' => [
'reserved' => 1,
'reserved_at' => $this->getTime(),
'reserved_at' => Carbon::now()->getTimestamp(),
],
],
[
......@@ -79,7 +75,7 @@ class MongoQueue extends DatabaseQueue
*/
protected function releaseJobsThatHaveBeenReservedTooLong($queue)
{
$expiration = Carbon::now()->subSeconds($this->expire)->getTimestamp();
$expiration = Carbon::now()->subSeconds($this->retryAfter)->getTimestamp();
$now = time();
$reserved = $this->database->collection($this->table)
......@@ -118,11 +114,7 @@ class MongoQueue extends DatabaseQueue
}
/**
* Delete a reserved job from the queue.
*
* @param string $queue
* @param string $id
* @return void
* @inheritdoc
*/
public function deleteReserved($queue, $id)
{
......
......@@ -3,7 +3,7 @@
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
{
/**
* Set the base constraints on the relation query.
* @inheritdoc
*/
public function addConstraints()
{
......@@ -16,9 +16,7 @@ class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @inheritdoc
*/
public function addEagerConstraints(array $models)
{
......
......@@ -7,9 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMan
class BelongsToMany extends EloquentBelongsToMany
{
/**
* Hydrate the pivot table relationship on the models.
*
* @param array $models
* @inheritdoc
*/
protected function hydratePivotRelation(array $models)
{
......@@ -20,7 +18,7 @@ class BelongsToMany extends EloquentBelongsToMany
* Set the select clause for the relation query.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
* @return array
*/
protected function getSelectColumns(array $columns = ['*'])
{
......@@ -28,7 +26,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Set the base constraints on the relation query.
* @inheritdoc
*/
public function addConstraints()
{
......@@ -52,12 +50,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @inheritdoc
*/
public function save(Model $model, array $joining = [], $touch = true)
{
......@@ -69,12 +62,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Create a new instance of the related model.
*
* @param array $attributes
* @param array $joining
* @param bool $touch
* @return \Illuminate\Database\Eloquent\Model
* @inheritdoc
*/
public function create(array $attributes, array $joining = [], $touch = true)
{
......@@ -91,16 +79,14 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Sync the intermediate tables with a list of IDs or collection of models.
*
* @param array $ids
* @param bool $detaching
* @return array
* @inheritdoc
*/
public function sync($ids, $detaching = true)
{
$changes = [
'attached' => [], 'detached' => [], 'updated' => [],
'attached' => [],
'detached' => [],
'updated' => [],
];
if ($ids instanceof Collection) {
......@@ -151,11 +137,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Update an existing pivot record on the table.
*
* @param mixed $id
* @param array $attributes
* @param bool $touch
* @inheritdoc
*/
public function updateExistingPivot($id, array $attributes, $touch = true)
{
......@@ -163,11 +145,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Attach a model to the parent.
*
* @param mixed $id
* @param array $attributes
* @param bool $touch
* @inheritdoc
*/
public function attach($id, array $attributes = [], $touch = true)
{
......@@ -200,11 +178,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Detach models from the relationship.
*
* @param int|array $ids
* @param bool $touch
* @return int
* @inheritdoc
*/
public function detach($ids = [], $touch = true)
{
......@@ -238,10 +212,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Build model dictionary keyed by the relation's foreign key.
*
* @param \Illuminate\Database\Eloquent\Collection $results
* @return array
* @inheritdoc
*/
protected function buildDictionary(Collection $results)
{
......@@ -262,9 +233,7 @@ class BelongsToMany extends EloquentBelongsToMany
}
/**
* Create a new query builder for the related model.
*
* @return \Illuminate\Database\Query\Builder
* @inheritdoc
*/
protected function newPivotQuery()
{
......
<?php namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
......@@ -8,10 +9,7 @@ use MongoDB\BSON\ObjectID;
class EmbedsMany extends EmbedsOneOrMany
{
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
* @inheritdoc
*/
public function initRelation(array $models, $relation)
{
......@@ -23,9 +21,7 @@ class EmbedsMany extends EmbedsOneOrMany
}
/**
* Get the results of the relationship.
*
* @return \Illuminate\Database\Eloquent\Collection
* @inheritdoc
*/
public function getResults()
{
......@@ -35,8 +31,8 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model|bool
*/
public function performInsert(Model $model)
{
......@@ -48,8 +44,7 @@ class EmbedsMany extends EmbedsOneOrMany
// For deeply nested documents, let the parent handle the changes.
if ($this->isNested()) {
$this->associate($model);
return $this->parent->save();
return $this->parent->save() ? $model : false;
}
// Push the new model to the database.
......@@ -66,7 +61,7 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Save an existing model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param Model $model
* @return Model|bool
*/
public function performUpdate(Model $model)
......@@ -82,10 +77,10 @@ class EmbedsMany extends EmbedsOneOrMany
$foreignKey = $this->getForeignKeyValue($model);
// Use array dot notation for better update behavior.
$values = array_dot($model->getDirty(), $this->localKey . '.$.');
$values = array_dot($model->getDirty(), $this->localKey.'.$.');
// Update document in database.
$result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey)
$result = $this->getBaseQuery()->where($this->localKey.'.'.$model->getKeyName(), $foreignKey)
->update($values);
// Attach the model to its parent.
......@@ -126,8 +121,8 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Associate the model instance to the given parent, without saving it to the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model
*/
public function associate(Model $model)
{
......@@ -223,8 +218,8 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Save alias.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model
*/
public function attach(Model $model)
{
......@@ -234,8 +229,8 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Associate a new model instance to the given parent, without saving it to the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model
*/
protected function associateNew($model)
{
......@@ -255,8 +250,8 @@ class EmbedsMany extends EmbedsOneOrMany
/**
* Associate an existing model instance to the given parent, without saving it to the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model
*/
protected function associateExisting($model)
{
......@@ -282,7 +277,7 @@ class EmbedsMany extends EmbedsOneOrMany
* Get a paginator for the "select" statement.
*
* @param int $perPage
* @return \Illuminate\Pagination\Paginator
* @return \Illuminate\Pagination\AbstractPaginator
*/
public function paginate($perPage = null)
{
......@@ -302,9 +297,7 @@ class EmbedsMany extends EmbedsOneOrMany
}
/**
* Get the embedded records array.
*
* @return array
* @inheritdoc
*/
protected function getEmbedded()
{
......@@ -312,9 +305,7 @@ class EmbedsMany extends EmbedsOneOrMany
}
/**
* Set the embedded records array.
*
* @param array $models
* @inheritdoc
*/
protected function setEmbedded($models)
{
......@@ -326,15 +317,11 @@ class EmbedsMany extends EmbedsOneOrMany
}
/**
* Handle dynamic method calls to the relationship.
*
* @param string $method
* @param array $parameters
* @return mixed
* @inheritdoc
*/
public function __call($method, $parameters)
{
if (method_exists('Illuminate\Database\Eloquent\Collection', $method)) {
if (method_exists(Collection::class, $method)) {
return call_user_func_array([$this->getResults(), $method], $parameters);
}
......
......@@ -6,10 +6,7 @@ use MongoDB\BSON\ObjectID;
class EmbedsOne extends EmbedsOneOrMany
{
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
* @inheritdoc
*/
public function initRelation(array $models, $relation)
{
......@@ -21,9 +18,7 @@ class EmbedsOne extends EmbedsOneOrMany
}
/**
* Get the results of the relationship.
*
* @return \Illuminate\Database\Eloquent\Model
* @inheritdoc
*/
public function getResults()
{
......@@ -33,8 +28,8 @@ class EmbedsOne extends EmbedsOneOrMany
/**
* Save a new model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model|bool
*/
public function performInsert(Model $model)
{
......@@ -46,8 +41,7 @@ class EmbedsOne extends EmbedsOneOrMany
// For deeply nested documents, let the parent handle the changes.
if ($this->isNested()) {
$this->associate($model);
return $this->parent->save();
return $this->parent->save() ? $model : false;
}
$result = $this->getBaseQuery()->update([$this->localKey => $model->getAttributes()]);
......@@ -63,8 +57,8 @@ class EmbedsOne extends EmbedsOneOrMany
/**
* Save an existing model and attach it to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model|bool
* @param Model $model
* @return Model|bool
*/
public function performUpdate(Model $model)
{
......@@ -75,7 +69,7 @@ class EmbedsOne extends EmbedsOneOrMany
}
// Use array dot notation for better update behavior.
$values = array_dot($model->getDirty(), $this->localKey . '.');
$values = array_dot($model->getDirty(), $this->localKey.'.');
$result = $this->getBaseQuery()->update($values);
......@@ -90,15 +84,13 @@ class EmbedsOne extends EmbedsOneOrMany
/**
* Delete an existing model and detach it from the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return int
*/
public function performDelete(Model $model)
public function performDelete()
{
// For deeply nested documents, let the parent handle the changes.
if ($this->isNested()) {
$this->dissociate($model);
$this->dissociate();
return $this->parent->save();
}
......@@ -116,8 +108,8 @@ class EmbedsOne extends EmbedsOneOrMany
/**
* Attach the model to its parent.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model
*/
public function associate(Model $model)
{
......@@ -127,7 +119,7 @@ class EmbedsOne extends EmbedsOneOrMany
/**
* Detach the model from its parent.
*
* @return \Illuminate\Database\Eloquent\Model
* @return Model
*/
public function dissociate()
{
......@@ -141,8 +133,6 @@ class EmbedsOne extends EmbedsOneOrMany
*/
public function delete()
{
$model = $this->getResults();
return $this->performDelete($model);
return $this->performDelete();
}
}
......@@ -2,8 +2,8 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Eloquent\Model;
abstract class EmbedsOneOrMany extends Relation
{
......@@ -31,9 +31,9 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Create a new embeds many relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param \Illuminate\Database\Eloquent\Model $related
* @param Builder $query
* @param Model $parent
* @param Model $related
* @param string $localKey
* @param string $foreignKey
* @param string $relation
......@@ -56,7 +56,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Set the base constraints on the relation query.
* @inheritdoc
*/
public function addConstraints()
{
......@@ -66,9 +66,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @inheritdoc
*/
public function addEagerConstraints(array $models)
{
......@@ -76,12 +74,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
* @inheritdoc
*/
public function match(array $models, Collection $results, $relation)
{
......@@ -99,7 +92,7 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Shorthand to get the results of the relationship.
*
* @return \Illuminate\Database\Eloquent\Collection
* @return Collection
*/
public function get()
{
......@@ -119,8 +112,8 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Attach a model instance to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
* @param Model $model
* @return Model|bool
*/
public function save(Model $model)
{
......@@ -132,8 +125,8 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Attach a collection of models to the parent instance.
*
* @param \Illuminate\Database\Eloquent\Collection|array $models
* @return \Illuminate\Database\Eloquent\Collection|array
* @param Collection|array $models
* @return Collection|array
*/
public function saveMany($models)
{
......@@ -148,7 +141,7 @@ abstract class EmbedsOneOrMany extends Relation
* Create a new instance of the related model.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @return Model
*/
public function create(array $attributes)
{
......@@ -207,9 +200,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Get the embedded records array.
*
* @return array
* @inheritdoc
*/
protected function getEmbedded()
{
......@@ -223,10 +214,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Set the embedded records array.
*
* @param array $records
* @return \Illuminate\Database\Eloquent\Model
* @inheritdoc
*/
protected function setEmbedded($records)
{
......@@ -261,7 +249,7 @@ abstract class EmbedsOneOrMany extends Relation
* Convert an array of records to a Collection.
*
* @param array $records
* @return \Jenssegers\Mongodb\Eloquent\Collection
* @return Collection
*/
protected function toCollection(array $records = [])
{
......@@ -282,7 +270,7 @@ abstract class EmbedsOneOrMany extends Relation
* Create a related model instanced.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
* @return Model
*/
protected function toModel($attributes = [])
{
......@@ -305,7 +293,7 @@ abstract class EmbedsOneOrMany extends Relation
/**
* Get the relation instance of the parent.
*
* @return \Illuminate\Database\Eloquent\Relations\Relation
* @return Relation
*/
protected function getParentRelation()
{
......@@ -313,9 +301,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Get the underlying query for the relation.
*
* @return \Illuminate\Database\Eloquent\Builder
* @inheritdoc
*/
public function getQuery()
{
......@@ -325,9 +311,7 @@ abstract class EmbedsOneOrMany extends Relation
}
/**
* Get the base query builder driving the Eloquent builder.
*
* @return \Illuminate\Database\Query\Builder
* @inheritdoc
*/
public function getBaseQuery()
{
......@@ -355,21 +339,19 @@ abstract class EmbedsOneOrMany extends Relation
protected function getPathHierarchy($glue = '.')
{
if ($parentRelation = $this->getParentRelation()) {
return $parentRelation->getPathHierarchy($glue) . $glue . $this->localKey;
return $parentRelation->getPathHierarchy($glue).$glue.$this->localKey;
}
return $this->localKey;
}
/**
* Get the parent's fully qualified key name.
*
* @return string
* @inheritdoc
*/
public function getQualifiedParentKeyName()
{
if ($parentRelation = $this->getParentRelation()) {
return $parentRelation->getPathHierarchy() . '.' . $this->parent->getKeyName();
return $parentRelation->getPathHierarchy().'.'.$this->parent->getKeyName();
}
return $this->parent->getKeyName();
......
......@@ -8,9 +8,9 @@ class HasMany extends EloquentHasMany
/**
* Add the constraints for a relationship count query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @return \Illuminate\Database\Eloquent\Builder
* @param Builder $query
* @param Builder $parent
* @return Builder
*/
public function getRelationCountQuery(Builder $query, Builder $parent)
{
......@@ -22,10 +22,10 @@ class HasMany extends EloquentHasMany
/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @param Builder $query
* @param Builder $parent
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
{
......
......@@ -8,9 +8,9 @@ class HasOne extends EloquentHasOne
/**
* Add the constraints for a relationship count query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @return \Illuminate\Database\Eloquent\Builder
* @param Builder $query
* @param Builder $parent
* @return Builder
*/
public function getRelationCountQuery(Builder $query, Builder $parent)
{
......@@ -22,10 +22,10 @@ class HasOne extends EloquentHasOne
/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @param Builder $query
* @param Builder $parent
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
* @return Builder
*/
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
{
......
......@@ -5,7 +5,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo;
class MorphTo extends EloquentMorphTo
{
/**
* Set the base constraints on the relation query.
* @inheritdoc
*/
public function addConstraints()
{
......@@ -18,10 +18,7 @@ class MorphTo extends EloquentMorphTo
}
/**
* Get all of the relation results for a type.
*
* @param string $type
* @return \Illuminate\Database\Eloquent\Collection
* @inheritdoc
*/
protected function getResultsByType($type)
{
......
<?php namespace Jenssegers\Mongodb\Schema;
use Closure;
use Illuminate\Database\Connection;
class Blueprint extends \Illuminate\Database\Schema\Blueprint
......@@ -27,10 +26,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
protected $columns = [];
/**
* Create a new schema blueprint.
*
* @param string $table
* @param Closure $callback
* @inheritdoc
*/
public function __construct(Connection $connection, $collection)
{
......@@ -40,13 +36,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Specify an index for the collection.
*
* @param string|array $columns
* @param array $options
* @param string $name
* @param string|null $algorithm
* @return Blueprint
* @inheritdoc
*/
public function index($columns = null, $name = null, $algorithm = null, $options = [])
{
......@@ -70,13 +60,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Specify the primary key(s) for the table.
*
* @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options
* @return \Illuminate\Support\Fluent
* @inheritdoc
*/
public function primary($columns = null, $name = null, $algorithm = null, $options = [])
{
......@@ -84,10 +68,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Indicate that the given index should be dropped.
*
* @param string|array $columns
* @return Blueprint
* @inheritdoc
*/
public function dropIndex($columns = null)
{
......@@ -99,7 +80,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
$transform = [];
foreach ($columns as $column) {
$transform[$column] = $column . '_1';
$transform[$column] = $column.'_1';
}
$columns = $transform;
......@@ -113,13 +94,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Specify a unique index for the collection.
*
* @param string|array $columns
* @param string $name
* @param string|null $algorithm
* @param array $options
* @return Blueprint
* @inheritdoc
*/
public function unique($columns = null, $name = null, $algorithm = null, $options = [])
{
......@@ -183,9 +158,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Indicate that the table needs to be created.
*
* @return bool
* @inheritdoc
*/
public function create()
{
......@@ -198,9 +171,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Indicate that the collection should be dropped.
*
* @return bool
* @inheritdoc
*/
public function drop()
{
......@@ -208,12 +179,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
}
/**
* Add a new column to the blueprint.
*
* @param string $type
* @param string $name
* @param array $parameters
* @return Blueprint
* @inheritdoc
*/
public function addColumn($type, $name, array $parameters = [])
{
......@@ -261,6 +227,8 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint
/**
* Allows the use of unsupported schema methods.
*
* @param $method
* @param $args
* @return Blueprint
*/
public function __call($method, $args)
......
......@@ -6,9 +6,7 @@ use Jenssegers\Mongodb\Connection;
class Builder extends \Illuminate\Database\Schema\Builder
{
/**
* Create a new database Schema manager.
*
* @param Connection $connection
* @inheritdoc
*/
public function __construct(Connection $connection)
{
......@@ -16,11 +14,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Determine if the given table has a given column.
*
* @param string $table
* @param string $column
* @return bool
* @inheritdoc
*/
public function hasColumn($table, $column)
{
......@@ -28,11 +22,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Determine if the given table has given columns.
*
* @param string $table
* @param array $columns
* @return bool
* @inheritdoc
*/
public function hasColumns($table, array $columns)
{
......@@ -59,10 +49,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Determine if the given collection exists.
*
* @param string $collection
* @return bool
* @inheritdoc
*/
public function hasTable($collection)
{
......@@ -86,11 +73,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Modify a collection on the schema.
*
* @param string $collection
* @param Closure $callback
* @return bool
* @inheritdoc
*/
public function table($collection, Closure $callback)
{
......@@ -98,11 +81,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Create a new collection on the schema.
*
* @param string $collection
* @param Closure $callback
* @return bool
* @inheritdoc
*/
public function create($collection, Closure $callback = null)
{
......@@ -116,10 +95,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Drop a collection from the schema.
*
* @param string $collection
* @return bool
* @inheritdoc
*/
public function drop($collection)
{
......@@ -129,10 +105,7 @@ class Builder extends \Illuminate\Database\Schema\Builder
}
/**
* Create a new Blueprint.
*
* @param string $collection
* @return Schema\Blueprint
* @inheritdoc
*/
protected function createBlueprint($collection, Closure $callback = null)
{
......
......@@ -18,9 +18,15 @@ class QueueTest extends TestCase
// Get and reserve the test job (next available)
$job = Queue::pop('test');
$this->assertInstanceOf('Illuminate\Queue\Jobs\DatabaseJob', $job);
$this->assertEquals(1, $job->getDatabaseJob()->reserved);
$this->assertEquals(json_encode(['job' => 'test', 'data' => ['action' => 'QueueJobLifeCycle']]), $job->getRawBody());
$this->assertInstanceOf(Jenssegers\Mongodb\Queue\MongoJob::class, $job);
$this->assertEquals(1, $job->isReserved());
$this->assertEquals(json_encode([
'displayName' => 'test',
'job' => 'test',
'maxTries' => null,
'timeout' => null,
'data' => ['action' => 'QueueJobLifeCycle'],
]), $job->getRawBody());
// Remove reserved job
$job->delete();
......@@ -34,12 +40,15 @@ class QueueTest extends TestCase
// Expire the test job
$expiry = \Carbon\Carbon::now()->subSeconds(Config::get('queue.connections.database.expire'))->getTimestamp();
Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->where('_id', $id)->update(['reserved' => 1, 'reserved_at' => $expiry]);
Queue::getDatabase()
->table(Config::get('queue.connections.database.table'))
->where('_id', $id)
->update(['reserved' => 1, 'reserved_at' => $expiry]);
// Expect an attempted older job in the queue
$job = Queue::pop('test');
$this->assertEquals(1, $job->getDatabaseJob()->attempts);
$this->assertGreaterThan($expiry, $job->getDatabaseJob()->reserved_at);
$this->assertEquals(1, $job->attempts());
$this->assertGreaterThan($expiry, $job->reservedAt());
$job->delete();
$this->assertEquals(0, Queue::getDatabase()->table(Config::get('queue.connections.database.table'))->count());
......
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