|
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|
|
namespace App\Service\Utils;
|
|
namespace App\Service\Utils;
|
|
|
|
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator;
|
|
use Symfony\Component\Routing\Generator\UrlGenerator;
|
|
|
-use Symfony\Component\Routing\RequestContext;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Building url utilities
|
|
* Building url utilities
|
|
@@ -12,20 +11,20 @@ use Symfony\Component\Routing\RequestContext;
|
|
|
class UrlBuilder
|
|
class UrlBuilder
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
- public function __construct(private RequestContext $requestContext){}
|
|
|
|
|
|
|
+ public function __construct(private string $baseUrl){}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Concatenate a base url and a path
|
|
* Concatenate a base url and a path
|
|
|
*
|
|
*
|
|
|
- * @param string $url The base url
|
|
|
|
|
- * @param string $path The following path
|
|
|
|
|
|
|
+ * @param string $base The base url
|
|
|
|
|
+ * @param array $tails La suite de l'URL sous forme de tableau
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
public static function concatPath(string $base, array $tails): string
|
|
public static function concatPath(string $base, array $tails): string
|
|
|
{
|
|
{
|
|
|
$url = $base;
|
|
$url = $base;
|
|
|
foreach ($tails as $tail){
|
|
foreach ($tails as $tail){
|
|
|
- $url = preg_replace('/^\/|\/$/i', '', $url) . '/' . preg_replace('/^\/?|\/?$/i', '', strval($tail));
|
|
|
|
|
|
|
+ $url = trim($url) . '/' . trim(strval($tail));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
return $url;
|
|
@@ -77,14 +76,14 @@ class UrlBuilder
|
|
|
* Build an url
|
|
* Build an url
|
|
|
*
|
|
*
|
|
|
* @param string $url The base url
|
|
* @param string $url The base url
|
|
|
- * @param string $path A path to append (can be an empty string)
|
|
|
|
|
|
|
+ * @param array $tails la suite de l'url sous forme de tableau
|
|
|
* @param list<string> $parameters A list of parameters (can be an empty array)
|
|
* @param list<string> $parameters A list of parameters (can be an empty array)
|
|
|
* @param bool $preprendHttps Should the 'https://' be prepended if missing
|
|
* @param bool $preprendHttps Should the 'https://' be prepended if missing
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
- public static function concat(string $url, string $path, array $parameters, bool $preprendHttps = false): string
|
|
|
|
|
|
|
+ public static function concat(string $url, array $tails, array $parameters, bool $preprendHttps = false): string
|
|
|
{
|
|
{
|
|
|
- $url = self::concatParameters(self::concatPath($url, $path), $parameters);
|
|
|
|
|
|
|
+ $url = self::concatParameters(self::concatPath($url, $tails), $parameters);
|
|
|
if ($preprendHttps) {
|
|
if ($preprendHttps) {
|
|
|
$url = self::prependHttps($url);
|
|
$url = self::prependHttps($url);
|
|
|
}
|
|
}
|
|
@@ -97,8 +96,7 @@ class UrlBuilder
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
public function getRelativeUrl(string $path): string{
|
|
public function getRelativeUrl(string $path): string{
|
|
|
- $baseUrl = sprintf('%s://%s/', $this->requestContext->getScheme(), $this->requestContext->getHost() );
|
|
|
|
|
- return UrlGenerator::getRelativePath($baseUrl, $path);
|
|
|
|
|
|
|
+ return UrlGenerator::getRelativePath($this->baseUrl, $path);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -107,6 +105,6 @@ class UrlBuilder
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
public function getAbsoluteUrl(string $path): string{
|
|
public function getAbsoluteUrl(string $path): string{
|
|
|
- return sprintf('%s://%s/%s', $this->requestContext->getScheme(), $this->requestContext->getHost(), $path );
|
|
|
|
|
|
|
+ return self::concat($this->baseUrl, [$path], []);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|