__construct() public method

Constructor. Adds config values to the public properties when a new object is created.
See also: lithium\net\http\Message::__construct()
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 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
Esempio n. 1
0
 /**
  * Adds config values to the public properties when a new object is created, pulling
  * request data from superglobals if `globals` is set to `true`.
  *
  * @param array $config Configuration options : default values are:
  *        - `'base'` _string_: null
  *        - `'url'` _string_: null
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'method'` _string_: 'GET'
  *        - `'scheme'` _string_: 'http'
  *        - `'host'` _string_: 'localhost'
  *        - `'port'` _integer_: null
  *        - `'username'` _string_: null
  *        - `'password'` _string_: null
  *        - `'path'` _string_: null
  *        - `'query'` _array_: array()
  *        - `'headers'` _array_: array()
  *        - `'type'` _string_: null
  *        - `'auth'` _mixed_: null
  *        - `'body'` _mixed_: null
  *        - `'data'` _array_: array()
  *        - `'env'` _array_: array()
  *        - `'globals'` _boolean_: true
  */
 public function __construct(array $config = array())
 {
     $defaults = array('base' => null, 'url' => null, 'env' => array(), 'query' => array(), 'data' => array(), 'globals' => true);
     $config += $defaults;
     if ($config['globals'] === true) {
         if (isset($_SERVER)) {
             $config['env'] += $_SERVER;
         }
         if (isset($_ENV)) {
             $config['env'] += $_ENV;
         }
         if (isset($_GET)) {
             $config['query'] += $_GET;
         }
         if (isset($_POST)) {
             $config['data'] += $_POST;
         }
     }
     $this->_env = $config['env'];
     if (!isset($config['host'])) {
         $config['host'] = $this->env('HTTP_HOST');
     }
     if (!isset($config['protocol'])) {
         $config['protocol'] = $this->env('SERVER_PROTOCOL');
     }
     if ($config['protocol'] && strpos($config['protocol'], '/')) {
         list($scheme, $version) = explode('/', $config['protocol']);
         $https = $this->env('HTTPS') ? 's' : '';
         $scheme = strtolower($scheme) . $https;
         if (!isset($config['scheme'])) {
             $config['scheme'] = $scheme;
         }
         if (!isset($config['version'])) {
             $config['version'] = $version;
         }
     }
     $this->_base = $this->_base($config['base']);
     $this->url = $this->_url($config['url']);
     parent::__construct($config);
     $this->headers('Content-Type', $this->env('CONTENT_TYPE'));
     $this->headers('Content-Length', $this->env('CONTENT_LENGTH'));
     foreach ($this->_env as $name => $value) {
         if (substr($name, 0, 5) == 'HTTP_') {
             $name = str_replace('_', ' ', substr($name, 5));
             $name = str_replace(' ', '-', ucwords(strtolower($name)));
             $this->headers($name, $value);
         }
     }
 }
Esempio n. 2
0
 /**
  * Adds config values to the public properties when a new object is created, pulling
  * request data from superglobals if `globals` is set to `true`.
  *
  * @param array $config Configuration options : default values are:
  *        - `'base'` _string_: null
  *        - `'url'` _string_: null
  *        - `'protocol'` _string_: null
  *        - `'version'` _string_: '1.1'
  *        - `'method'` _string_: 'GET'
  *        - `'scheme'` _string_: 'http'
  *        - `'host'` _string_: 'localhost'
  *        - `'port'` _integer_: null
  *        - `'username'` _string_: null
  *        - `'password'` _string_: null
  *        - `'path'` _string_: null
  *        - `'query'` _array_: array()
  *        - `'headers'` _array_: array()
  *        - `'type'` _string_: null
  *        - `'auth'` _mixed_: null
  *        - `'body'` _mixed_: null
  *        - `'data'` _array_: array()
  *        - `'env'` _array_: array()
  *        - `'globals'` _boolean_: true
  */
 public function __construct(array $config = array())
 {
     $defaults = array('base' => null, 'url' => null, 'env' => array(), 'query' => array(), 'data' => array(), 'globals' => true);
     $config += $defaults;
     if ($config['globals'] === true) {
         if (isset($_SERVER)) {
             $config['env'] += $_SERVER;
         }
         if (isset($_ENV)) {
             $config['env'] += $_ENV;
         }
         if (isset($_GET)) {
             $config['query'] += $_GET;
         }
         if (isset($_POST)) {
             $config['data'] += $_POST;
         }
     }
     $this->_env = $config['env'];
     if (!isset($config['host'])) {
         $config['host'] = $this->env('HTTP_HOST');
     }
     if (!isset($config['protocol'])) {
         $config['protocol'] = $this->env('SERVER_PROTOCOL');
     }
     if ($config['protocol'] && strpos($config['protocol'], '/')) {
         list($scheme, $version) = explode('/', $config['protocol']);
         $https = $this->env('HTTPS') ? 's' : '';
         $scheme = strtolower($scheme) . $https;
         if (!isset($config['scheme'])) {
             $config['scheme'] = $scheme;
         }
         if (!isset($config['version'])) {
             $config['version'] = $version;
         }
     }
     $this->_base = $this->_base($config['base']);
     $this->url = $this->_url($config['url']);
     parent::__construct($config);
 }