diff --git a/src/Illuminate/Support/Uri.php b/src/Illuminate/Support/Uri.php index ac91120a5c6c..c43f5b0fbfe7 100644 --- a/src/Illuminate/Support/Uri.php +++ b/src/Illuminate/Support/Uri.php @@ -47,6 +47,14 @@ public static function of(UriInterface|Stringable|string $uri = ''): static return new static($uri); } + /** + * Get a URI instance of the URL for the current request. + */ + public static function current(): static + { + return new static(call_user_func(static::$urlGeneratorResolver)->current()); + } + /** * Get a URI instance of an absolute URL for the given path. */ diff --git a/tests/Support/SupportUriTest.php b/tests/Support/SupportUriTest.php index 1f86588a1eb8..c87beb888b01 100644 --- a/tests/Support/SupportUriTest.php +++ b/tests/Support/SupportUriTest.php @@ -2,11 +2,24 @@ namespace Illuminate\Tests\Support; +use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Support\Uri; use PHPUnit\Framework\TestCase; class SupportUriTest extends TestCase { + public function test_can_build_special_urls() + { + Uri::setUrlGeneratorResolver(fn () => new CustomUrlGeneratorResolver); + + $this->assertEquals('https://laravel.com/current', Uri::current()->value()); + $this->assertEquals('https://laravel.com/to', Uri::to('')->value()); + $this->assertEquals('https://laravel.com/route', Uri::route('')->value()); + $this->assertEquals('https://laravel.com/signed-route', Uri::signedRoute('')->value()); + $this->assertEquals('https://laravel.com/signed-route', Uri::temporarySignedRoute('', '')->value()); + $this->assertEquals('https://laravel.com/action', Uri::action('')->value()); + } + public function test_basic_uri_interactions() { $uri = Uri::of($originalUri = 'https://laravel.com/docs/installation'); @@ -232,3 +245,66 @@ public function test_macroable() $this->assertSame('https://laravel.com/foobar', (string) $uri->myMacro()); } } + +class CustomUrlGeneratorResolver implements UrlGenerator +{ + public function current() + { + return 'https://laravel.com/current'; + } + + public function previous($fallback = false) + { + // TODO: Implement previous() method. + } + + public function to($path, $extra = [], $secure = null) + { + return 'https://laravel.com/to'; + } + + public function secure($path, $parameters = []) + { + // TODO: Implement secure() method. + } + + public function asset($path, $secure = null) + { + // TODO: Implement asset() method. + } + + public function route($name, $parameters = [], $absolute = true) + { + return 'https://laravel.com/route'; + } + + public function signedRoute($name, $parameters = [], $expiration = null, $absolute = true) + { + return 'https://laravel.com/signed-route'; + } + + public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true) + { + return 'https://laravel.com/temporary-signed-route'; + } + + public function query($path, $query = [], $extra = [], $secure = null) + { + // TODO: Implement query() method. + } + + public function action($action, $parameters = [], $absolute = true) + { + return 'https://laravel.com/action'; + } + + public function getRootControllerNamespace() + { + // TODO: Implement getRootControllerNamespace() method. + } + + public function setRootControllerNamespace($rootNamespace) + { + // TODO: Implement setRootControllerNamespace() method. + } +}