From fd105cc5031c6442d9d11d0c9f4240271b959625 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Mon, 12 Dec 2022 07:41:48 +0800 Subject: [PATCH] New componenets (#83) * Adds MySql Grammar Addon * Adds model-morph-addon Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- .github/workflows/close-pull-request.yml | 13 +++ .github/workflows/release.yaml | 25 ++++++ LICENSE | 21 +++++ README.md | 102 ++++++++++++++++++++++ composer.json | 45 ++++++++++ src/Aspect/MorphToAspect.php | 35 ++++++++ src/Aspect/QueriesRelationshipsAspect.php | 45 ++++++++++ src/ConfigProvider.php | 27 ++++++ 8 files changed, 313 insertions(+) create mode 100644 .github/workflows/close-pull-request.yml create mode 100644 .github/workflows/release.yaml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Aspect/MorphToAspect.php create mode 100644 src/Aspect/QueriesRelationshipsAspect.php create mode 100644 src/ConfigProvider.php diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml new file mode 100644 index 0000000..fa4b5ca --- /dev/null +++ b/.github/workflows/close-pull-request.yml @@ -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.

Thanks!" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..dde70ae --- /dev/null +++ b/.github/workflows/release.yaml @@ -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 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bf891ca --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5066c65 --- /dev/null +++ b/README.md @@ -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 +``` + +## Before + +```php +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 +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'; + } +} +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..db71815 --- /dev/null +++ b/composer.json @@ -0,0 +1,45 @@ +{ + "name": "friendsofhyperf/model-morph-addon", + "description": "The Model morph addon for Hyperf.", + "type": "library", + "license": "MIT", + "authors": [{ + "name": "huangdijia", + "email": "huangdijia@gmail.com" + }], + "support": { + "issues": "https://github.com/friendsofhyperf/components/issues", + "source": "https://github.com/friendsofhyperf/components" + }, + "require": { + "php": ">=8.0", + "hyperf/database": "~3.0.0", + "hyperf/di": "~3.0.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.0", + "phpstan/phpstan": "^1.0" + }, + "autoload": { + "psr-4": { + "FriendsOfHyperf\\ModelMorphAddon\\": "src" + } + }, + "extra": { + "branch-alias": { + "dev-main": "1.0-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" + } +} diff --git a/src/Aspect/MorphToAspect.php b/src/Aspect/MorphToAspect.php new file mode 100644 index 0000000..f497d12 --- /dev/null +++ b/src/Aspect/MorphToAspect.php @@ -0,0 +1,35 @@ +getArguments(); + $type = $arguments[0]; + $instance = $proceedingJoinPoint->getInstance(); + /** @var Model $parent */ + $parent = $instance->getChild(); + $class = $parent::getActualClassNameForMorph($type); + + return new $class(); + } +} diff --git a/src/Aspect/QueriesRelationshipsAspect.php b/src/Aspect/QueriesRelationshipsAspect.php new file mode 100644 index 0000000..e701faf --- /dev/null +++ b/src/Aspect/QueriesRelationshipsAspect.php @@ -0,0 +1,45 @@ +getArguments(); + + if ($arguments[1] === ['*']) { + /** @var Model $model */ + $model = $proceedingJoinPoint->getInstance()->getModel(); + + $relation = Relation::noConstraints(fn () => $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(); + } +} diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php new file mode 100644 index 0000000..9a3bb78 --- /dev/null +++ b/src/ConfigProvider.php @@ -0,0 +1,27 @@ + [ + MorphToAspect::class, + QueriesRelationshipsAspect::class, + ], + ]; + } +}