lithium\net\http\Request::__construct()

public method

Constructor. Adds config values to the public properties when a new object is created.

Parameters

  • array $config

    The available configuration options are the following. Further options are inherited from the parent classes.

    • 'method' string: Defaults to 'GET'.
    • 'path' string: Defaults to null.
    • 'query' array: Defaults to array().
    • 'cookies' array: Defaults to array().
    • 'type' string: Defaults to null.
    • 'auth' mixed: Defaults to null.
    • 'proxy' string: Defaults to null.
    • 'ignoreErrors' boolean: Defaults to true.
    • 'followLocation' boolean: Defaults to true.

Returns

void

Source

	public function __construct(array $config = []) {
		$defaults = [
			'method' => 'GET',
			'query' => [],
			'cookies' => [],
			'type' => null,
			'auth' => null,
			'proxy' => null,
			'ignoreErrors' => true,
			'followLocation' => true
		];
		$config += $defaults;

		$this->method  = $config['method'];
		$this->query   = $config['query'];
		$this->auth    = $config['auth'];
		parent::__construct($config);

		$this->headers = [
			'Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host,
			'Connection' => 'Close',
			'User-Agent' => 'Mozilla/5.0'
		];
		foreach (['type', 'headers', 'cookies'] as $field) {
			if ($value = $this->_config[$field]) {
				$this->{$field}($value);
			}
		}
		if ($cookies = $this->headers('Cookie')) {
			$this->_parseCookies($cookies);
		}

		$this->_formats += [
			'url' => function($req, $options) {
				$options['port'] = $options['port'] ? ":{$options['port']}" : '';
				$options['path'] = str_replace('//', '/', $options['path']);

				return Text::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options);
			},
			'context' => function($req, $options, $defaults) {
				$req->headers($options['headers']);

				return ['http' => array_diff_key($options, $defaults) + [
					'content' => $req->body(),
					'method' => $options['method'],
					'header' => $req->headers(),
					'protocol_version' => $options['version'],
					'ignore_errors' => $options['ignore_errors'],
					'follow_location' => $options['follow_location'],
					'request_fulluri' => $options['request_fulluri'],
					'proxy' => $options['proxy']
				]];
			},
			'string' => function($req, $options) {
				$body = $req->body();
				$path = str_replace('//', '/', $options['path']) . $options['query'];
				$status = "{$options['method']} {$path} {$req->protocol}";

				return join("\r\n", [$status, join("\r\n", $req->headers()), "", $body]);
			}
		];
	}