-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds packages * Optimize * Remove PHP version limit Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
0 parents
commit 0e532e7
Showing
8 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
name: Close Pull Request | ||
|
||
on: | ||
pull_request_target: | ||
types: [opened] | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: superbrothers/close-pull-request@v3 | ||
with: | ||
comment: "Thank you for your pull request. However, you have submitted this PR on the friendsofhyperf organization which is a read-only sub split of `friendsofhyperf/components`. Please submit your PR on the https://github.com/friendsofhyperf/components repository.<br><br>Thanks!" |
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,25 @@ | ||
on: | ||
push: | ||
# Sequence of patterns matched against refs/tags | ||
tags: | ||
- 'v*' # Push events to matching v*, i.e. v1.0, v1.0.0 | ||
|
||
name: Release | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Create Release | ||
id: create_release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ github.ref }} | ||
release_name: Release ${{ github.ref }} | ||
draft: false | ||
prerelease: false |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 D.J.Hwang | ||
|
||
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,102 @@ | ||
# model-morph-addon | ||
|
||
[![Latest Test](https://github.com/friendsofhyperf/model-morph-addon/workflows/tests/badge.svg)](https://github.com/friendsofhyperf/model-morph-addon/actions) | ||
[![Latest Stable Version](https://poser.pugx.org/friendsofhyperf/model-morph-addon/v/stable.svg)](https://packagist.org/packages/friendsofhyperf/model-morph-addon) | ||
[![Latest Unstable Version](https://poser.pugx.org/friendsofhyperf/model-morph-addon/v/unstable.svg)](https://packagist.org/packages/friendsofhyperf/model-morph-addon) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/friendsofhyperf/model-morph-addon)](https://packagist.org/packages/friendsofhyperf/model-morph-addon) | ||
[![License](https://img.shields.io/packagist/l/friendsofhyperf/model-morph-addon)](https://github.com/friendsofhyperf/model-morph-addon) | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require friendsofhyperf/model-morph-addon:^2.0 | ||
``` | ||
|
||
## Before | ||
|
||
```php | ||
<?php | ||
namespace App\Model; | ||
|
||
class Image extends Model | ||
{ | ||
public function imageable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} | ||
|
||
class Book extends Model | ||
{ | ||
public function images() | ||
{ | ||
return $this->morphMany(Image::class, 'imageable'); | ||
} | ||
} | ||
|
||
class User extends Model | ||
{ | ||
public function images() | ||
{ | ||
return $this->morphMany(Image::class, 'imageable'); | ||
} | ||
} | ||
|
||
// Global | ||
Relation::morphMap([ | ||
'user' => App\Model\User::class, | ||
'book' => App\Model\Book::class, | ||
]); | ||
``` | ||
|
||
## After | ||
|
||
```php | ||
<?php | ||
namespace App\Model; | ||
|
||
class Image extends Model | ||
{ | ||
public function imageable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
// Privately-owned | ||
public static function getActualClassNameForMorph($class) | ||
{ | ||
$morphMap = [ | ||
'user' => User::class, | ||
'book' => Book::class, | ||
]; | ||
|
||
return Arr::get($morphMap, $class, $class); | ||
} | ||
} | ||
|
||
class Book extends Model | ||
{ | ||
public function images() | ||
{ | ||
return $this->morphMany(Image::class, 'imageable'); | ||
} | ||
|
||
public function getMorphClass() | ||
{ | ||
return 'book'; | ||
} | ||
} | ||
|
||
class User extends Model | ||
{ | ||
public function images() | ||
{ | ||
return $this->morphMany(Image::class, 'imageable'); | ||
} | ||
|
||
public function getMorphClass() | ||
{ | ||
return 'user'; | ||
} | ||
} | ||
``` |
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,44 @@ | ||
{ | ||
"name": "friendsofhyperf/model-morph-addon", | ||
"description": "The Model morph addon for Hyperf.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "huangdijia", | ||
"email": "[email protected]" | ||
}], | ||
"support": { | ||
"issues": "https://github.com/friendsofhyperf/components/issues", | ||
"source": "https://github.com/friendsofhyperf/components" | ||
}, | ||
"require": { | ||
"hyperf/database": "~2.2.0", | ||
"hyperf/di": "~2.2.0" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^3.0", | ||
"phpstan/phpstan": "^1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"FriendsOfHyperf\\ModelMorphAddon\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"0.x-dev": "0.1-dev" | ||
}, | ||
"hyperf": { | ||
"config": "FriendsOfHyperf\\ModelMorphAddon\\ConfigProvider" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"config": { | ||
"sort-packages": true, | ||
"optimize-autoloader": true | ||
}, | ||
"scripts": { | ||
"analyse": "phpstan analyse --memory-limit 4096M -l 0 -c phpstan.neon ./src", | ||
"cs-fix": "php-cs-fixer fix $1" | ||
} | ||
} |
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/2.x/README.md | ||
* @contact [email protected] | ||
*/ | ||
namespace FriendsOfHyperf\ModelMorphAddon\Aspect; | ||
|
||
use Hyperf\Database\Model\Model; | ||
use Hyperf\Database\Model\Relations\MorphTo; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
|
||
class MorphToAspect extends AbstractAspect | ||
{ | ||
public $classes = [ | ||
MorphTo::class . '::createModelByType', | ||
]; | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
$arguments = $proceedingJoinPoint->getArguments(); | ||
$type = $arguments[0]; | ||
$instance = $proceedingJoinPoint->getInstance(); | ||
/** @var Model $parent */ | ||
$parent = $instance->getChild(); | ||
$class = $parent::getActualClassNameForMorph($type); | ||
|
||
return new $class(); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/2.x/README.md | ||
* @contact [email protected] | ||
*/ | ||
namespace FriendsOfHyperf\ModelMorphAddon\Aspect; | ||
|
||
use Hyperf\Database\Model\Concerns\QueriesRelationships; | ||
use Hyperf\Database\Model\Model; | ||
use Hyperf\Database\Model\Relations\Relation; | ||
use Hyperf\Di\Aop\AbstractAspect; | ||
use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
|
||
class QueriesRelationshipsAspect extends AbstractAspect | ||
{ | ||
public $classes = [ | ||
QueriesRelationships::class . '::hasMorph', | ||
]; | ||
|
||
public function process(ProceedingJoinPoint $proceedingJoinPoint) | ||
{ | ||
$arguments = $proceedingJoinPoint->getArguments(); | ||
|
||
if ($arguments[1] === ['*']) { | ||
/** @var Model $model */ | ||
$model = $proceedingJoinPoint->getInstance()->getModel(); | ||
|
||
$relation = Relation::noConstraints(function () use ($model, $arguments) { | ||
return $model->{$arguments[0]}(); | ||
}); | ||
$types = $model->newModelQuery()->distinct()->pluck($relation->getMorphType())->filter()->all(); | ||
|
||
foreach ($types as &$type) { | ||
$type = $model::getActualClassNameForMorph($type) ?? $type; | ||
} | ||
|
||
$proceedingJoinPoint->arguments['keys']['types'] = $types; | ||
} | ||
|
||
return $proceedingJoinPoint->process(); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/2.x/README.md | ||
* @contact [email protected] | ||
*/ | ||
namespace FriendsOfHyperf\ModelMorphAddon; | ||
|
||
use FriendsOfHyperf\ModelMorphAddon\Aspect\MorphToAspect; | ||
use FriendsOfHyperf\ModelMorphAddon\Aspect\QueriesRelationshipsAspect; | ||
|
||
class ConfigProvider | ||
{ | ||
public function __invoke(): array | ||
{ | ||
defined('BASE_PATH') or define('BASE_PATH', ''); | ||
|
||
return [ | ||
'aspects' => [ | ||
MorphToAspect::class, | ||
QueriesRelationshipsAspect::class, | ||
], | ||
]; | ||
} | ||
} |