Skip to content
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"68publishers/file-storage": "^1.3.0",
"68publishers/file-storage": "^1.4.0",
"tg666/image": "^2.7.3",
"psr/http-message": "^2.0"
},
Expand Down
8 changes: 6 additions & 2 deletions src/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public function __construct(ImageLinkGeneratorInterface $linkGenerator, PathInfo
parent::__construct($linkGenerator, $pathInfo, $imageStorageName);
}

public function srcSet(DescriptorInterface $descriptor): SrcSet
public function srcSet(DescriptorInterface $descriptor, bool $absolute = true): SrcSet
{
assert($this->linkGenerator instanceof ImageLinkGeneratorInterface);

return $this->linkGenerator->srcSet($this, $descriptor);
return $this->linkGenerator->srcSet(
info: $this,
descriptor: $descriptor,
absolute: $absolute,
);
}

public function getModifiers(): string|array|null
Expand Down
2 changes: 1 addition & 1 deletion src/FileInfoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

interface FileInfoInterface extends BaseFileInfoInterface, PathInfoInterface
{
public function srcSet(DescriptorInterface $descriptor): SrcSet;
public function srcSet(DescriptorInterface $descriptor, bool $absolute = true): SrcSet;
}
8 changes: 6 additions & 2 deletions src/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ public function resolveNoImage(string $path): ImagePathInfoInterface
return $this->noImageResolver->resolveNoImage($path);
}

public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor): SrcSet
public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor, bool $absolute = true): SrcSet
{
assert($this->linkGenerator instanceof ImageLinkGeneratorInterface);

return $this->linkGenerator->srcSet($info, $descriptor);
return $this->linkGenerator->srcSet(
info: $info,
descriptor: $descriptor,
absolute: $absolute,
);
}

public function getSignatureStrategy(): ?SignatureStrategyInterface
Expand Down
15 changes: 11 additions & 4 deletions src/LinkGenerator/LinkGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
parent::__construct($this->config);
}

public function link(FilePathInfoInterface $pathInfo): string
public function link(FilePathInfoInterface $pathInfo, bool $absolute = true): string
{
if (!$pathInfo instanceof ImagePathInfoInterface) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -47,16 +47,23 @@ public function link(FilePathInfoInterface $pathInfo): string
$pathInfo = $pathInfo->withModifiers(['original' => true]);
}

return parent::link($pathInfo);
return parent::link(
pathInfo: $pathInfo,
absolute: $absolute,
);
}

public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor): SrcSet
public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor, bool $absolute = true): SrcSet
{
if (null === $this->srcSetGenerator) {
$this->srcSetGenerator = $this->srcSetGeneratorFactory->create($this, $this->modifierFacade);
}

return $this->srcSetGenerator->generate($descriptor, $info);
return $this->srcSetGenerator->generate(
descriptor: $descriptor,
pathInfo: $info,
absolute: $absolute,
);
}

public function getSignatureStrategy(): ?SignatureStrategyInterface
Expand Down
2 changes: 1 addition & 1 deletion src/LinkGenerator/LinkGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

interface LinkGeneratorInterface extends BaseLinkGeneratorInterface
{
public function srcSet(PathInfoInterface $info, DescriptorInterface $descriptor): SrcSet;
public function srcSet(PathInfoInterface $info, DescriptorInterface $descriptor, bool $absolute = true): SrcSet;

public function getSignatureStrategy(): ?SignatureStrategyInterface;
}
6 changes: 5 additions & 1 deletion src/Responsive/Descriptor/ArgsFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
private readonly LinkGeneratorInterface $linkGenerator,
private readonly ModifierFacadeInterface $modifierFacade,
private readonly PathInfoInterface $pathInfo,
private readonly bool $absolute,
) {
$modifiers = $this->pathInfo->getModifiers();

Expand All @@ -42,7 +43,10 @@ public function getDefaultModifiers(): ?array
*/
public function createLink(array $modifiers): string
{
return $this->linkGenerator->link($this->pathInfo->withModifiers($modifiers));
return $this->linkGenerator->link(
pathInfo: $this->pathInfo->withModifiers($modifiers),
absolute: $this->absolute,
);
}

public function getModifierAlias(string $modifierClassName): ?string
Expand Down
11 changes: 6 additions & 5 deletions src/Responsive/SrcSetGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ public function __construct(
private readonly ModifierFacadeInterface $modifierFacade,
) {}

public function generate(DescriptorInterface $descriptor, PathInfoInterface $pathInfo): SrcSet
public function generate(DescriptorInterface $descriptor, PathInfoInterface $pathInfo, bool $absolute): SrcSet
{
$key = $descriptor . '::' . (empty($pathInfo->getModifiers()) ? $pathInfo->withModifiers(['original' => true]) : $pathInfo);
$key = $descriptor . '::' . ($absolute ? 'abs' : 'rel') . '::' . (empty($pathInfo->getModifiers()) ? $pathInfo->withModifiers(['original' => true]) : $pathInfo);

if (array_key_exists($key, $this->results)) {
return $this->results[$key];
}

return $this->results[$key] = $descriptor->createSrcSet(new ArgsFacade(
$this->linkGenerator,
$this->modifierFacade,
$pathInfo,
linkGenerator: $this->linkGenerator,
modifierFacade: $this->modifierFacade,
pathInfo: $pathInfo,
absolute: $absolute,
));
}
}
25 changes: 24 additions & 1 deletion tests/FileInfoTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,35 @@ final class FileInfoTest extends TestCase

$linkGenerator->shouldReceive('srcSet')
->once()
->with($fileInfo, $descriptor)
->with($fileInfo, $descriptor, true)
->andReturn($srcSet);

Assert::same($srcSet, $fileInfo->srcSet($descriptor));
}

public function testRelativeSrcSetShouldBeReturned(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
$descriptor = Mockery::mock(DescriptorInterface::class);
$fileInfo = new FileInfo($linkGenerator, $pathInfo, 'default');
$srcSet = new SrcSet(
descriptor: 'w',
links: [
100 => 'var/www/h:100,w:100/file.png',
200 => 'var/www/h:100,w:200/file.png',
],
value: 'var/www/h:100,w:100/file.png 100w, var/www/h:100,w:200/file.png 200w',
);

$linkGenerator->shouldReceive('srcSet')
->once()
->with($fileInfo, $descriptor, false)
->andReturn($srcSet);

Assert::same($srcSet, $fileInfo->srcSet($descriptor, false));
}

public function testModifiersShouldBeNullIfFilePathInfoPassed(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
Expand Down
26 changes: 25 additions & 1 deletion tests/ImageStorageTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,38 @@ final class ImageStorageTest extends TestCase

$linkGenerator->shouldReceive('srcSet')
->once()
->with($pathInfo, $descriptor)
->with($pathInfo, $descriptor, true)
->andReturn($srcSet);

$imageStorage = $this->createImageStorage(linkGenerator: $linkGenerator);

Assert::same($srcSet, $imageStorage->srcSet($pathInfo, $descriptor));
}

public function testRelativeSrcSetShouldBeReturned(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
$descriptor = Mockery::mock(DescriptorInterface::class);
$srcSet = new SrcSet(
descriptor: 'w',
links: [
100 => 'var/www/h:100,w:100/file.png',
200 => 'var/www/h:100,w:200/file.png',
],
value: 'var/www/h:100,w:100/file.png 100w, var/www/h:100,w:200/file.png 200w',
);

$linkGenerator->shouldReceive('srcSet')
->once()
->with($pathInfo, $descriptor, false)
->andReturn($srcSet);

$imageStorage = $this->createImageStorage(linkGenerator: $linkGenerator);

Assert::same($srcSet, $imageStorage->srcSet($pathInfo, $descriptor, false));
}

public function testSignatureStrategyShouldBeReturned(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
Expand Down
32 changes: 31 additions & 1 deletion tests/LinkGenerator/LinkGeneratorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,43 @@ final class LinkGeneratorTest extends TestCase

$srcSetGenerator->shouldReceive('generate')
->times(2)
->with($descriptor, $pathInfo)
->with($descriptor, $pathInfo, true)
->andReturn($srcSet);

Assert::same($srcSet, $linkGenerator->srcSet($pathInfo, $descriptor));
Assert::same($srcSet, $linkGenerator->srcSet($pathInfo, $descriptor));
}

public function testRelativeSrcSetShouldBeCreated(): void
{
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
$srcSetGeneratorFactory = Mockery::mock(SrcSetGeneratorFactoryInterface::class);
$srcSetGenerator = Mockery::mock(SrcSetGenerator::class);
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
$descriptor = Mockery::mock(DescriptorInterface::class);
$linkGenerator = new LinkGenerator(new Config([]), $modifierFacade, $srcSetGeneratorFactory);
$srcSet = new SrcSet(
descriptor: 'test',
links: [
1 => 'srcset',
],
value: 'srcset',
);

$srcSetGeneratorFactory->shouldReceive('create')
->once()
->with($linkGenerator, $modifierFacade)
->andReturn($srcSetGenerator);

$srcSetGenerator->shouldReceive('generate')
->times(2)
->with($descriptor, $pathInfo, false)
->andReturn($srcSet);

Assert::same($srcSet, $linkGenerator->srcSet($pathInfo, $descriptor, false));
Assert::same($srcSet, $linkGenerator->srcSet($pathInfo, $descriptor, false));
}

public function tearDown(): void
{
Mockery::close();
Expand Down
41 changes: 34 additions & 7 deletions tests/Responsive/Descriptor/ArgsFacadeTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ArgsFacadeTest extends TestCase
->withNoArgs()
->andReturn(null);

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::null($facade->getDefaultModifiers());
}
Expand All @@ -48,7 +48,7 @@ final class ArgsFacadeTest extends TestCase
->withNoArgs()
->andReturn(['w' => 150]);

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::same(['w' => 150], $facade->getDefaultModifiers());
}
Expand Down Expand Up @@ -79,7 +79,7 @@ final class ArgsFacadeTest extends TestCase
return ['w' => 150];
});

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::same(['w' => 150], $facade->getDefaultModifiers());
}
Expand All @@ -103,14 +103,41 @@ final class ArgsFacadeTest extends TestCase

$linkGenerator->shouldReceive('link')
->once()
->with($modifiedPathInfo)
->with($modifiedPathInfo, true)
->andReturn('/var/www/h:100/file.png');

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::same('/var/www/h:100/file.png', $facade->createLink(['h' => 100]));
}

public function testRelativeLinkShouldBeCreated(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
$modifierFacade = Mockery::mock(ModifierFacadeInterface::class);
$pathInfo = Mockery::mock(PathInfoInterface::class);
$modifiedPathInfo = Mockery::mock(PathInfoInterface::class);

$pathInfo->shouldReceive('getModifiers')
->once()
->withNoArgs()
->andReturn(null);

$pathInfo->shouldReceive('withModifiers')
->once()
->with(['h' => 100])
->andReturn($modifiedPathInfo);

$linkGenerator->shouldReceive('link')
->once()
->with($modifiedPathInfo, false)
->andReturn('var/www/h:100/file.png');

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, false);

Assert::same('var/www/h:100/file.png', $facade->createLink(['h' => 100]));
}

public function testErrorShouldBeTriggeredIfModifierNotFound(): void
{
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
Expand All @@ -133,7 +160,7 @@ final class ArgsFacadeTest extends TestCase
->with(Width::class)
->andThrows(new InvalidArgumentException('Missing modifier.'));

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::error(
static fn () => $facade->getModifierAlias(Width::class),
Expand Down Expand Up @@ -164,7 +191,7 @@ final class ArgsFacadeTest extends TestCase
->with(Width::class)
->andReturn(new Width());

$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo);
$facade = new ArgsFacade($linkGenerator, $modifierFacade, $pathInfo, true);

Assert::same('w', $facade->getModifierAlias(Width::class));
}
Expand Down
Loading