Skip to content

Commit

Permalink
Merge pull request #3 from lacodix/feat/github-actions
Browse files Browse the repository at this point in the history
feat(function): added withGlobalOrScopes
  • Loading branch information
renky authored Feb 19, 2024
2 parents 84751e6 + 9788ae3 commit 2df25a6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"extra": {
"laravel": {
"providers": [
"Lacodix\\LaravelGlobalOrScope\\LaravelGlobalOrScopeServiceProvider"
]
}
},
Expand Down
27 changes: 27 additions & 0 deletions src/LaravelGlobalOrScopeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Lacodix\LaravelGlobalOrScope;

use Illuminate\Database\Eloquent\Builder;
use Lacodix\LaravelGlobalOrScope\Scopes\OrScope;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class LaravelGlobalOrScopeServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package->name('laravel-global-or-scope');
}

public function boot(): void
{
parent::boot();

Builder::macro(
'withGlobalOrScopes',
// @phpstan-ignore-next-line
fn (array $scopes = null) => $this->withGlobalScope(md5(serialize($scopes)), new OrScope($scopes))
);
}
}
18 changes: 0 additions & 18 deletions src/Scopes/OrScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,11 @@ public function apply(Builder $builder, Model $model): Builder
*/
public function extend(Builder $builder): void
{
$this->addWithGlobalOrScope($builder);
$this->addWithGlobalOrScopes($builder);
$this->addWithoutGlobalOrScope($builder);
$this->addWithoutGlobalOrScopes($builder);
$this->addRemovedOrScopes($builder);
}

/**
* @param Builder<Model> $builder
*/
protected function addWithGlobalOrScope(Builder $builder): void
{
$builder->macro('withGlobalOrScope', function (Builder $builder, $identifier, $scope) {
$this->scopes[$identifier] = $scope;

if (method_exists($scope, 'extend')) {
$scope->extend($builder);
}

return $builder;
});
}

/**
* @param Builder<Model> $builder
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Str;
use Lacodix\LaravelGlobalOrScope\LaravelGlobalOrScopeServiceProvider;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{
Expand Down Expand Up @@ -53,6 +54,19 @@ protected function setUp(): void
);
}

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
* @return array<int, class-string<\Illuminate\Support\ServiceProvider>>
*/
protected function getPackageProviders($app)
{
return [
LaravelGlobalOrScopeServiceProvider::class,
];
}

/**
* Define environment setup.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/GlobalOrScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
$this->assertEquals([0], $query->getBindings());
});

test('QueryGlobalOrScopeIsApplied', function () {
$model = new EloquentQueryGlobalOrScopesTestModel;
$query = $model->newQuery()->withGlobalOrScopes([new ActiveScope, new ConfirmedScope]);
$this->assertSame('select * from "table" where (("active" = ?) or ("confirmed" = ?))', $query->toSql());
$this->assertEquals([1,0], $query->getBindings());
});

test('ClassNameGlobalOrScopeIsApplied', function () {
$model = new EloquentClassNameGlobalOrScopesTestModel;
$query = $model->newQuery();
Expand Down Expand Up @@ -105,6 +112,17 @@
$this->assertEquals(['bar', 1, 0, 'baz', 1, 0], $query->getBindings());
});

class EloquentQueryGlobalOrScopesTestModel extends Model
{
use GlobalOrScope;

protected $table = 'table';

public static function booting(): void
{
static::clearGlobalOrScope();
}
}

class EloquentGlobalOrScopesTestModel extends Model
{
Expand Down

0 comments on commit 2df25a6

Please sign in to comment.