__construct() public method

Constructor. Adds config values to the public properties when a new object is created.
See also: lithium\net\Message::__construct()
public __construct ( array $config = [] ) : void
$config array The available configuration options are the following. Further options are inherited from the parent class. - `'protocol'` _string_: Defaults to `null`. - `'version'` _string_: Defaults to `'1.1'`. - `'scheme'` _string_: Overridden and defaulting to `'http'`. - `'headers'` _array_: Defaults to `array()`.
return void
Exemplo n.º 1
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config Configuration options : default value
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'headers'` _array_: array()
  *        - `'body'` _mixed_: null
  *        - `'message'` _string_: null
  *        - `'status'` _mixed_: null
  *        - `'type'` _string_: null
  */
 public function __construct(array $config = array())
 {
     $defaults = array('message' => null, 'status' => null, 'type' => null, 'cookies' => null);
     parent::__construct($config + $defaults);
     if ($this->_config['message']) {
         $this->body = $this->_parseMessage($this->_config['message']);
     }
     if ($this->headers('Transfer-Encoding')) {
         $this->body = $this->_httpChunkedDecode($this->body);
     }
     if ($status = $this->_config['status']) {
         $this->status($status);
     }
     if ($cookies = $this->headers('Set-Cookie')) {
         $this->_parseCookies($cookies);
     }
     if ($cookies = $this->_config['cookies']) {
         $this->cookies($cookies);
     }
     if ($type = $this->_config['type']) {
         $this->type($type);
     }
     if (!($header = $this->headers('Content-Type'))) {
         return;
     }
     $header = is_array($header) ? end($header) : $header;
     preg_match('/([-\\w\\/\\.+]+)(;\\s*?charset=(.+))?/i', $header, $match);
     if (isset($match[1])) {
         $this->type(trim($match[1]));
     }
     if (isset($match[3])) {
         $this->encoding = strtoupper(trim($match[3]));
     }
 }
Exemplo n.º 2
0
 /**
  * Constructor. Adds config values to the public properties when a new object is created.
  *
  * @see lithium\net\http\Message::__construct()
  * @see lithium\net\Message::__construct()
  * @param 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`.
  * @return void
  */
 public function __construct(array $config = array())
 {
     $defaults = array('method' => 'GET', 'query' => array(), 'cookies' => array(), '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 = array('Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host, 'Connection' => 'Close', 'User-Agent' => 'Mozilla/5.0');
     foreach (array('type', 'headers', 'cookies') as $field) {
         if ($value = $this->_config[$field]) {
             $this->{$field}($value);
         }
     }
     if ($cookies = $this->headers('Cookie')) {
         $this->_parseCookies($cookies);
     }
     $this->_formats += array('url' => function ($req, $options) {
         $options['port'] = $options['port'] ? ":{$options['port']}" : '';
         $options['path'] = str_replace('//', '/', $options['path']);
         return String::insert("{:scheme}://{:host}{:port}{:path}{:query}", $options);
     }, 'context' => function ($req, $options, $defaults) {
         $req->headers($options['headers']);
         return array('http' => array_diff_key($options, $defaults) + array('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", array($status, join("\r\n", $req->headers()), "", $body));
     });
 }
Exemplo n.º 3
0
	/**
	 * Adds config values to the public properties when a new object is created.
	 *
	 * @param array $config Configuration options : default value
	 * - `scheme`: http
	 * - `host`: localhost
	 * - `port`: null
	 * - `username`: null
	 * - `password`: null
	 * - `path`: null
	 * - `query`: array - after the question mark ?
	 * - `fragment`: null - after the hashmark #
	 * - `auth` - the Authorization method (Basic|Digest)
	 * - `method` - GET
	 * - `version`: 1.1
	 * - `headers`: array
	 * - `body`: null
	 */
	public function __construct(array $config = array()) {
		$defaults = array(
			'scheme' => 'http',
			'host' => 'localhost',
			'port' => null,
			'username' => null,
			'password' => null,
			'path' => null,
			'query' => array(),
			'fragment' => null,
			'headers' => array(),
			'body' => null,
			'auth' => null,
			'method' => 'GET'
		);
		$config += $defaults;
		parent::__construct($config);

		$this->headers = array(
			'Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host,
			'Connection' => 'Close',
			'User-Agent' => 'Mozilla/5.0'
		);
		$this->headers($config['headers']);
	}
Exemplo n.º 4
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config
  */
 public function __construct(array $config = array())
 {
     $defaults = array('message' => null, 'type' => null);
     parent::__construct($config + $defaults);
     if ($this->_config['message']) {
         $this->body = $this->_parseMessage($this->_config['message']);
     }
     if (isset($this->headers['Transfer-Encoding'])) {
         $this->body = $this->_httpChunkedDecode($this->body);
     }
     if ($type = $this->_config['type']) {
         $this->type($type);
     }
     if (!isset($this->headers['Content-Type'])) {
         return;
     }
     $pattern = '/([-\\w\\/\\.+]+)(;\\s*?charset=(.+))?/i';
     preg_match($pattern, $this->headers['Content-Type'], $match);
     if (isset($match[1])) {
         $this->type(trim($match[1]));
     }
     if (isset($match[3])) {
         $this->encoding = strtoupper(trim($match[3]));
     }
 }
Exemplo n.º 5
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config Configuration options : default value
  * - `scheme`: http
  * - `host`: localhost
  * - `port`: null
  * - `username`: null
  * - `password`: null
  * - `path`: null
  * - `query`: array - after the question mark ?
  * - `fragment`: null - after the hashmark #
  * - `auth` - the Authorization method (Basic|Digest)
  * - `method` - GET
  * - `type`: null
  * - `version`: 1.1
  * - `headers`: array
  * - `body`: null
  */
 public function __construct(array $config = array())
 {
     $defaults = array('scheme' => 'http', 'host' => 'localhost', 'port' => null, 'username' => null, 'password' => null, 'path' => null, 'query' => array(), 'fragment' => null, 'headers' => array(), 'body' => null, 'auth' => null, 'method' => 'GET', 'type' => null, 'proxy' => null, 'ignoreErrors' => true, 'followLocation' => true);
     parent::__construct($config + $defaults);
     $this->method = $this->method ?: $this->_config['method'];
     $this->headers = array('Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host, 'Connection' => 'Close', 'User-Agent' => 'Mozilla/5.0');
     if ($type = $this->_config['type']) {
         $this->type($type);
     }
     $this->headers($this->_config['headers']);
 }
Exemplo n.º 6
0
 /**
  * Constructor
  *
  * @param array $config
  *        - auth: the Authorization method (Basic|Digest)
  *        - username: the username for auth
  *        - password: the password for auth
  * @return object
  */
 public function __construct(array $config = array())
 {
     $defaults = array('scheme' => 'http', 'host' => 'localhost', 'port' => null, 'method' => 'GET', 'path' => '/', 'auth' => null, 'headers' => array(), 'body' => array(), 'params' => array());
     $config += $defaults;
     parent::__construct($config);
     $this->protocol = "HTTP/{$this->version}";
     $this->headers = array('Host' => $this->port ? "{$this->host}:{$this->port}" : $this->host, 'Connection' => 'Close', 'User-Agent' => 'Mozilla/5.0');
     $this->headers($config['headers']);
     if (strpos($this->host, '/') !== false) {
         $parts = explode('/', $this->host, 2);
         $this->host = $parts[0];
         $this->path = str_replace('//', '/', "/{$parts[1]}/");
     }
 }
Exemplo n.º 7
0
 /**
  * Adds config values to the public properties when a new object is created.
  *
  * @param array $config
  */
 public function __construct(array $config = array())
 {
     $defaults = array('message' => null);
     $config += $defaults;
     parent::__construct($config);
 }