Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Url;
use InvalidArgumentException;
use League\Flysystem\FilesystemAdapter as FlysystemAdapter;
use League\Flysystem\FilesystemOperator;
Expand Down Expand Up @@ -790,12 +791,12 @@ protected function concatPathToUrl($url, $path)
*/
protected function replaceBaseUrl($uri, $url)
{
$parsed = parse_url($url);
$parsed = Url::parse($url);

return $uri
->withScheme($parsed['scheme'])
->withHost($parsed['host'])
->withPort($parsed['port'] ?? null);
->withScheme($parsed->scheme)
->withHost($parsed->host)
->withPort($parsed->port);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Illuminate/Foundation/Bootstrap/SetRequestForConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Url;

class SetRequestForConsole
{
Expand All @@ -17,14 +18,14 @@ public function bootstrap(Application $app)
{
$uri = $app->make('config')->get('app.url', 'http://localhost');

$components = parse_url($uri);
$components = Url::parse($uri);

$server = $_SERVER;

if (isset($components['path'])) {
if ($components->path) {
$server = array_merge($server, [
'SCRIPT_FILENAME' => $components['path'],
'SCRIPT_NAME' => $components['path'],
'SCRIPT_FILENAME' => $components->path,
'SCRIPT_NAME' => $components->path,
]);
}

Expand Down
119 changes: 119 additions & 0 deletions src/Illuminate/Support/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Illuminate\Support;

use Illuminate\Contracts\Support\Arrayable;
use RuntimeException;
use Stringable;

class Url implements Arrayable, Stringable
{
/**
* Constructor.
*/
public function __construct(
public ?string $scheme = null,
public ?string $host = null,
public ?int $port = null,
public ?string $user = null,
#[\SensitiveParameter]
public ?string $pass = null,
public ?string $path = null,
public ?UrlQueryParameters $query = null,
public ?string $fragment = null,
) {
$this->query ??= new UrlQueryParameters;
}

/**
* Parse a URL string into a URL object.
*
* @param string $url
* @return static
*/
public static function parse(string $url): static
{
$components = parse_url($url);

if ($components === false) {
throw new RuntimeException("Invalid URL [$url].");
}

if (isset($components['query'])) {
$components['query'] = static::query($components['query']);
}

return new static(...$components);
}

/**
* Parse URL query parameters.
*
* @return \Illuminate\Support\UrlQueryParameters
*/
protected static function query(?string $query): UrlQueryParameters
{
return UrlQueryParameters::parse($query);
}

/**
* Convert the URL object to an array.
*
* @return array<string, string|null>
*/
public function toArray(): array
{
return [
'scheme' => $this->scheme,
'host' => $this->host,
'port' => $this->port,
'user' => $this->user,
'pass' => $this->pass,
'path' => $this->path,
'query' => (string) $this->query,
'fragment' => $this->fragment,
];
}

/**
* Convert the URL object to a string.
*
* @return string
*/
public function __toString(): string
{
$url = '';

if ($this->scheme) {
$url .= $this->scheme.':';
}

$url .= '//';

if ($this->user) {
$url .= $this->user.($this->pass ? ':'.$this->pass : '').'@';
}

if ($this->host) {
$url .= $this->host;
}

if ($this->port) {
$url .= ':'.$this->port;
}

if ($this->path) {
$url .= $this->path;
}

if ($this->query) {
$url .= '?'.$this->query;
}

if ($this->fragment) {
$url .= '#'.$this->fragment;
}

return $url;
}
}
190 changes: 190 additions & 0 deletions src/Illuminate/Support/UrlQueryParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

namespace Illuminate\Support;

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Stringable;

class UrlQueryParameters implements Arrayable, ArrayAccess, Stringable
{
/**
* Constructor.
*
* @param array<string, string> $parameters
* @return void
*/
public function __construct(
protected array $parameters = []
) {
}

/**
* Parse a query string.
*
* @param string|null $query
* @return static
*/
public static function parse(?string $query): static
{
if (is_null($query)) {
return new static;
}

parse_str(
Str::of($query)
->chopStart('?')
->toString(),
$parameters
);

return new static($parameters);
}

/**
* Get a parameter value.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->parameters[$key] ?? $default;
}

/**
* Set a parameter value.
*
* @param string $key
* @param string $value
* @return $this
*/
public function set(string $key, string $value): static
{
$this->parameters[$key] = $value;

return $this;
}

/**
* Remove a parameter.
*
* @param string $key
* @return $this
*/
public function forget(string $key): static
{
unset($this->parameters[$key]);

return $this;
}

/**
* Clear all parameters.
*
* @return $this
*/
public function clear(): static
{
$this->parameters = [];

return $this;
}

/**
* Check if a parameter exists.
*
* @param string $key
* @return bool
*/
public function has(string $key): bool
{
return isset($this->parameters[$key]);
}

/**
* Get all parameters.
*
* @return array<string, mixed>
*/
public function all(): array
{
return $this->parameters;
}

/**
* Determine if the parameters are empty.
*
* @return bool
*/
public function isEmpty(): bool
{
return empty($this->parameters);
}

/**
* Get the instance as an array.
*
* @return array<string, string>
*/
public function toArray(): array
{
return $this->all();
}

/**
* Convert the query parameters to a string.
*
* @return string
*/
public function __toString(): string
{
return Arr::query($this->parameters);
}

/**
* Determine if a parameter exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->parameters[$offset]);
}

/**
* Get a parameter value.
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet(mixed $offset): mixed
{
return $this->parameters[$offset];
}

/**
* Set a parameter value.
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->parameters[$offset] = $value;
}

/**
* Remove a parameter.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
unset($this->parameters[$offset]);
}
}
Loading