Esempio n. 1
0
 /**
  * Loads the section $section from the config file $filename for
  * access facilitated by nested object properties.
  *
  * If the section name contains a ":" then the section name to the right
  * is loaded and included into the properties. Note that the keys in
  * this $section will override any keys of the same
  * name in the sections that have been included via ":".
  *
  * If the $section is null, then all sections in the ini file are loaded.
  *
  * If any key includes a ".", then this will act as a separator to
  * create a sub-property.
  *
  * example ini file:
  *      [all]
  *      db.connection = database
  *      hostname = live
  *
  *      [staging : all]
  *      hostname = staging
  *
  * after calling $data = new Yaf_Config_Ini($file, 'staging'); then
  *      $data->hostname === "staging"
  *      $data->db->connection === "database"
  *
  * @param  string        $filename
  * @param  mixed         $section
  * @param  boolean $readonly
  * @throws Yaf_Config_Exception
  * @return void
  */
 public function __construct($filename, $section = null)
 {
     if (empty($filename)) {
         \Yaf\Exception::trigger_error('Unable to find config file ' . $filename, E_USER_ERROR);
         //throw new Yaf_Config_Exception('Filename is not set');
     }
     if (is_array($filename)) {
         $this->_config = $filename;
     } elseif (is_string($filename)) {
         $iniArray = $this->_loadIniFile($filename);
         if (null === $section) {
             // Load entire file
             $dataArray = array();
             foreach ($iniArray as $sectionName => $sectionData) {
                 if (!is_array($sectionData)) {
                     $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
                 } else {
                     $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
                 }
             }
             parent::__construct($dataArray, true);
         } else {
             // Load one or more sections
             if (!is_array($section)) {
                 $section = array($section);
             }
             $dataArray = array();
             foreach ($section as $sectionName) {
                 if (!isset($iniArray[$sectionName])) {
                     throw new Exception("There is no section '{$sectionName}' in '{$filename}'");
                 }
                 $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
             }
             parent::__construct($dataArray, true);
         }
     } else {
         throw new \Yaf\Exception\TypeError('Invalid parameters provided, must be path of ini file');
     }
 }
Esempio n. 2
0
 /**
  * 框架错误模板渲染
  * @return string
  */
 public function frameworkExceptionHandler(\Yaf\Exception $exception)
 {
     $this->response(['message' => $exception->getMessage()], $exception->getCode());
 }
Esempio n. 3
0
 public function __construct($msg, $code = 700)
 {
     parent::__construct($msg, $code);
 }
 /**
  *
  * @param \Yaf\Exception $exception   Yaf定义的异常类
  * @return boolean
  */
 public function errorAction($exception)
 {
     switch ($exception->getCode()) {
         case YAF\ERR\NOTFOUND\MODULE:
         case YAF\ERR\NOTFOUND\CONTROLLER:
         case YAF\ERR\NOTFOUND\ACTION:
         case YAF\ERR\NOTFOUND\VIEW:
         case 404:
             header('HTTP/1.1 404 Not Found');
             break;
         default:
             header('HTTP/1.1 500');
             break;
     }
     // 只有以开发环境下, 才会输出完整的错误信息, 线上环境只输出错误码.
     if ('develop' == \Yaf\Application::app()->environ()) {
         if ($this->getRequest()->isXmlHttpRequest()) {
             $data = array('code' => $exception->getCode(), 'file' => $exception->getFile(), 'errormsg' => $exception->getMessage(), 'line' => $exception->getLine(), 'traceAsString' => $exception->getTraceAsString());
             $this->jsonp($data, false, $exception->getCode(), $exception->getMessage(), $this->getCallbackName());
         } else {
             echo '<h1> Error No.: ', $exception->getCode(), '</h1>';
             echo '<h2>Message:</h2>';
             echo '<div>', $exception->getMessage(), '</div>';
             echo '<h2>File:</h2>';
             echo '<div>', $exception->getFile(), '</div>';
             echo '<h2>Line:</h2>';
             echo '<div>', $exception->getLine(), '</div>';
             echo '<h2>Trace:</h2>';
             echo '<pre>', $exception->getTraceAsString(), '</pre>';
         }
     } else {
         if ($this->getRequest()->isXmlHttpRequest()) {
             $this->jsonp(array('code' => $exception->getCode()), false, $exception->getCode(), '', $this->getCallbackName());
             exit;
         }
     }
 }