Open easy-system opened 8 years ago
And for the host is must be required! forced host... This is not such a rare situation. And it should be done without interfering with the application logic, the best from the index.php
use DomainException;
class ServerHostFactory
{
protected static $forcedHost = '';
public static function setForcedHost($host)
{
static::$forcedHost = $host;
}
public static function getForcedHost()
{
return static::$forcedHost;
}
public static function make(array $server = null)
{
if (! empty(static::$forcedHost)) {
return static::$forcedHost;
}
return static::calculate($server);
}
public static function calculate(array $server = null)
{
if (empty($server)) {
$server = $_SERVER;
}
// from server config, if present
if (isset($server['SERVER_NAME'])) {
return $server['SERVER_NAME'];
}
// from headers, dangerous to use
/* if (isset($server['HTTP_HOST'])) {
return $server['HTTP_HOST'];
}*/
throw new DomainException(
'Missing host in server parameters.'
);
}
}
Ok. Im delete this from public. P.S.: reported
This repository has been closed and moved to laminas/laminas-diactoros; a new issue has been opened at https://github.com/laminas/laminas-diactoros/issues/23.
This all works fine. Great. But why all the logic is concentrated in one place? Moreover, there is a class of Server. I think it would be better to make a directory of the factories. Firstly, it allows to separate each logic element in its factory. Secondly, because I can always get in the code of the original values. For example, Zend\Diactoros\Factory\HttpProtocolFactory:
All this is true for the request method, headers, Uri, uploaded files etc