mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 12:13:13 +08:00
Ensured base path is honored when stringifying short URLs with a custom domain
This commit is contained in:
@@ -121,7 +121,7 @@ return [
|
||||
],
|
||||
|
||||
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ['em'],
|
||||
ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain'],
|
||||
ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain', 'config.router.base_path'],
|
||||
ShortUrl\Transformer\ShortUrlDataTransformer::class => [ShortUrl\Helper\ShortUrlStringifier::class],
|
||||
|
||||
Mercure\MercureUpdatesGenerator::class => [ShortUrl\Transformer\ShortUrlDataTransformer::class],
|
||||
|
||||
@@ -7,13 +7,17 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
||||
use Laminas\Diactoros\Uri;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
class ShortUrlStringifier implements ShortUrlStringifierInterface
|
||||
{
|
||||
private array $domainConfig;
|
||||
private string $basePath;
|
||||
|
||||
public function __construct(array $domainConfig)
|
||||
public function __construct(array $domainConfig, string $basePath = '')
|
||||
{
|
||||
$this->domainConfig = $domainConfig;
|
||||
$this->basePath = $basePath;
|
||||
}
|
||||
|
||||
public function stringify(ShortUrl $shortUrl): string
|
||||
@@ -31,6 +35,6 @@ class ShortUrlStringifier implements ShortUrlStringifierInterface
|
||||
return $this->domainConfig['hostname'] ?? '';
|
||||
}
|
||||
|
||||
return $domain->getAuthority();
|
||||
return sprintf('%s%s', $domain->getAuthority(), $this->basePath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,11 @@ class ShortUrlStringifierTest extends TestCase
|
||||
*/
|
||||
public function generatesExpectedOutputBasedOnConfigAndShortUrl(
|
||||
array $config,
|
||||
string $basePath,
|
||||
ShortUrl $shortUrl,
|
||||
string $expected
|
||||
): void {
|
||||
$stringifier = new ShortUrlStringifier($config);
|
||||
$stringifier = new ShortUrlStringifier($config, $basePath);
|
||||
|
||||
self::assertEquals($expected, $stringifier->stringify($shortUrl));
|
||||
}
|
||||
@@ -35,21 +36,36 @@ class ShortUrlStringifierTest extends TestCase
|
||||
]),
|
||||
);
|
||||
|
||||
yield 'no config' => [[], $shortUrlWithShortCode('foo'), 'http:/foo'];
|
||||
yield 'no config' => [[], '', $shortUrlWithShortCode('foo'), 'http:/foo'];
|
||||
yield 'hostname in config' => [
|
||||
['hostname' => 'example.com'],
|
||||
'',
|
||||
$shortUrlWithShortCode('bar'),
|
||||
'http://example.com/bar',
|
||||
];
|
||||
yield 'hostname with base path in config' => [
|
||||
['hostname' => 'example.com/foo/bar'],
|
||||
'',
|
||||
$shortUrlWithShortCode('abc'),
|
||||
'http://example.com/foo/bar/abc',
|
||||
];
|
||||
yield 'full config' => [
|
||||
['schema' => 'https', 'hostname' => 'foo.com'],
|
||||
'',
|
||||
$shortUrlWithShortCode('baz'),
|
||||
'https://foo.com/baz',
|
||||
];
|
||||
yield 'custom domain' => [
|
||||
['schema' => 'https', 'hostname' => 'foo.com'],
|
||||
'',
|
||||
$shortUrlWithShortCode('baz', 'mydom.es'),
|
||||
'https://mydom.es/baz',
|
||||
];
|
||||
yield 'custom domain with base path' => [
|
||||
['schema' => 'https', 'hostname' => 'foo.com'],
|
||||
'/foo/bar',
|
||||
$shortUrlWithShortCode('baz', 'mydom.es'),
|
||||
'https://mydom.es/foo/bar/baz',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user