diff --git a/src/Support/PathGenerator/PathGeneratorFactory.php b/src/Support/PathGenerator/PathGeneratorFactory.php index 8bb558302..63d770e6b 100644 --- a/src/Support/PathGenerator/PathGeneratorFactory.php +++ b/src/Support/PathGenerator/PathGeneratorFactory.php @@ -2,6 +2,7 @@ namespace Spatie\MediaLibrary\Support\PathGenerator; +use Illuminate\Database\Eloquent\Relations\Relation; use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidPathGenerator; use Spatie\MediaLibrary\MediaCollections\Models\Media; @@ -9,7 +10,7 @@ class PathGeneratorFactory { public static function create(Media $media): PathGenerator { - $pathGeneratorClass = self::getPathGeneratorClass($media); + $pathGeneratorClass = static::getPathGeneratorClass($media); static::guardAgainstInvalidPathGenerator($pathGeneratorClass); @@ -21,7 +22,7 @@ protected static function getPathGeneratorClass(Media $media) $defaultPathGeneratorClass = config('media-library.path_generator'); foreach (config('media-library.custom_path_generators', []) as $modelClass => $customPathGeneratorClass) { - if (is_a($media->model_type, $modelClass, true) || $media->model_type === $modelClass) { + if (static::mediaBelongToModelClass($media, $modelClass)) { return $customPathGeneratorClass; } } @@ -29,6 +30,24 @@ protected static function getPathGeneratorClass(Media $media) return $defaultPathGeneratorClass; } + protected static function mediaBelongToModelClass(Media $media, string $modelClass): bool + { + // model doesn't have morphMap, so morph type and class are equal + if (is_a($media->model_type, $modelClass, true)) { + return true; + } + // config is set via morphMap alias + if ($media->model_type === $modelClass) { + return true; + } + // config is set via morphMap class name + if (is_a((string)Relation::getMorphedModel($media->model_type), $modelClass, true)) { + return true; + } + + return false; + } + protected static function guardAgainstInvalidPathGenerator(string $pathGeneratorClass): void { if (! class_exists($pathGeneratorClass)) { diff --git a/tests/Support/PathGenerator/BasePathGeneratorTest.php b/tests/Support/PathGenerator/BasePathGeneratorTest.php index 408ee5907..b05de650c 100644 --- a/tests/Support/PathGenerator/BasePathGeneratorTest.php +++ b/tests/Support/PathGenerator/BasePathGeneratorTest.php @@ -4,6 +4,7 @@ use Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator; use Spatie\MediaLibrary\Tests\Support\PathGenerator\CustomPathGenerator; use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithConversion; +use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithMorphMap; beforeEach(function () { $this->config = app('config'); @@ -61,3 +62,15 @@ expect($media->getUrl())->toEqual('/media/c4ca4238a0b923820dcc509a6f75849b/test.jpg'); }); + +it('can use a custom path generator on a morph map model via class', function () { + config()->set('media-library.custom_path_generators', [ + TestModelWithMorphMap::class => CustomPathGenerator::class, + ]); + + $media = $this->testModelWithMorphMap + ->addMedia($this->getTestFilesDirectory('test.jpg')) + ->toMediaCollection(); + + expect($media->getUrl())->toEqual('/media/c4ca4238a0b923820dcc509a6f75849b/test.jpg'); +});