Skip to content

Commit

Permalink
Server URL Helper Cleanup
Browse files Browse the repository at this point in the history
- Deprecate all methods and properties in the server url helper
- Introduce constructor to provide the helper with a configured url
- Deprecate existing tests to make them easy to remove in future

Signed-off-by: George Steel <[email protected]>
  • Loading branch information
gsteel committed Mar 24, 2022
1 parent b7c269a commit 977708b
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 32 deletions.
85 changes: 79 additions & 6 deletions src/Helper/ServerUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,61 @@

/**
* Helper for returning the current server URL (optionally with request URI)
*
* @psalm-suppress DeprecatedProperty,DeprecatedMethod
* @final
*/
class ServerUrl extends AbstractHelper
{
use DeprecatedAbstractHelperHierarchyTrait;

/**
* Host (including port)
*
* @deprecated since 2.21.0, this property will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @var string|null
*/
protected $host;

/**
* Port
*
* @deprecated since 2.21.0, this property will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @var int|null
*/
protected $port;

/**
* Scheme
*
* @deprecated since 2.21.0, this property will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @var string|null
*/
protected $scheme;

/**
* Whether or not to query proxy servers for address
*
* @deprecated since 2.21.0, this property will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @var bool
*/
protected $useProxy = false;

private ?string $serverUrl;

public function __construct(?string $serverUrl = null)
{
$this->serverUrl = $serverUrl;
}

/**
* View helper entry point:
* Returns the current host's URL like http://site.com
Expand All @@ -60,19 +84,28 @@ class ServerUrl extends AbstractHelper
public function __invoke($requestUri = null)
{
if ($requestUri === true) {
$path = $_SERVER['REQUEST_URI'];
} elseif (is_string($requestUri)) {
$path = $requestUri;
} else {
$path = '';
/** @var string|null $requestUri */
$requestUri = $_SERVER['REQUEST_URI'] ?? null;
}

return $this->getScheme() . '://' . $this->getHost() . $path;
$path = is_string($requestUri) ? $requestUri : '';
$serverUrl = $this->serverUrl ?: $this->legacyServerUrlDetection();

return $serverUrl . $path;
}

private function legacyServerUrlDetection(): string
{
/** @psalm-suppress DeprecatedMethod */
return $this->getScheme() . '://' . $this->getHost();
}

/**
* Detect the host based on headers
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return void
*/
protected function detectHost()
Expand Down Expand Up @@ -118,6 +151,9 @@ protected function detectHost()

/**
* Detect the port
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*/
protected function detectPort(): void
{
Expand All @@ -136,6 +172,9 @@ protected function detectPort(): void

/**
* Detect the scheme
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*/
protected function detectScheme(): void
{
Expand All @@ -158,6 +197,10 @@ protected function detectScheme(): void
$this->setScheme($scheme);
}

/**
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*/
protected function isReversedProxy(): bool
{
return isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https';
Expand All @@ -166,6 +209,9 @@ protected function isReversedProxy(): bool
/**
* Detect if a proxy is in use, and, if so, set the host based on it
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return bool
*/
protected function setHostFromProxy()
Expand Down Expand Up @@ -194,6 +240,9 @@ protected function setHostFromProxy()
/**
* Set port based on detected proxy headers
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return bool
*/
protected function setPortFromProxy()
Expand All @@ -215,6 +264,9 @@ protected function setPortFromProxy()
/**
* Set the current scheme based on detected proxy headers
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return bool
*/
protected function setSchemeFromProxy()
Expand Down Expand Up @@ -248,6 +300,9 @@ protected function setSchemeFromProxy()
/**
* Sets host
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @param string $host
* @return ServerUrl
*/
Expand All @@ -272,6 +327,9 @@ public function setHost($host)
/**
* Returns host
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return string
*/
public function getHost()
Expand All @@ -286,6 +344,9 @@ public function getHost()
/**
* Set server port
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @param int|numeric-string $port
* @return ServerUrl
*/
Expand All @@ -299,6 +360,9 @@ public function setPort($port)
/**
* Retrieve the server port
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return int|null
*/
public function getPort()
Expand All @@ -313,6 +377,9 @@ public function getPort()
/**
* Sets scheme (typically http or https)
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @param string $scheme
* @return ServerUrl
*/
Expand All @@ -326,6 +393,9 @@ public function setScheme($scheme)
/**
* Returns scheme (typically http or https)
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @return string
*/
public function getScheme()
Expand All @@ -340,6 +410,9 @@ public function getScheme()
/**
* Set flag indicating whether or not to query proxy servers
*
* @deprecated since 2.21.0, this method will be removed in version 3.0.0 of this component.
* The server url should be given to the constructor.
*
* @param bool $useProxy
* @return ServerUrl
*/
Expand Down
Loading

0 comments on commit 977708b

Please sign in to comment.