Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServerRequestFactory #23

Closed
weierophinney opened this issue Dec 31, 2019 · 3 comments
Closed

ServerRequestFactory #23

weierophinney opened this issue Dec 31, 2019 · 3 comments

Comments

@weierophinney
Copy link
Member

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:

use UnexpectedValueException;

class HttpProtocolFactory
{
    public static function make(array $server = null)
    {
        if (empty($server)) {
            $server = $_SERVER;
        }

        if (! isset($server['SERVER_PROTOCOL'])) {
            return '1.1';
        }

        $protocol = $server['SERVER_PROTOCOL'];

        if (! preg_match('#\A(?:HTTP/)?(?P<version>\d{1}\.\d+)\Z#', $protocol, $matches)) {
            throw new UnexpectedValueException(sprintf(
                'Unrecognized protocol version "%s".',
                $server['SERVER_PROTOCOL']
            ));
        }

        return $matches['version'];
    }
}

All this is true for the request method, headers, Uri, uploaded files etc


Originally posted by @easy-system at zendframework/zend-diactoros#158

@weierophinney
Copy link
Member Author

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.'
        );
    }
}

Originally posted by @easy-system at zendframework/zend-diactoros#158 (comment)

@weierophinney
Copy link
Member Author

Ok. Im delete this from public.
P.S.: reported


Originally posted by @easy-system at zendframework/zend-diactoros#158 (comment)

@Xerkus
Copy link
Member

Xerkus commented May 1, 2023

No longer relevant.

@Xerkus Xerkus closed this as completed May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants