/**
  * Throws an deprecated exception if given type exists.
  *
  * If no message is given, an auto-generated message will be used
  * which contains type/method & location where this method has been
  * invoked - respectively the location of the deprecated code.
  *
  * @param string $namespace_
  * @param string $clazz_
  * @param string $message_
  *
  * @throws \Components\Deprecated
  */
 public static function ifClassExists($namespace_, $clazz_, $message_ = null)
 {
     // ignore for production.
     if (Environment::isLive()) {
         return;
     }
     if (false === @class_exists($clazz_)) {
         if (null === $message_) {
             throw static::createDefaultException($namespace_);
         }
         throw new static($namespace_, $message_, null, true);
     }
 }
Exemplo n.º 2
0
 /**
  * Loads global configuration from file and process it.
  * @param  string|Nette\Config\Config  file name or Config object
  * @param  bool
  * @return Nette\Config\Config
  */
 public function loadConfig($file, $useCache)
 {
     if ($useCache === NULL) {
         $useCache = Environment::isLive();
     }
     $cache = $useCache && $this->cacheKey ? Environment::getCache('Nette.Environment') : NULL;
     $name = Environment::getName();
     $cacheKey = Environment::expand($this->cacheKey);
     if (isset($cache[$cacheKey])) {
         Environment::swapState($cache[$cacheKey]);
         $config = Environment::getConfig();
     } else {
         if ($file instanceof Config) {
             $config = $file;
             $file = NULL;
         } else {
             if ($file === NULL) {
                 $file = $this->defaultConfigFile;
             }
             $file = Environment::expand($file);
             $config = Config::fromFile($file, $name, 0);
         }
         // process environment variables
         if ($config->variable instanceof Config) {
             foreach ($config->variable as $key => $value) {
                 Environment::setVariable($key, $value);
             }
         }
         if (PATH_SEPARATOR !== ';' && isset($config->set->include_path)) {
             $config->set->include_path = str_replace(';', PATH_SEPARATOR, $config->set->include_path);
         }
         $config->expand();
         $config->setReadOnly();
         // process services
         $locator = Environment::getServiceLocator();
         if ($config->service instanceof Config) {
             foreach ($config->service as $key => $value) {
                 $locator->addService($value, strtr($key, '-', '\\'));
             }
         }
         // save cache
         if ($cache) {
             $state = Environment::swapState(NULL);
             $state[0] = $config;
             // TODO: better!
             $cache->save($cacheKey, $state, array(Cache::FILES => $file));
         }
     }
     // check temporary directory - TODO: discuss
     /*
     $dir = Environment::getVariable('tempDir');
     if ($dir && !(is_dir($dir) && is_writable($dir))) {
     	trigger_error("Temporary directory '$dir' is not writable", E_USER_NOTICE);
     }
     */
     // process ini settings
     if ($config->set instanceof Config) {
         foreach ($config->set as $key => $value) {
             $key = strtr($key, '-', '.');
             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:
                         throw new NotSupportedException('Required function ini_set() is disabled.');
                 }
             }
         }
     }
     // define constants
     if ($config->const instanceof Config) {
         foreach ($config->const as $key => $value) {
             define($key, $value);
         }
     }
     // set modes
     if (isset($config->mode)) {
         foreach ($config->mode as $mode => $state) {
             Environment::setMode($mode, $state);
         }
     }
     return $config;
 }
Exemplo n.º 3
0
 /**
  * Enables displaying or logging errors and exceptions.
  * @param  int   error reporting level
  * @param  bool|string  log to file?
  * @param  array|string  send emails?
  * @return void
  */
 public static function enable($level = E_ALL, $logErrors = NULL, $sendEmails = FALSE)
 {
     if (version_compare(PHP_VERSION, '5.2.1') === 0) {
         throw new NotSupportedException(__METHOD__ . ' is not supported in PHP 5.2.1');
         // PHP bug #40815
     }
     // Environment auto-detection
     if ($logErrors === NULL && class_exists('Environment')) {
         $logErrors = Environment::isLive();
     }
     // Firebug detection
     if (self::$useFirebug === NULL) {
         self::$useFirebug = function_exists('json_encode') && !$logErrors && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/');
     }
     if ($level !== NULL) {
         error_reporting($level);
     }
     if (function_exists('ini_set')) {
         ini_set('display_startup_errors', !$logErrors);
         ini_set('display_errors', !$logErrors);
         // or 'stderr'
         ini_set('html_errors', self::$html);
         ini_set('log_errors', (bool) $logErrors);
     } elseif ($logErrors) {
         // throws error only on production server
         throw new NotSupportedException('Function ini_set() is not enabled.');
     }
     if ($logErrors) {
         if (is_string($logErrors)) {
             self::$logFile = strpos($logErrors, '%') === FALSE ? $logErrors : Environment::expand($logErrors);
         } else {
             try {
                 self::$logFile = Environment::expand('%logDir%/php_error.log');
             } catch (InvalidStateException $e) {
                 self::$logFile = 'php_error.log';
             }
         }
         ini_set('error_log', self::$logFile);
     }
     self::$sendEmails = $logErrors && $sendEmails;
     if (self::$sendEmails) {
         if (is_string($sendEmails)) {
             self::$emailHeaders['To'] = $sendEmails;
         } elseif (is_array($sendEmails)) {
             self::$emailHeaders = $sendEmails + self::$emailHeaders;
         }
         if (mt_rand() / mt_getrandmax() < self::$emailProbability) {
             self::observeErrorLog();
         }
     }
     if (!defined('E_RECOVERABLE_ERROR')) {
         define('E_RECOVERABLE_ERROR', 4096);
     }
     if (!defined('E_DEPRECATED')) {
         define('E_DEPRECATED', 8192);
     }
     set_exception_handler(array(__CLASS__, 'exceptionHandler'));
     set_error_handler(array(__CLASS__, 'errorHandler'));
     self::$enabled = TRUE;
 }