-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkGenerator.php
More file actions
125 lines (104 loc) 路 4.43 KB
/
Copy pathLinkGenerator.php
File metadata and controls
125 lines (104 loc) 路 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace SixtyEightPublishers\ImageStorage\LinkGenerator;
use SixtyEightPublishers\FileStorage\Config\ConfigInterface;
use SixtyEightPublishers\FileStorage\LinkGenerator\LinkGenerator as FileLinkGenerator;
use SixtyEightPublishers\FileStorage\PathInfoInterface as FilePathInfoInterface;
use SixtyEightPublishers\ImageStorage\Config\Config;
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
use SixtyEightPublishers\ImageStorage\Modifier\Facade\ModifierFacadeInterface;
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
use SixtyEightPublishers\ImageStorage\Responsive\Descriptor\DescriptorInterface;
use SixtyEightPublishers\ImageStorage\Responsive\SrcSet;
use SixtyEightPublishers\ImageStorage\Responsive\SrcSetGenerator;
use SixtyEightPublishers\ImageStorage\Responsive\SrcSetGeneratorFactoryInterface;
use SixtyEightPublishers\ImageStorage\Security\SignatureStrategyInterface;
use function assert;
use function explode;
use function is_string;
use function sprintf;
final class LinkGenerator extends FileLinkGenerator implements LinkGeneratorInterface
{
private ?SrcSetGenerator $srcSetGenerator = null;
public function __construct(
private readonly ConfigInterface $config,
private readonly ModifierFacadeInterface $modifierFacade,
private readonly SrcSetGeneratorFactoryInterface $srcSetGeneratorFactory,
private readonly ?SignatureStrategyInterface $signatureStrategy = null,
) {
parent::__construct($this->config);
}
public function link(FilePathInfoInterface $pathInfo, bool $absolute = true): string
{
if (!$pathInfo instanceof ImagePathInfoInterface) {
throw new InvalidArgumentException(
message: sprintf(
'Path info passed into the method %s() must be an instance of %s.',
__METHOD__,
ImagePathInfoInterface::class,
),
);
}
if (null === $pathInfo->getModifiers() || [] === $pathInfo->getModifiers()) {
$pathInfo = $pathInfo->withModifiers(['original' => true]);
}
return parent::link(
pathInfo: $pathInfo,
absolute: $absolute,
);
}
public function srcSet(ImagePathInfoInterface $info, ?DescriptorInterface $descriptor = null, bool $absolute = true): SrcSet
{
if (null === $descriptor) {
$descriptor = $this->resolveDescriptor(
pathInfo: $info,
);
}
if (null === $this->srcSetGenerator) {
$this->srcSetGenerator = $this->srcSetGeneratorFactory->create($this, $this->modifierFacade);
}
return $this->srcSetGenerator->generate(
descriptor: $descriptor,
pathInfo: $info,
absolute: $absolute,
);
}
public function getSignatureStrategy(): ?SignatureStrategyInterface
{
return $this->signatureStrategy;
}
protected function buildQueryParams(FilePathInfoInterface $pathInfo): array
{
$params = parent::buildQueryParams($pathInfo);
if (null !== $this->signatureStrategy) {
$signatureParameterName = $this->config[Config::SIGNATURE_PARAMETER_NAME];
assert(is_string($signatureParameterName));
$token = $this->signatureStrategy->createToken($pathInfo->getPath());
if (null !== $token) {
$params[$signatureParameterName] = $token;
}
}
return $params;
}
private function resolveDescriptor(ImagePathInfoInterface $pathInfo): DescriptorInterface
{
$modifiers = $pathInfo->getModifiers();
if (is_string($modifiers)) {
$presets = $this->modifierFacade->getPresetCollection();
$assigner = $this->config[Config::MODIFIER_ASSIGNER];
$assigner = empty($assigner) ? ':' : $assigner;
[$presetAlias] = explode($assigner, $modifiers, 2);
$preset = $presets->get(presetAlias: $presetAlias);
if (null !== $preset->descriptor) {
return $preset->descriptor;
}
}
throw new InvalidArgumentException(
message: sprintf(
'Unable to resolve descriptor for path info %s. Descriptor must be provided to the method %s::srcSet() manually.',
$pathInfo,
__CLASS__,
),
);
}
}