File indexing completed on 2024-04-28 17:09:58

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_Http
0010  * @subpackage  Server
0011  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0012  * @copyright   Akira Ohgaki
0013  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0014  * @link        https://github.com/akiraohgaki/flooer
0015  */
0016 
0017 /**
0018  * Usage
0019  *
0020  * $server = new Flooer_Http_Server();
0021  * echo $server->baseUri;
0022  */
0023 
0024 /**
0025  * Server and execution environment information class
0026  *
0027  * @category    Flooer
0028  * @package     Flooer_Http
0029  * @subpackage  Server
0030  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0031  */
0032 class Flooer_Http_Server
0033 {
0034 
0035     /**
0036      * Configuration options
0037      *
0038      * @var     array
0039      */
0040     protected $_config = array(
0041         'importPredefinedVars' => true,
0042         'generateAdditionalVars' => true
0043     );
0044 
0045     /**
0046      * Overloaded properties
0047      *
0048      * @var     array
0049      */
0050     protected $_properties = array();
0051 
0052     /**
0053      * Constructor
0054      *
0055      * @param   array $config
0056      * @return  void
0057      */
0058     public function __construct(array $config = null)
0059     {
0060         if ($config) {
0061             $this->_config = $config + $this->_config;
0062         }
0063         if ($this->_config['importPredefinedVars']) {
0064             $this->importPredefinedVars();
0065         }
0066         if ($this->_config['generateAdditionalVars']) {
0067             $this->generateAdditionalVars();
0068         }
0069     }
0070 
0071     /**
0072      * Magic method to set a property
0073      *
0074      * @param   string $key
0075      * @param   mixed $value
0076      * @return  void
0077      */
0078     public function __set($key, $value)
0079     {
0080         $this->_properties[$key] = $value;
0081     }
0082 
0083     /**
0084      * Magic method to get a property
0085      *
0086      * @param   string $key
0087      * @return  mixed
0088      */
0089     public function __get($key)
0090     {
0091         if (isset($this->_properties[$key])) {
0092             return $this->_properties[$key];
0093         }
0094         return null;
0095     }
0096 
0097     /**
0098      * Magic method to check a property
0099      *
0100      * @param   string $key
0101      * @return  bool
0102      */
0103     public function __isset($key)
0104     {
0105         return isset($this->_properties[$key]);
0106     }
0107 
0108     /**
0109      * Magic method to unset a property
0110      *
0111      * @param   string $key
0112      * @return  void
0113      */
0114     public function __unset($key)
0115     {
0116         unset($this->_properties[$key]);
0117     }
0118 
0119     /**
0120      * Import a predefined variables
0121      *
0122      * @return  void
0123      */
0124     public function importPredefinedVars()
0125     {
0126         if (!empty($_SERVER)) {
0127             $this->_properties += $_SERVER;
0128         }
0129     }
0130 
0131     /**
0132      * Generate an additional information variables
0133      *
0134      * @return  void
0135      */
0136     public function generateAdditionalVars()
0137     {
0138         $scheme = 'http';
0139         if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
0140             $scheme = 'https';
0141         }
0142         $port = '';
0143         if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
0144             $port = ':' . $_SERVER['SERVER_PORT'];
0145         }
0146         $location = $scheme . '://' . $_SERVER['SERVER_NAME'] . $port;
0147         $additionalVars = array(
0148             'serverUri' => $location . '/',
0149             'baseUri' => dirname($location . $_SERVER['SCRIPT_NAME']) . '/',
0150             'scriptUri' => $location . $_SERVER['SCRIPT_NAME']
0151         );
0152         $this->_properties += $additionalVars;
0153     }
0154 
0155 }