Skip to content

Commit

Permalink
initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Müller committed Feb 19, 2024
1 parent f6d5771 commit 44e16ae
Show file tree
Hide file tree
Showing 28 changed files with 11,638 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# IDE & System
/.idea
/*.iml
/.vscode
.phpstorm.meta.php
.phpunit.result.cache
.DS_Store
Thumbs.db

# Project
/node_modules
/vendor
/build
23 changes: 23 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$config = new PhpCsFixer\Config();

return $config
->setUsingCache(false)
//->setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'no_empty_statement' => true,
'no_unneeded_control_parentheses' => true,
'no_unneeded_braces' => true,
'no_unused_imports' => true,
'ordered_imports' => true,
'protected_to_private' => true,
])
->setFinder(PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src/',
__DIR__ . '/tests/',
])
);
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

All notable changes to `:package_name` will be documented in this file.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) :vendor_name <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# laravel-global-or-scope

[![Latest Version on Packagist](https://img.shields.io/packagist/v/lacodix/laravel-global-or-scope.svg?style=flat-square)](https://packagist.org/packages/lacodix/laravel-global-or-scope)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/lacodix/laravel-global-or-scope/test.yaml?branch=master&label=tests&style=flat-square)](https://github.com/lacodix/laravel-global-or-scope/actions?query=workflow%3Atest+branch%3Amaster)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/lacodix/laravel-global-or-scope/style.yaml?branch=master&label=code%20style&style=flat-square)](https://github.com/lacodix/laravel-global-or-scope/actions?query=workflow%3Astyle+branch%3Amaster)
[![Total Downloads](https://img.shields.io/packagist/dt/lacodix/laravel-global-or-scope.svg?style=flat-square)](https://packagist.org/packages/lacodix/laravel-global-or-scope)

This package allows you to add global scopes to models combined with an or condition.
It contains additional functionality to disable some or all or-scopes on the fly.

## Documentation

You can find the entire documentation for this package on [our documentation site](https://www.lacodix.de/docs/laravel-global-or-scope)

## Installation

```bash
composer require lacodix/laravel-global-or-scope
```

## Basic Usage

Just add the trait to your eloquent model and then you can use the addGlobalOrScopes method when booting.

```php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Lacodix\LaravelGlobalOrScope\Traits\GlobalOrScope;

class Post extends Model
{
use GlobalOrScope;

public static function booting(): void
{
static::addGlobalOrScopes([Scope1::class, Scope2::class]);
}
}

class Scope1 implements Scope
{
public function apply(Builder $builder, Model $model)
{
return $builder->whereNull('col1')->where('col2', 1);
}
}

class Scope2 implements Scope
{
public function apply(Builder $builder, Model $model)
{
return $builder->where('col3', 2);
}
}
...
Post::query()->where('user_id', 1000)->get();
```

This results in running the following SQL Query
```sql
select * from "posts" where "user_id" = 1000 and (("col1" is null and "col2" = 1) or ("col3" = 2))
```

For temporary disabling you can use
```php
Post::query()->withoutGlobalOrScopes()->where('user_id', 1000)->get();
```
what results in a simple
```sql
select * from "posts" where "user_id" = 1000
```

## Testing

```bash
composer test
```

## Contributing

Please run the following commands and solve potential problems before committing
and think about adding tests for new functionality.

```bash
composer csfixer:test
composer rector:test
composer phpstan:test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Credits

- [lacodix](https://github.com/lacodix)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
78 changes: 78 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"name": "lacodix/laravel-global-or-scope",
"description": "A Laravel package to add possibility to use global scopes with an or operation in Eloquent Models.",
"type": "laravel-package",
"keywords": [
"lacodix",
"laravel",
"global",
"scopes",
"model",
"eloquent"
],
"homepage": "https://github.com/lacodix/laravel-global-or-scope",
"license": "MIT",
"authors": [
{
"name": "Dominik Müller",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^9.0|^10.0|^11.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.13",
"illuminate/database": "^9.47|^10.0|^11.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.8",
"larastan/larastan": "^2.0.1",
"orchestra/testbench": "^8.8",
"pestphp/pest": "^2.20",
"pestphp/pest-plugin-arch": "^2.5",
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-ray": "^1.26"
},
"autoload": {
"psr-4": {
"Lacodix\\LaravelGlobalOrScope\\": "src",
"Lacodix\\LaravelGlobalOrScope\\Database\\Factories\\": "database/factories"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint",
"csfixer:test": "php-cs-fixer fix -v",
"phpstan:test": "php -d memory_limit=-1 vendor/bin/phpstan analyse --ansi",
"rector:test": "rector process --ansi",
"insights": "phpinsights analyse --ansi -v --no-interaction"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"laravel": {
"providers": [
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading

0 comments on commit 44e16ae

Please sign in to comment.