Centralized how routes are configured to support multi-segment slugs

This commit is contained in:
Alejandro Celaya
2022-08-06 09:30:13 +02:00
parent 4629f1b03f
commit 16bd368a58
10 changed files with 113 additions and 46 deletions

View File

@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Config;
use function Functional\map;
use function str_replace;
class MultiSegmentSlugProcessor
{
private const SINGLE_SHORT_CODE_PATTERN = '{shortCode}';
private const MULTI_SHORT_CODE_PATTERN = '{shortCode:.+}';
public function __invoke(array $config): array
{
$multiSegmentEnabled = $config['url_shortener']['multi_segment_slugs_enabled'] ?? false;
if (! $multiSegmentEnabled) {
return $config;
}
$config['routes'] = map($config['routes'] ?? [], static function (array $route): array {
['path' => $path] = $route;
$route['path'] = str_replace(self::SINGLE_SHORT_CODE_PATTERN, self::MULTI_SHORT_CODE_PATTERN, $path);
return $route;
});
return $config;
}
}