Skip to content

Commit 6e4fc49

Browse files
committed
Absolute X Relative links
- added argument `$absolute = true` to `LinkGeneratorInterface` and `FileInfoInterface` - added unit tests
1 parent 2db5fd6 commit 6e4fc49

13 files changed

Lines changed: 229 additions & 35 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": "^8.1",
1414
"ext-json": "*",
15-
"68publishers/file-storage": "^1.3.0",
15+
"68publishers/file-storage": "^1.4.0",
1616
"tg666/image": "^2.7.3",
1717
"psr/http-message": "^2.0"
1818
},

src/FileInfo.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public function __construct(ImageLinkGeneratorInterface $linkGenerator, PathInfo
2020
parent::__construct($linkGenerator, $pathInfo, $imageStorageName);
2121
}
2222

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

27-
return $this->linkGenerator->srcSet($this, $descriptor);
27+
return $this->linkGenerator->srcSet(
28+
info: $this,
29+
descriptor: $descriptor,
30+
absolute: $absolute,
31+
);
2832
}
2933

3034
public function getModifiers(): string|array|null

src/FileInfoInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
interface FileInfoInterface extends BaseFileInfoInterface, PathInfoInterface
1212
{
13-
public function srcSet(DescriptorInterface $descriptor): SrcSet;
13+
public function srcSet(DescriptorInterface $descriptor, bool $absolute = true): SrcSet;
1414
}

src/ImageStorage.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ public function resolveNoImage(string $path): ImagePathInfoInterface
9090
return $this->noImageResolver->resolveNoImage($path);
9191
}
9292

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

97-
return $this->linkGenerator->srcSet($info, $descriptor);
97+
return $this->linkGenerator->srcSet(
98+
info: $info,
99+
descriptor: $descriptor,
100+
absolute: $absolute,
101+
);
98102
}
99103

100104
public function getSignatureStrategy(): ?SignatureStrategyInterface

src/LinkGenerator/LinkGenerator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
parent::__construct($this->config);
3434
}
3535

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

50-
return parent::link($pathInfo);
50+
return parent::link(
51+
pathInfo: $pathInfo,
52+
absolute: $absolute,
53+
);
5154
}
5255

53-
public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor): SrcSet
56+
public function srcSet(ImagePathInfoInterface $info, DescriptorInterface $descriptor, bool $absolute = true): SrcSet
5457
{
5558
if (null === $this->srcSetGenerator) {
5659
$this->srcSetGenerator = $this->srcSetGeneratorFactory->create($this, $this->modifierFacade);
5760
}
5861

59-
return $this->srcSetGenerator->generate($descriptor, $info);
62+
return $this->srcSetGenerator->generate(
63+
descriptor: $descriptor,
64+
pathInfo: $info,
65+
absolute: $absolute,
66+
);
6067
}
6168

6269
public function getSignatureStrategy(): ?SignatureStrategyInterface

src/LinkGenerator/LinkGeneratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
interface LinkGeneratorInterface extends BaseLinkGeneratorInterface
1414
{
15-
public function srcSet(PathInfoInterface $info, DescriptorInterface $descriptor): SrcSet;
15+
public function srcSet(PathInfoInterface $info, DescriptorInterface $descriptor, bool $absolute = true): SrcSet;
1616

1717
public function getSignatureStrategy(): ?SignatureStrategyInterface;
1818
}

src/Responsive/Descriptor/ArgsFacade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
private readonly LinkGeneratorInterface $linkGenerator,
2222
private readonly ModifierFacadeInterface $modifierFacade,
2323
private readonly PathInfoInterface $pathInfo,
24+
private readonly bool $absolute,
2425
) {
2526
$modifiers = $this->pathInfo->getModifiers();
2627

@@ -42,7 +43,10 @@ public function getDefaultModifiers(): ?array
4243
*/
4344
public function createLink(array $modifiers): string
4445
{
45-
return $this->linkGenerator->link($this->pathInfo->withModifiers($modifiers));
46+
return $this->linkGenerator->link(
47+
pathInfo: $this->pathInfo->withModifiers($modifiers),
48+
absolute: $this->absolute,
49+
);
4650
}
4751

4852
public function getModifierAlias(string $modifierClassName): ?string

src/Responsive/SrcSetGenerator.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ public function __construct(
2121
private readonly ModifierFacadeInterface $modifierFacade,
2222
) {}
2323

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

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

3232
return $this->results[$key] = $descriptor->createSrcSet(new ArgsFacade(
33-
$this->linkGenerator,
34-
$this->modifierFacade,
35-
$pathInfo,
33+
linkGenerator: $this->linkGenerator,
34+
modifierFacade: $this->modifierFacade,
35+
pathInfo: $pathInfo,
36+
absolute: $absolute,
3637
));
3738
}
3839
}

tests/FileInfoTest.phpt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,35 @@ final class FileInfoTest extends TestCase
5454

5555
$linkGenerator->shouldReceive('srcSet')
5656
->once()
57-
->with($fileInfo, $descriptor)
57+
->with($fileInfo, $descriptor, true)
5858
->andReturn($srcSet);
5959

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

63+
public function testRelativeSrcSetShouldBeReturned(): void
64+
{
65+
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
66+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
67+
$descriptor = Mockery::mock(DescriptorInterface::class);
68+
$fileInfo = new FileInfo($linkGenerator, $pathInfo, 'default');
69+
$srcSet = new SrcSet(
70+
descriptor: 'w',
71+
links: [
72+
100 => 'var/www/h:100,w:100/file.png',
73+
200 => 'var/www/h:100,w:200/file.png',
74+
],
75+
value: 'var/www/h:100,w:100/file.png 100w, var/www/h:100,w:200/file.png 200w',
76+
);
77+
78+
$linkGenerator->shouldReceive('srcSet')
79+
->once()
80+
->with($fileInfo, $descriptor, false)
81+
->andReturn($srcSet);
82+
83+
Assert::same($srcSet, $fileInfo->srcSet($descriptor, false));
84+
}
85+
6386
public function testModifiersShouldBeNullIfFilePathInfoPassed(): void
6487
{
6588
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);

tests/ImageStorageTest.phpt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,38 @@ final class ImageStorageTest extends TestCase
249249

250250
$linkGenerator->shouldReceive('srcSet')
251251
->once()
252-
->with($pathInfo, $descriptor)
252+
->with($pathInfo, $descriptor, true)
253253
->andReturn($srcSet);
254254

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

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

260+
public function testRelativeSrcSetShouldBeReturned(): void
261+
{
262+
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);
263+
$pathInfo = Mockery::mock(ImagePathInfoInterface::class);
264+
$descriptor = Mockery::mock(DescriptorInterface::class);
265+
$srcSet = new SrcSet(
266+
descriptor: 'w',
267+
links: [
268+
100 => 'var/www/h:100,w:100/file.png',
269+
200 => 'var/www/h:100,w:200/file.png',
270+
],
271+
value: 'var/www/h:100,w:100/file.png 100w, var/www/h:100,w:200/file.png 200w',
272+
);
273+
274+
$linkGenerator->shouldReceive('srcSet')
275+
->once()
276+
->with($pathInfo, $descriptor, false)
277+
->andReturn($srcSet);
278+
279+
$imageStorage = $this->createImageStorage(linkGenerator: $linkGenerator);
280+
281+
Assert::same($srcSet, $imageStorage->srcSet($pathInfo, $descriptor, false));
282+
}
283+
260284
public function testSignatureStrategyShouldBeReturned(): void
261285
{
262286
$linkGenerator = Mockery::mock(LinkGeneratorInterface::class);

0 commit comments

Comments
 (0)