Skip to content

Commit 6e6ecfc

Browse files
committed
Improve UriTemplate URI extension detection
1 parent e4f8653 commit 6e6ecfc

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

uri/UriTemplate.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Deprecated;
1717
use League\Uri\Contracts\UriException;
1818
use League\Uri\Contracts\UriInterface;
19+
use League\Uri\Exceptions\MissingFeature;
1920
use League\Uri\Exceptions\SyntaxError;
2021
use League\Uri\UriTemplate\Template;
2122
use League\Uri\UriTemplate\TemplateCanNotBeExpanded;
@@ -30,6 +31,7 @@
3031

3132
use function array_fill_keys;
3233
use function array_key_exists;
34+
use function class_exists;
3335

3436
/**
3537
* Defines the URI Template syntax and the process for expanding a URI Template into a URI reference.
@@ -138,22 +140,28 @@ public function expand(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable
138140
}
139141

140142
/**
143+
* @throws MissingFeature if no Uri\Rfc3986\Uri class is found
141144
* @throws TemplateCanNotBeExpanded if the variables are invalid
142145
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Rfc3986\Uri instance
143146
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
144147
*/
145148
public function expandToUri(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): Rfc3986Uri
146149
{
150+
class_exists(Rfc3986Uri::class) || throw new MissingFeature('Support for '.Rfc3986Uri::class.' requires PHP8.5+ or a polyfill. Run "composer require league/uri-polyfill" or use you owm polyfill.');
151+
147152
return new Rfc3986Uri($this->templateExpanded($variables), $this->newRfc3986Uri($baseUri));
148153
}
149154

150155
/**
156+
* @throws MissingFeature if no Uri\Whatwg\Url class is found
151157
* @throws TemplateCanNotBeExpanded if the variables are invalid
152158
* @throws InvalidUrlException if the base URI cannot be converted to a Uri\Whatwg\Url instance
153159
* @throws InvalidUrlException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
154160
*/
155161
public function expandToUrl(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUrl = null): WhatWgUrl
156162
{
163+
class_exists(WhatWgUrl::class) || throw new MissingFeature('Support for '.Rfc3986Uri::class.' requires PHP8.5+ or a polyfill. Run "composer require league/uri-polyfill" or use you owm polyfill.');
164+
157165
return new WhatWgUrl($this->templateExpanded($variables), $this->newWhatWgUrl($baseUrl));
158166
}
159167

@@ -191,22 +199,28 @@ public function expandOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stri
191199
}
192200

193201
/**
202+
* @throws MissingFeature if no Uri\Rfc3986\Uri class is found
194203
* @throws TemplateCanNotBeExpanded if the variables are invalid
195204
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Rfc3986\Uri instance
196205
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
197206
*/
198207
public function expandToUriOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): Rfc3986Uri
199208
{
209+
class_exists(Rfc3986Uri::class) || throw new MissingFeature('Support for '.Rfc3986Uri::class.' requires PHP8.5+ or a polyfill. Run "composer require league/uri-polyfill" or use you owm polyfill.');
210+
200211
return new Rfc3986Uri($this->templateExpandedOrFail($variables), $this->newRfc3986Uri($baseUri));
201212
}
202213

203214
/**
215+
* @throws MissingFeature if no Uri\Whatwg\Url class is found
204216
* @throws TemplateCanNotBeExpanded if the variables are invalid
205217
* @throws InvalidUrlException if the base URI cannot be converted to a Uri\Whatwg\Url instance
206218
* @throws InvalidUrlException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
207219
*/
208220
public function expandToUrlOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUrl = null): WhatWgUrl
209221
{
222+
class_exists(WhatWgUrl::class) || throw new MissingFeature('Support for '.Rfc3986Uri::class.' requires PHP8.5+ or a polyfill. Run "composer require league/uri-polyfill" or use you owm polyfill.');
223+
210224
return new WhatWgUrl($this->templateExpandedOrFail($variables), $this->newWhatWgUrl($baseUrl));
211225
}
212226

0 commit comments

Comments
 (0)