Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix extended model morph map by checking parent class map #1944

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/Base/ModelManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function guessModelClass(string $modelContract): string
return 'Lunar\\Models\\'.$shortName;
}

public function isLunarModel(BaseModel $model): bool
public function isLunarModel(string|BaseModel $model): bool
{
$class = (new \ReflectionClass($model));

Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/Base/Traits/HasModelExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunar\Base\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Lunar\Facades\ModelManifest;

Expand Down Expand Up @@ -46,6 +47,23 @@ public static function modelClass(): string
return ModelManifest::get($contractClass) ?? static::class;
}

public function getMorphClass(): string
{
$morphMap = Relation::morphMap();

if ($customModelMorphMap = array_search(static::modelClass(), $morphMap, true)) {
return $customModelMorphMap;
}

$parentClass = get_parent_class(static::class);

if (ModelManifest::isLunarModel($parentClass) && $lunarModelMorphMap = array_search($parentClass, $morphMap, true)) {
return $lunarModelMorphMap;
}

return parent::getMorphClass();
}

public static function isLunarInstance(): bool
{
return static::class == static::modelClass();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Facades/ModelManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @method static string|null get(string $interfaceClass)
* @method static string guessContractClass(string $modelClass)
* @method static string guessModelClass(string $modelContract)
* @method static bool isLunarModel(BaseModel $model)
* @method static bool isLunarModel(string|BaseModel $model)
* @method static string getTable(BaseModel $model)
* @method static void morphMap()
* @method static string getMorphMapKey(string $className)
Expand Down
13 changes: 13 additions & 0 deletions tests/core/Unit/Base/Traits/HasModelExtendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,16 @@ function () {
expect($newStaticMethod)->toBeInstanceOf(Collection::class);
expect($newStaticMethod)->toHaveCount(3);
});

test('morph map is correct when models are extended', function () {
\Lunar\Facades\ModelManifest::replace(
\Lunar\Models\Contracts\Product::class,
\Lunar\Tests\Core\Stubs\Models\CustomProduct::class
);

expect((new \Lunar\Tests\Core\Stubs\Models\CustomProduct)->getMorphClass())
->toBe('product')
->and((new Product)->getMorphClass())
->toBe('product');
});