-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 517115e
Showing
30 changed files
with
1,762 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/.github export-ignore | ||
/tests export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.php-cs-fixer.php export-ignore | ||
/phpunit.xml export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
github: stayallive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: CI | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
- develop | ||
- release/** | ||
|
||
jobs: | ||
phpunit: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 15 | ||
env: | ||
COMPOSER_NO_INTERACTION: 1 | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
php: [ "8.2", "8.1" ] | ||
|
||
name: phpunit (PHP:${{ matrix.php }}) | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
coverage: none | ||
tools: composer:v2 | ||
|
||
- name: Install Composer dependencies | ||
run: composer install --no-interaction --prefer-dist --no-progress | ||
|
||
- name: Run phpunit | ||
run: composer test:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Code style | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
- develop | ||
|
||
jobs: | ||
php-cs-fixer: | ||
name: PHP-CS-Fixer | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.2' | ||
|
||
- name: Install dependencies | ||
run: composer update --no-progress --no-interaction --prefer-dist | ||
|
||
- name: Run script | ||
run: composer phpcs:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- "v*.*.*" | ||
|
||
jobs: | ||
release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Create GitHub release | ||
uses: softprops/action-gh-release@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.lock | ||
/.idea | ||
/vendor | ||
/.phpunit.cache | ||
/.php-cs-fixer.cache | ||
/.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->in(__DIR__ . '/src') | ||
->in(__DIR__ . '/tests') | ||
->name('*.php') | ||
->ignoreDotFiles(true) | ||
->ignoreVCS(true); | ||
|
||
$config = new PhpCsFixer\Config; | ||
|
||
$config | ||
->setRules([ | ||
'@Symfony' => true, | ||
|
||
'yoda_style' => false, | ||
'phpdoc_order' => true, | ||
'new_with_braces' => false, | ||
'short_scalar_cast' => true, | ||
'phpdoc_to_comment' => false, | ||
'single_line_throw' => false, | ||
'single_blank_line_at_eof' => true, | ||
'no_superfluous_phpdoc_tags' => false, | ||
'linebreak_after_opening_tag' => true, | ||
'class_attributes_separation' => false, | ||
'not_operator_with_successor_space' => false, | ||
'single_trait_insert_per_statement' => false, | ||
'nullable_type_declaration_for_default_null_value' => true, | ||
|
||
'concat_space' => [ | ||
'spacing' => 'one', | ||
], | ||
'binary_operator_spaces' => [ | ||
'operators' => [ | ||
'|' => null, | ||
'=' => 'align_single_space', | ||
'=>' => 'align_single_space', | ||
], | ||
], | ||
'array_syntax' => [ | ||
'syntax' => 'short', | ||
], | ||
'ordered_imports' => [ | ||
'sort_algorithm' => 'length', | ||
], | ||
'cast_spaces' => [ | ||
'space' => 'none', | ||
], | ||
'align_multiline_comment' => [ | ||
'comment_type' => 'phpdocs_like', | ||
], | ||
'phpdoc_align' => [ | ||
'align' => 'vertical', | ||
], | ||
'increment_style' => [ | ||
'style' => 'post', | ||
], | ||
'phpdoc_no_alias_tag' => [ | ||
'replacements' => [ | ||
'type' => 'var', | ||
'link' => 'see', | ||
], | ||
], | ||
'no_extra_blank_lines' => [ | ||
'tokens' => [], | ||
], | ||
]) | ||
->setFinder($finder); | ||
|
||
return $config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2022 Alex Bouma | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Laravel Inverse Relations | ||
|
||
[![Latest Version](https://img.shields.io/github/release/stayallive/laravel-inverse-relations.svg?style=flat-square)](https://github.com/stayallive/laravel-inverse-relations/releases) | ||
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) | ||
[![Build Status](https://img.shields.io/github/workflow/status/stayallive/laravel-inverse-relations/CI/master.svg?style=flat-square)](https://github.com/stayallive/laravel-inverse-relations/actions/workflows/ci.yaml) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/stayallive/laravel-inverse-relations.svg?style=flat-square)](https://packagist.org/packages/stayallive/laravel-inverse-relations) | ||
|
||
Inverse relations for Laravel Eloquent models. | ||
|
||
[Jonathan Reinink](https://github.com/reinink) wrote a great blog post about [optimizing circular relationships in Laravel](https://reinink.ca/articles/optimizing-circular-relationships-in-laravel). This [package](https://github.com/archtechx/laravel-hasmanywithinverse) ran with that idea and implemented it for the `hasMany` relation. This package tries to improve on that by implementing it for the `hasOne` and `morphMany` relations too. | ||
|
||
## Installation | ||
|
||
```bash | ||
composer require stayallive/laravel-inverse-relations | ||
``` | ||
|
||
## Usage | ||
|
||
Adding the `HasHasManyWithInverseRelation`, `HasHasOneWithInverseRelation` and/or `HasMorphManyWithInverseRelation` trait will provide you with the helper methods to setup the inverse relations. | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Stayallive\Laravel\Eloquent\UUID\UsesUUID; | ||
|
||
class SomeModel extends Model | ||
{ | ||
use UsesUUID; | ||
|
||
/** | ||
* This method is not needed but can be used to override which attribute is filled with the UUID. | ||
*/ | ||
public function getUUIDAttributeName(): string | ||
{ | ||
return $this->getKeyName(); | ||
} | ||
} | ||
``` | ||
|
||
## Security Vulnerabilities | ||
|
||
If you discover a security vulnerability within this package, please send an e-mail to Alex Bouma at `[email protected]`. All security vulnerabilities will be swiftly addressed. | ||
|
||
## License | ||
|
||
This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "stayallive/laravel-inverse-relations", | ||
"type": "library", | ||
"description": "HasMany, HasOne and MorphMany Eloquent relations with inverse improvements.", | ||
"keywords": [ | ||
"laravel", | ||
"eloquent", | ||
"relations" | ||
], | ||
"homepage": "https://github.com/stayallive/laravel-inverse-relations", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Alex Bouma", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.1", | ||
"laravel/framework": "^9.0|^10" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Stayallive\\Laravel\\Eloquent\\Relations\\": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^3.16", | ||
"orchestra/testbench": "^8.0", | ||
"pestphp/pest": "^2.4", | ||
"phpunit/phpunit": "^10.0" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"scripts": { | ||
"test": "vendor/bin/pest --stop-on-failure", | ||
"test:ci": "vendor/bin/pest", | ||
"phpcs": "vendor/bin/php-cs-fixer fix", | ||
"phpcs:ci": "vendor/bin/php-cs-fixer fix --dry-run --diff" | ||
}, | ||
"config": { | ||
"sort-packages": true, | ||
"preferred-install": "dist", | ||
"optimize-autoloader": true, | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" | ||
colors="true" | ||
bootstrap="tests/bootstrap.php" | ||
stopOnFailure="false" | ||
cacheDirectory=".phpunit.cache" | ||
processIsolation="false" | ||
backupStaticProperties="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="DB_CONNECTION" value="testing"/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Stayallive\Laravel\Eloquent\Relations; | ||
|
||
trait HasHasManyWithInverseRelation | ||
{ | ||
public function hasManyWithInverse($related, $inverse, $foreignKey = null, $localKey = null): HasManyWithInverseRelation | ||
{ | ||
/** @var \Illuminate\Database\Eloquent\Model $this */ | ||
$instance = $this->newRelatedInstance($related); | ||
$localKey = $localKey ?: $this->getKeyName(); | ||
$foreignKey = $foreignKey ?: $this->getForeignKey(); | ||
|
||
return new HasManyWithInverseRelation($instance->newQuery(), $this, $instance->getTable() . '.' . $foreignKey, $localKey, $inverse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Stayallive\Laravel\Eloquent\Relations; | ||
|
||
trait HasHasOneWithInverseRelation | ||
{ | ||
public function hasOneWithInverse($related, $inverse, $foreignKey = null, $localKey = null): HasOneWithInverseRelation | ||
{ | ||
/** @var \Illuminate\Database\Eloquent\Model $this */ | ||
$instance = $this->newRelatedInstance($related); | ||
$localKey = $localKey ?: $this->getKeyName(); | ||
$foreignKey = $foreignKey ?: $this->getForeignKey(); | ||
|
||
return new HasOneWithInverseRelation($instance->newQuery(), $this, $instance->getTable() . '.' . $foreignKey, $localKey, $inverse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Stayallive\Laravel\Eloquent\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model | ||
* | ||
* @extends \Illuminate\Database\Eloquent\Relations\HasMany<TRelatedModel> | ||
*/ | ||
class HasManyWithInverseRelation extends HasMany | ||
{ | ||
use WithInverseOneOrManyRelation; | ||
|
||
public function __construct(Builder $query, Model $parent, string $foreignKey, string $localKey, string $relationToParent) | ||
{ | ||
$this->relationToParent = $relationToParent; | ||
|
||
parent::__construct($query, $parent, $foreignKey, $localKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Stayallive\Laravel\Eloquent\Relations; | ||
|
||
trait HasMorphManyWithInverseRelation | ||
{ | ||
/** | ||
* Define a polymorphic one-to-many relationship with inverse. | ||
* | ||
* @param string $related | ||
* @param string $name | ||
* @param string|null $type | ||
* @param string|null $id | ||
* @param string|null $localKey | ||
* | ||
* @return \Stayallive\Laravel\Eloquent\Relations\MorphManyWithInverseRelation | ||
*/ | ||
public function morphManyWithInverse($related, $name, $type = null, $id = null, $localKey = null): MorphManyWithInverseRelation | ||
{ | ||
$instance = $this->newRelatedInstance($related); | ||
|
||
// Here we will gather up the morph type and ID for the relationship so that we | ||
// can properly query the intermediate table of a relation. Finally, we will | ||
// get the table and create the relationship instances for the developers. | ||
[$type, $id] = $this->getMorphs($name, $type, $id); | ||
|
||
$table = $instance->getTable(); | ||
|
||
$localKey = $localKey ?: $this->getKeyName(); | ||
|
||
return new MorphManyWithInverseRelation($instance->newQuery(), $this, $table . '.' . $type, $table . '.' . $id, $localKey, $name); | ||
} | ||
} |
Oops, something went wrong.