Skip to content

Commit

Permalink
Allows serializing global scopes.
Browse files Browse the repository at this point in the history
Fixes #3

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Oct 25, 2020
1 parent eb509f1 commit 52b54f6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
24 changes: 19 additions & 5 deletions src/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ class Eloquent
*/
public static function serialize(EloquentBuilder $builder): array
{
$model = $builder->getModel();

return [
'model' => [
'class' => \get_class($builder->getModel()),
'connection' => $builder->getModel()->getConnectionName(),
'class' => \get_class($model),
'connection' => $model->getConnectionName(),
'eager' => \collect($builder->getEagerLoads())->map(function ($callback) {
return \serialize(new SerializableClosure($callback));
})->all(),
'deletedScoped' => $builder->removedScopes(),
'globalScopes' => \collect($model->getGlobalScopes())->map(function ($callback) {
return \serialize(new SerializableClosure($callback));
})->all(),
'removedScopes' => $builder->removedScopes(),
],
'builder' => Query::serialize($builder->getQuery()),
];
Expand All @@ -30,16 +35,25 @@ public static function serialize(EloquentBuilder $builder): array
*/
public static function unserialize(array $payload): EloquentBuilder
{
$model = \tap(new $payload['model']['class'](), static function ($model) use ($payload) {
$modelName = $payload['model']['class'];

$model = \tap(new $modelName(), static function ($model) use ($payload) {
$model->setConnection($payload['model']['connection']);
});

return (new EloquentBuilder(Query::unserialize($payload['builder'])))
$builder = (new EloquentBuilder(Query::unserialize($payload['builder'])))
->setModel($model)
->withoutGlobalScopes($payload['model']['removedScopes'])
->setEagerLoads(
\collect($payload['model']['eager'])->map(function ($callback) {
return \unserialize($callback)->getClosure();
})->all()
);

\collect($payload['model']['globalScopes'])->map(function ($callback, $name) use ($builder) {
$builder->withGlobalScope($name, \unserialize($callback)->getClosure());
});

return $builder;
}
}
35 changes: 30 additions & 5 deletions tests/Feature/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Application;
use Laravie\SerializesQuery\Eloquent;
use Laravie\SerializesQuery\Tests\Models\Comment;
use Laravie\SerializesQuery\Tests\Models\Post;
use Laravie\SerializesQuery\Tests\Models\User;
use Laravie\SerializesQuery\Tests\TestCase;
Expand All @@ -23,7 +24,8 @@ public function it_can_serialize_a_basic_eloquent_builder()
'class' => User::class,
'connection' => null,
'eager' => [],
'deletedScoped' => [],
'globalScopes' => [],
'removedScopes' => [],
],
'builder' => [
'connection' => 'testing',
Expand All @@ -39,6 +41,25 @@ public function it_can_serialize_a_basic_eloquent_builder()
$this->assertSame($builder->toSql(), $unserialize->toSql());
}

/** @test */
public function it_can_serialize_a_basic_eloquent_builder_with_global_scopes()
{
$builder = Comment::query();
$serialized = Eloquent::serialize($builder);

$this->assertSame([
'connection' => 'testing',
'bindings' => $this->defaultBindings(),
'from' => 'comments',
], $serialized['builder']);
$this->assertSame(Comment::class, $serialized['model']['class']);

$unserialize = Eloquent::unserialize($serialized);

$this->assertSame('select * from "comments" where "id" < ?', $unserialize->toSql());
$this->assertSame($builder->toSql(), $unserialize->toSql());
}

/** @test */
public function it_can_serialize_a_basic_eloquent_with_eager_relations()
{
Expand Down Expand Up @@ -74,7 +95,8 @@ public function it_can_serialize_a_basic_eloquent_builder_on_custom_connection()
'class' => User::class,
'connection' => 'mysql',
'eager' => [],
'deletedScoped' => [],
'globalScopes' => [],
'removedScopes' => [],
],
'builder' => [
'connection' => 'mysql',
Expand Down Expand Up @@ -102,7 +124,8 @@ public function it_can_serialize_a_basic_eloquent_builder_with_wheres()
'class' => User::class,
'connection' => null,
'eager' => [],
'deletedScoped' => [],
'globalScopes' => [],
'removedScopes' => [],
],
'builder' => [
'connection' => 'testing',
Expand Down Expand Up @@ -135,7 +158,8 @@ public function it_can_serialize_a_basic_eloquent_builder_with_join()
'class' => Post::class,
'connection' => null,
'eager' => [],
'deletedScoped' => [],
'globalScopes' => [],
'removedScopes' => [],
],
'builder' => [
'connection' => 'testing',
Expand Down Expand Up @@ -181,7 +205,8 @@ public function it_can_serialize_a_basic_eloquent_builder_with_belongs_to_many_j
'class' => User::class,
'connection' => null,
'eager' => [],
'deletedScoped' => [],
'globalScopes' => [],
'removedScopes' => [],
],
'builder' => [
'connection' => 'testing',
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravie\SerializesQuery\Tests\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
protected $table = 'comments';

public static function booted()
{
static::addGlobalScope('id', function (Builder $builder) {
$builder->where('id', '<', 10);
});
}
}

0 comments on commit 52b54f6

Please sign in to comment.