| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Illuminate\Contracts\Routing\UrlRoutable; |
| 7 |
use Illuminate\Contracts\Support\Htmlable; |
| 8 |
use Illuminate\Contracts\Support\Responsable; |
| 9 |
use Illuminate\Http\RedirectResponse; |
| 10 |
use Illuminate\Support\Traits\Conditionable; |
| 11 |
use Illuminate\Support\Traits\Dumpable; |
| 12 |
use Illuminate\Support\Traits\Macroable; |
| 13 |
use Illuminate\Support\Traits\Tappable; |
| 14 |
use JsonSerializable; |
| 15 |
use League\Uri\Contracts\UriInterface; |
| 16 |
use League\Uri\Uri as LeagueUri; |
| 17 |
use SensitiveParameter; |
| 18 |
use Stringable; |
| 19 |
|
| 20 |
class Uri implements Htmlable, JsonSerializable, Responsable, Stringable |
| 21 |
{ |
| 22 |
use Conditionable, Dumpable, Macroable, Tappable; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
protected UriInterface $uri; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected static ?Closure $urlGeneratorResolver = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct(UriInterface|Stringable|string $uri = '') |
| 38 |
{ |
| 39 |
$this->uri = $uri instanceof UriInterface ? $uri : LeagueUri::new((string) $uri); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function of(UriInterface|Stringable|string $uri = ''): static |
| 46 |
{ |
| 47 |
return new static($uri); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static function to(string $path): static |
| 54 |
{ |
| 55 |
return new static(call_user_func(static::$urlGeneratorResolver)->to($path)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static function route($name, $parameters = [], $absolute = true): static |
| 69 |
{ |
| 70 |
return new static(call_user_func(static::$urlGeneratorResolver)->route($name, $parameters, $absolute)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static function signedRoute($name, $parameters = [], $expiration = null, $absolute = true): static |
| 85 |
{ |
| 86 |
return new static(call_user_func(static::$urlGeneratorResolver)->signedRoute($name, $parameters, $expiration, $absolute)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true): static |
| 99 |
{ |
| 100 |
return static::signedRoute($name, $parameters, $expiration, $absolute); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static function action($action, $parameters = [], $absolute = true): static |
| 114 |
{ |
| 115 |
return new static(call_user_func(static::$urlGeneratorResolver)->action($action, $parameters, $absolute)); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function authority(): ?string |
| 122 |
{ |
| 123 |
return $this->uri->getAuthority(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function scheme(): ?string |
| 130 |
{ |
| 131 |
return $this->uri->getScheme(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function user(bool $withPassword = false): ?string |
| 138 |
{ |
| 139 |
return $withPassword |
| 140 |
? $this->uri->getUserInfo() |
| 141 |
: $this->uri->getUsername(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function password(): ?string |
| 148 |
{ |
| 149 |
return $this->uri->getPassword(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function host(): ?string |
| 156 |
{ |
| 157 |
return $this->uri->getHost(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function port(): ?int |
| 164 |
{ |
| 165 |
return $this->uri->getPort(); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function path(): string |
| 176 |
{ |
| 177 |
$path = trim((string) $this->uri->getPath(), '/'); |
| 178 |
|
| 179 |
return $path === '' ? '/' : $path; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function pathSegments(): Collection |
| 188 |
{ |
| 189 |
$path = $this->path(); |
| 190 |
|
| 191 |
return $path === '/' ? new Collection : new Collection(explode('/', $path)); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function query(): UriQueryString |
| 198 |
{ |
| 199 |
return new UriQueryString($this); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function fragment(): ?string |
| 206 |
{ |
| 207 |
return $this->uri->getFragment(); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function withScheme(Stringable|string $scheme): static |
| 214 |
{ |
| 215 |
return new static($this->uri->withScheme($scheme)); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function withUser(Stringable|string|null $user, #[SensitiveParameter] Stringable|string|null $password = null): static |
| 222 |
{ |
| 223 |
return new static($this->uri->withUserInfo($user, $password)); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function withHost(Stringable|string $host): static |
| 230 |
{ |
| 231 |
return new static($this->uri->withHost($host)); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function withPort(?int $port): static |
| 238 |
{ |
| 239 |
return new static($this->uri->withPort($port)); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function withPath(Stringable|string $path): static |
| 246 |
{ |
| 247 |
return new static($this->uri->withPath(Str::start((string) $path, '/'))); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function withQuery(array $query, bool $merge = true): static |
| 254 |
{ |
| 255 |
foreach ($query as $key => $value) { |
| 256 |
if ($value instanceof UrlRoutable) { |
| 257 |
$query[$key] = $value->getRouteKey(); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if ($merge) { |
| 262 |
$mergedQuery = $this->query()->all(); |
| 263 |
|
| 264 |
foreach ($query as $key => $value) { |
| 265 |
data_set($mergedQuery, $key, $value); |
| 266 |
} |
| 267 |
|
| 268 |
$newQuery = $mergedQuery; |
| 269 |
} else { |
| 270 |
$newQuery = []; |
| 271 |
|
| 272 |
foreach ($query as $key => $value) { |
| 273 |
data_set($newQuery, $key, $value); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return new static($this->uri->withQuery(Arr::query($newQuery) ?: null)); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function withQueryIfMissing(array $query): static |
| 284 |
{ |
| 285 |
$currentQuery = $this->query(); |
| 286 |
|
| 287 |
foreach ($query as $key => $value) { |
| 288 |
if (! $currentQuery->missing($key)) { |
| 289 |
Arr::forget($query, $key); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return $this->withQuery($query); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public function pushOntoQuery(string $key, mixed $value): static |
| 300 |
{ |
| 301 |
$currentValue = data_get($this->query()->all(), $key); |
| 302 |
|
| 303 |
$values = Arr::wrap($value); |
| 304 |
|
| 305 |
return $this->withQuery([$key => match (true) { |
| 306 |
is_array($currentValue) && array_is_list($currentValue) => array_values(array_unique([...$currentValue, ...$values])), |
| 307 |
is_array($currentValue) => [...$currentValue, ...$values], |
| 308 |
! is_null($currentValue) => [$currentValue, ...$values], |
| 309 |
default => $values, |
| 310 |
}]); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function withoutQuery(array|string $keys): static |
| 317 |
{ |
| 318 |
return $this->replaceQuery(Arr::except($this->query()->all(), $keys)); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public function replaceQuery(array $query): static |
| 325 |
{ |
| 326 |
return $this->withQuery($query, merge: false); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function withFragment(string $fragment): static |
| 333 |
{ |
| 334 |
return new static($this->uri->withFragment($fragment)); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public function redirect(int $status = 302, array $headers = []): RedirectResponse |
| 341 |
{ |
| 342 |
return new RedirectResponse($this->value(), $status, $headers); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function toStringable() |
| 351 |
{ |
| 352 |
return Str::of($this->value()); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function toResponse($request) |
| 362 |
{ |
| 363 |
return new RedirectResponse($this->value()); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function toHtml() |
| 372 |
{ |
| 373 |
return $this->value(); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public function decode(): string |
| 380 |
{ |
| 381 |
if (empty($this->query()->toArray())) { |
| 382 |
return $this->value(); |
| 383 |
} |
| 384 |
|
| 385 |
return Str::replace(Str::after($this->value(), '?'), $this->query()->decode(), $this->value()); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public function value(): string |
| 392 |
{ |
| 393 |
return (string) $this; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public function isEmpty(): bool |
| 400 |
{ |
| 401 |
return trim($this->value()) === ''; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public function dump(...$args) |
| 411 |
{ |
| 412 |
dump($this->value(), ...$args); |
| 413 |
|
| 414 |
return $this; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public static function setUrlGeneratorResolver(Closure $urlGeneratorResolver): void |
| 421 |
{ |
| 422 |
static::$urlGeneratorResolver = $urlGeneratorResolver; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function getUri(): UriInterface |
| 429 |
{ |
| 430 |
return $this->uri; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public function jsonSerialize(): string |
| 439 |
{ |
| 440 |
return $this->value(); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public function __toString(): string |
| 447 |
{ |
| 448 |
return $this->uri->toString(); |
| 449 |
} |
| 450 |
} |