Skip to content

Commit 8956cf2

Browse files
authored
feat: domain alias (#13)
1 parent 6c715af commit 8956cf2

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Edit .env.local to match your setup:
7474
```
7575
APP_DEBUG=0
7676
77-
ALLOWED_DOMAINS=mysite.com,another-site.com
77+
ALLOWED_DOMAINS=mysite.com,another-site.com=domain_alias
7878
STORAGE_TYPE=local # "local" or "s3"
7979
8080
# Local storage configuration

docker/php/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
FROM bref/php-83-fpm-dev:2
22
COPY --from=bref/extra-imagick-php-83:1 /opt /opt
3+
COPY --from=bref/extra-pcov-php-83:1 /opt /opt

src/Cdn.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@
2929

3030
final class Cdn
3131
{
32-
/** @param string[] $allowedDomains */
32+
/**
33+
* @param string[] $allowedDomains
34+
* @param string[] $domainsAliases
35+
*/
3336
public function __construct(
3437
private readonly array $allowedDomains,
38+
private readonly array $domainsAliases,
3539
private readonly Storage $storage,
3640
private readonly ImageProcessor $imageProcessor,
3741
private readonly Cache $cache,
@@ -45,7 +49,7 @@ public function handleRequest(Request $request): Response
4549
return new Response('Only GET request is supported.', Response::HTTP_METHOD_NOT_ALLOWED);
4650
}
4751

48-
$decoder = new UriDecoder($request->getRequestUri());
52+
$decoder = new UriDecoder($request->getRequestUri(), $this->domainsAliases);
4953

5054
try {
5155
$this->validate($decoder);

src/ContainerConfig.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ public function __construct()
3838
$this['storage_driver'] = $this->getEnv('STORAGE_DRIVER');
3939
$this['storage_path'] = $this->getEnv('STORAGE_PATH');
4040
$this['cache_ttl'] = (int) $this->getEnv('CACHE_TTL');
41-
$this['allowed_domains'] = explode(',', $this->getEnv('ALLOWED_DOMAINS'));
41+
42+
$allowedDomains = [];
43+
$domainsAliases = [];
44+
foreach (explode(',', $this->getEnv('ALLOWED_DOMAINS')) as $domain) {
45+
if (true === str_contains($domain, '=')) {
46+
$parts = explode('=', $domain);
47+
48+
$allowedDomains[] = $parts[0];
49+
$domainsAliases[$parts[1]] = $parts[0];
50+
51+
continue;
52+
}
53+
54+
$allowedDomains[] = $domain;
55+
}
56+
57+
$this['allowed_domains'] = $allowedDomains;
58+
$this['domains_aliases'] = $domainsAliases;
59+
4260
$this['image_compression'] = (int) $this->getEnv('IMAGE_COMPRESSION');
4361

4462
$this[LoggerInterface::class] = fn () => new BrefLogger(
@@ -99,6 +117,7 @@ public function __construct()
99117

100118
$this[Cdn::class] = static fn(self $c) => new Cdn(
101119
$c['allowed_domains'],
120+
$c['domains_aliases'],
102121
$c[Storage::class],
103122
$c[ImageProcessor::class],
104123
$c[Cache::class],

src/Decoder/UriDecoder.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
final class UriDecoder
1919
{
20-
private string $imageUrl;
21-
private string $domain;
22-
private string $filename;
23-
private QueryParams $query;
20+
private ?string $finalUri = null;
21+
private readonly string $imageUrl;
22+
private readonly string $domain;
23+
private readonly string $filename;
24+
private readonly QueryParams $query;
2425

25-
public function __construct(private readonly string $uri)
26+
/** @param string[] $domainsAliases */
27+
public function __construct(private readonly string $uri, private readonly array $domainsAliases = [])
2628
{
2729
$this->imageUrl = explode('?', $this->getUri())[0];
2830
$this->domain = (string) parse_url($this->getUri(), PHP_URL_HOST);
@@ -35,9 +37,21 @@ public function __construct(private readonly string $uri)
3537

3638
public function getUri(): string
3739
{
38-
$uri = str_replace(['http://', 'http:/', 'https://', 'https:/'], '', $this->uri);
40+
if (null === $this->finalUri) {
41+
$uri = ltrim($this->uri, '/');
3942

40-
return 'https://' . ltrim($uri, '/');
43+
if (true === str_starts_with($uri, '_')) {
44+
$domainAlias = str_replace('_', '', explode('/', $uri)[0]);
45+
$domain = $this->domainsAliases[$domainAlias] ?? '';
46+
$uri = str_replace("_{$domainAlias}_", $domain, $uri);
47+
}
48+
49+
$uri = str_replace(['http://', 'http:/', 'https://', 'https:/'], '', $uri);
50+
51+
$this->finalUri = "https://$uri";
52+
}
53+
54+
return $this->finalUri;
4155
}
4256

4357
public function getImageUrl(): string

tests/Decoder/UriDecoderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ public function canGetUri(): void
2828
static::assertSame(static::TEST_BASE_URI . $this->getQueryParameters(), $decoder->getUri());
2929
}
3030

31+
#[Test]
32+
public function canGetUriWithAnAlias(): void
33+
{
34+
$decoder = new UriDecoder(
35+
static::TEST_BASE_URI_ALIAS . $this->getQueryParameters(),
36+
[static::TEST_DOMAIN_ALIAS => static::TEST_DOMAIN],
37+
);
38+
39+
static::assertSame(static::TEST_BASE_URI . $this->getQueryParameters(), $decoder->getUri());
40+
}
41+
3142
#[Test]
3243
public function canGetImageUrl(): void
3344
{

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
class TestCase extends BaseTestCase
2424
{
2525
protected const string TEST_BASE_URI = 'https://example.com/image.jpg';
26+
protected const string TEST_BASE_URI_ALIAS = '_e_/image.jpg';
2627
protected const string TEST_WATERMARK_URL = 'https://example.com/watermark.jpg';
2728
protected const string TEST_DOMAIN = 'example.com';
29+
protected const string TEST_DOMAIN_ALIAS = 'e';
2830
protected const string TEST_FILENAME = 'image.jpg';
2931
protected const string TEST_FILENAME_MD5 = '18867d45576d8283d6fabb82406789c8.jpg';
3032
protected const string TEST_EXTENSION = 'jpg';

0 commit comments

Comments
 (0)