示例#1
0
 function loadConfig($file)
 {
     $name = NEnvironment::getName();
     if ($file instanceof NConfig) {
         $config = $file;
         $file = NULL;
     } else {
         if ($file === NULL) {
             $file = $this->defaultConfigFile;
         }
         $file = NEnvironment::expand($file);
         $config = NConfig::fromFile($file, $name);
     }
     if ($config->variable instanceof NConfig) {
         foreach ($config->variable as $key => $value) {
             NEnvironment::setVariable($key, $value);
         }
     }
     $iterator = new RecursiveIteratorIterator($config);
     foreach ($iterator as $key => $value) {
         $tmp = $iterator->getDepth() ? $iterator->getSubIterator($iterator->getDepth() - 1)->current() : $config;
         $tmp[$key] = NEnvironment::expand($value);
     }
     $runServices = array();
     $context = NEnvironment::getContext();
     if ($config->service instanceof NConfig) {
         foreach ($config->service as $key => $value) {
             $key = strtr($key, '-', '\\');
             if (is_string($value)) {
                 $context->removeService($key);
                 $context->addService($key, $value);
             } else {
                 $factory = $value->factory ? $value->factory : (isset($this->defaultServices[$key]) ? $this->defaultServices[$key] : NULL);
                 if ($factory) {
                     $context->removeService($key);
                     $context->addService($key, $factory, isset($value->singleton) ? $value->singleton : TRUE, (array) $value->option);
                 } else {
                     throw new InvalidStateException("Factory method is not specified for service {$key}.");
                 }
                 if ($value->run) {
                     $runServices[] = $key;
                 }
             }
         }
     }
     if (!$config->php) {
         $config->php = $config->set;
         unset($config->set);
     }
     if ($config->php instanceof NConfig) {
         if (PATH_SEPARATOR !== ';' && isset($config->php->include_path)) {
             $config->php->include_path = str_replace(';', PATH_SEPARATOR, $config->php->include_path);
         }
         foreach (clone $config->php as $key => $value) {
             if ($value instanceof NConfig) {
                 unset($config->php->{$key});
                 foreach ($value as $k => $v) {
                     $config->php->{"{$key}.{$k}"} = $v;
                 }
             }
         }
         foreach ($config->php as $key => $value) {
             $key = strtr($key, '-', '.');
             if (!is_scalar($value)) {
                 throw new InvalidStateException("Configuration value for directive '{$key}' is not scalar.");
             }
             if ($key === 'date.timezone') {
                 date_default_timezone_set($value);
             }
             if (function_exists('ini_set')) {
                 ini_set($key, $value);
             } else {
                 switch ($key) {
                     case 'include_path':
                         set_include_path($value);
                         break;
                     case 'iconv.internal_encoding':
                         iconv_set_encoding('internal_encoding', $value);
                         break;
                     case 'mbstring.internal_encoding':
                         mb_internal_encoding($value);
                         break;
                     case 'date.timezone':
                         date_default_timezone_set($value);
                         break;
                     case 'error_reporting':
                         error_reporting($value);
                         break;
                     case 'ignore_user_abort':
                         ignore_user_abort($value);
                         break;
                     case 'max_execution_time':
                         set_time_limit($value);
                         break;
                     default:
                         if (ini_get($key) != $value) {
                             throw new NotSupportedException('Required function ini_set() is disabled.');
                         }
                 }
             }
         }
     }
     if ($config->const instanceof NConfig) {
         foreach ($config->const as $key => $value) {
             define($key, $value);
         }
     }
     if (isset($config->mode)) {
         foreach ($config->mode as $mode => $state) {
             NEnvironment::setMode($mode, $state);
         }
     }
     foreach ($runServices as $name) {
         $context->getService($name);
     }
     return $config;
 }
示例#2
0
 public static function enable($mode = NULL, $logFile = NULL, $email = NULL)
 {
     error_reporting(E_ALL | E_STRICT);
     if (is_bool($mode)) {
         self::$productionMode = $mode;
     } elseif (is_string($mode)) {
         $mode = preg_split('#[,\\s]+#', $mode);
     }
     if (is_array($mode)) {
         self::$productionMode = !isset($_SERVER['REMOTE_ADDR']) || !in_array($_SERVER['REMOTE_ADDR'], $mode, TRUE);
     }
     if (self::$productionMode === self::DETECT) {
         if (class_exists('Environment')) {
             self::$productionMode = NEnvironment::isProduction();
         } elseif (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) {
             $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
             $oct = explode('.', $addr);
             self::$productionMode = $addr !== '::1' && (count($oct) !== 4 || $oct[0] !== '10' && $oct[0] !== '127' && ($oct[0] !== '172' || $oct[1] < 16 || $oct[1] > 31) && ($oct[0] !== '169' || $oct[1] !== '254') && ($oct[0] !== '192' || $oct[1] !== '168'));
         } else {
             self::$productionMode = !self::$consoleMode;
         }
     }
     if (self::$productionMode && $logFile !== FALSE) {
         self::$logFile = 'log/php_error.log';
         if (class_exists('Environment')) {
             if (is_string($logFile)) {
                 self::$logFile = NEnvironment::expand($logFile);
             } else {
                 try {
                     self::$logFile = NEnvironment::expand('%logDir%/php_error.log');
                 } catch (InvalidStateException $e) {
                 }
             }
         } elseif (is_string($logFile)) {
             self::$logFile = $logFile;
         }
         ini_set('error_log', self::$logFile);
     }
     if (function_exists('ini_set')) {
         ini_set('display_errors', !self::$productionMode);
         ini_set('html_errors', FALSE);
         ini_set('log_errors', FALSE);
     } elseif (ini_get('display_errors') != !self::$productionMode && ini_get('display_errors') !== (self::$productionMode ? 'stderr' : 'stdout')) {
         throw new NotSupportedException('Function ini_set() must be enabled.');
     }
     self::$sendEmails = self::$logFile && $email;
     if (self::$sendEmails) {
         if (is_string($email)) {
             self::$emailHeaders['To'] = $email;
         } elseif (is_array($email)) {
             self::$emailHeaders = $email + self::$emailHeaders;
         }
     }
     if (!defined('E_DEPRECATED')) {
         define('E_DEPRECATED', 8192);
     }
     if (!defined('E_USER_DEPRECATED')) {
         define('E_USER_DEPRECATED', 16384);
     }
     register_shutdown_function(array(__CLASS__, '_shutdownHandler'));
     set_exception_handler(array(__CLASS__, '_exceptionHandler'));
     set_error_handler(array(__CLASS__, '_errorHandler'));
     self::$enabled = TRUE;
 }