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

Allow path generators to be customised per model #2845

Merged
merged 10 commits into from
Mar 17, 2022
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
4 changes: 2 additions & 2 deletions src/MediaCollections/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function syncFileNames(Media $media): void

public function syncMediaPath(Media $media): void
{
$factory = PathGeneratorFactory::create();
$factory = PathGeneratorFactory::create($media);

$oldMedia = (clone $media)->fill($media->getOriginal());

Expand Down Expand Up @@ -291,7 +291,7 @@ protected function renameConversionFiles(Media $media): void
public function getMediaDirectory(Media $media, ?string $type = null): string
{
$directory = null;
$pathGenerator = PathGeneratorFactory::create();
$pathGenerator = PathGeneratorFactory::create($media);

if (! $type) {
$directory = $pathGenerator->getPath($media);
Expand Down
11 changes: 9 additions & 2 deletions src/MediaCollections/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Spatie\MediaLibrary\Support\File;
use Spatie\MediaLibrary\Support\MediaLibraryPro;
use Spatie\MediaLibrary\Support\TemporaryDirectory;
use Spatie\MediaLibrary\Support\UrlGenerator\UrlGenerator;
use Spatie\MediaLibrary\Support\UrlGenerator\UrlGeneratorFactory;
use Spatie\MediaLibraryPro\Models\TemporaryUpload;

Expand Down Expand Up @@ -81,18 +82,24 @@ public function getUrl(string $conversionName = ''): string

public function getTemporaryUrl(DateTimeInterface $expiration, string $conversionName = '', array $options = []): string
{
$urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
$urlGenerator = $this->getUrlGenerator($conversionName);

return $urlGenerator->getTemporaryUrl($expiration, $options);
}

public function getPath(string $conversionName = ''): string
{
$urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
$urlGenerator = $this->getUrlGenerator($conversionName);

return $urlGenerator->getPath();
}


public function getUrlGenerator(string $conversionName): UrlGenerator
{
return UrlGeneratorFactory::createForMedia($this, $conversionName);
}

public function getAvailableUrl(array $conversionNames): string
{
foreach ($conversionNames as $conversionName) {
Expand Down
2 changes: 1 addition & 1 deletion src/ResponsiveImages/ResponsiveImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function stringBetween(string $subject, string $startCharacter, string

public function delete(): self
{
$pathGenerator = PathGeneratorFactory::create();
$pathGenerator = PathGeneratorFactory::create($this->media);

$path = $pathGenerator->getPathForResponsiveImages($this->media);

Expand Down
18 changes: 16 additions & 2 deletions src/Support/PathGenerator/PathGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@
namespace Spatie\MediaLibrary\Support\PathGenerator;

use Spatie\MediaLibrary\MediaCollections\Exceptions\InvalidPathGenerator;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class PathGeneratorFactory
{
public static function create(): PathGenerator
public static function create(Media $media): PathGenerator
{
$pathGeneratorClass = config('media-library.path_generator');
$pathGeneratorClass = self::getPathGeneratorClass($media);

static::guardAgainstInvalidPathGenerator($pathGeneratorClass);

return app($pathGeneratorClass);
}

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)) {
return $customPathGeneratorClass;
}
}

return $defaultPathGeneratorClass;
}

protected static function guardAgainstInvalidPathGenerator(string $pathGeneratorClass): void
{
if (! class_exists($pathGeneratorClass)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Support/UrlGenerator/UrlGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function createForMedia(Media $media, string $conversionName = '')
/** @var \Spatie\MediaLibrary\Support\UrlGenerator\UrlGenerator $urlGenerator */
$urlGenerator = app($urlGeneratorClass);

$pathGenerator = PathGeneratorFactory::create();
$pathGenerator = PathGeneratorFactory::create($media);

$urlGenerator
->setMedia($media)
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/PathGenerator/BasePathGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator;
use Spatie\MediaLibrary\Tests\Support\PathGenerator\CustomPathGenerator;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithConversion;

beforeEach(function () {
$this->config = app('config');
Expand Down Expand Up @@ -36,3 +37,15 @@

expect($this->urlGenerator->getPathRelativeToRoot())->toEqual($pathRelativeToRoot);
});

it('can use a custom path generator on the model', function () {
config()->set('media-library.custom_path_generators', [
TestModelWithConversion::class => CustomPathGenerator::class,
]);

$media = $this->testModelWithConversion
->addMedia($this->getTestFilesDirectory('test.jpg'))
->toMediaCollection();

expect($media->getUrl())->toEqual('/media/c4ca4238a0b923820dcc509a6f75849b/test.jpg');
});