Exemplo n.º 1
0
 /**
  * Constructor.
  *
  * @param string $file The configuration file location relative to the application root.
  *
  * @throws Exception
  */
 public function __construct($file)
 {
     $file = $file . '.php';
     $env = Environment::current();
     if (is_file($file)) {
         /** @noinspection PhpIncludeInspection */
         $data = (require $file);
         if (is_array($data)) {
             if (array_key_exists(Environment::ALL, $data)) {
                 $this->_data = $data[Environment::ALL];
                 if (array_key_exists($env, $data)) {
                     $this->_data = array_replace_recursive($this->_data, $data[$env]);
                 }
             } else {
                 $this->_data = $data;
             }
         } else {
             throw new Exception('Configuration data must be an array.');
         }
     } else {
         throw new Exception('Configuration file "' . $file . '" does not exist.');
     }
 }
Exemplo n.º 2
0
 public function __get($prop)
 {
     if ($prop === 'dev') {
         return Environment::current() === Environment::DEV;
     }
     if ($prop === 'enabled') {
         return $this->_config->get('enabled', true);
     }
     if ($prop === 'url') {
         return $this->memoize('url', function () {
             // Attempt to guess the website URL based on whether the request was over HTTPS, the serverName variable and
             // the port the request was made over.
             $guess = ($this->secure ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . ($this->port === 80 || $this->port === 443 ? '' : ':' . $this->port);
             return rtrim($this->_config->get('url', $guess), '/');
         });
     }
     if ($prop === 'public') {
         return $this->memoize('public', function () {
             $path = '/' . trim(parse_url($this->url, PHP_URL_PATH), '/');
             return $path === '/' ? '' : $path;
         });
     }
     if ($prop === 'root') {
         return rtrim($this->_root, '/');
     }
     if ($prop === 'router') {
         return $this->_router;
     }
     if ($prop === 'environment') {
         return Environment::current();
     }
     // Useful server information.
     if ($prop === 'host') {
         return $_SERVER['SERVER_NAME'];
     }
     if ($prop === 'port') {
         return intval($_SERVER['SERVER_PORT']);
     }
     if ($prop === 'secure') {
         return $this->memoize('secure', function () {
             return !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' || $this->port === 443;
         });
     }
     if ($prop === 'timezone') {
         return $this->_config->get('timezone', @date_default_timezone_get());
     }
     if ($prop === 'config') {
         return $this->_config;
     }
     if ($this->hasService($prop)) {
         // We found a service with a matching name. Set it up and return it.
         $service = $this->_services[$prop];
         $service->runSetup();
         return $service;
     }
     return null;
 }