web/lib/Zend/Config/Yaml.php
changeset 886 1e110b03ae96
parent 807 877f952ae2bd
child 1230 68c69c656a2c
equal deleted inserted replaced
885:2251fb41dbc7 886:1e110b03ae96
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category  Zend
    15  * @category  Zend
    16  * @package   Zend_Config
    16  * @package   Zend_Config
    17  * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version   $Id: Yaml.php 23294 2010-11-05 00:27:34Z ramon $
    19  * @version   $Id: Yaml.php 25169 2012-12-22 12:23:11Z rob $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Config
    23  * @see Zend_Config
    24  */
    24  */
    27 /**
    27 /**
    28  * YAML Adapter for Zend_Config
    28  * YAML Adapter for Zend_Config
    29  *
    29  *
    30  * @category  Zend
    30  * @category  Zend
    31  * @package   Zend_Config
    31  * @package   Zend_Config
    32  * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    33  * @license   http://framework.zend.com/license/new-bsd     New BSD License
    34  */
    34  */
    35 class Zend_Config_Yaml extends Zend_Config
    35 class Zend_Config_Yaml extends Zend_Config
    36 {
    36 {
    37     /**
    37     /**
    91     }
    91     }
    92 
    92 
    93     /**
    93     /**
    94      * Set callback for decoding YAML
    94      * Set callback for decoding YAML
    95      *
    95      *
    96      * @param  $yamlDecoder the decoder to set
    96      * @param  callable $yamlDecoder the decoder to set
    97      * @return Zend_Config_Yaml
    97      * @return Zend_Config_Yaml
    98      */
    98      */
    99     public function setYamlDecoder($yamlDecoder)
    99     public function setYamlDecoder($yamlDecoder)
   100     {
   100     {
   101         if (!is_callable($yamlDecoder)) {
   101         if (!is_callable($yamlDecoder)) {
   122      * Options may include:
   122      * Options may include:
   123      * - allow_modifications: whether or not the config object is mutable
   123      * - allow_modifications: whether or not the config object is mutable
   124      * - skip_extends: whether or not to skip processing of parent configuration
   124      * - skip_extends: whether or not to skip processing of parent configuration
   125      * - yaml_decoder: a callback to use to decode the Yaml source
   125      * - yaml_decoder: a callback to use to decode the Yaml source
   126      *
   126      *
   127      * @param  string  $yaml     YAML file to process
   127      * @param  string        $yaml     YAML file to process
   128      * @param  mixed   $section Section to process
   128      * @param  mixed         $section  Section to process
   129      * @param  boolean $options Whether modifiacations are allowed at runtime
   129      * @param  array|boolean $options
   130      */
   130      */
   131     public function __construct($yaml, $section = null, $options = false)
   131     public function __construct($yaml, $section = null, $options = false)
   132     {
   132     {
   133         if (empty($yaml)) {
   133         if (empty($yaml)) {
   134             require_once 'Zend/Config/Exception.php';
   134             require_once 'Zend/Config/Exception.php';
   199         } elseif (is_array($section)) {
   199         } elseif (is_array($section)) {
   200             $dataArray = array();
   200             $dataArray = array();
   201             foreach ($section as $sectionName) {
   201             foreach ($section as $sectionName) {
   202                 if (!isset($config[$sectionName])) {
   202                 if (!isset($config[$sectionName])) {
   203                     require_once 'Zend/Config/Exception.php';
   203                     require_once 'Zend/Config/Exception.php';
   204                     throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
   204                     throw new Zend_Config_Exception(sprintf(
       
   205                         'Section "%s" cannot be found', 
       
   206                         implode(' ', (array)$section)
       
   207                     ));
   205                 }
   208                 }
   206 
   209 
   207                 $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
   210                 $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
   208             }
   211             }
   209             parent::__construct($dataArray, $allowModifications);
   212             parent::__construct($dataArray, $allowModifications);
   210         } else {
   213         } else {
   211             if (!isset($config[$section])) {
   214             if (!isset($config[$section])) {
   212                 require_once 'Zend/Config/Exception.php';
   215                 require_once 'Zend/Config/Exception.php';
   213                 throw new Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
   216                 throw new Zend_Config_Exception(sprintf(
       
   217                     'Section "%s" cannot be found', 
       
   218                     implode(' ', (array)$section)
       
   219                 ));
   214             }
   220             }
   215 
   221 
   216             $dataArray = $this->_processExtends($config, $section);
   222             $dataArray = $this->_processExtends($config, $section);
   217             if (!is_array($dataArray)) {
   223             if (!is_array($dataArray)) {
   218                 // Section in the yaml data contains just one top level string
   224                 // Section in the yaml data contains just one top level string
   282     protected static function _decodeYaml($currentIndent, &$lines)
   288     protected static function _decodeYaml($currentIndent, &$lines)
   283     {
   289     {
   284         $config   = array();
   290         $config   = array();
   285         $inIndent = false;
   291         $inIndent = false;
   286         while (list($n, $line) = each($lines)) {
   292         while (list($n, $line) = each($lines)) {
   287             $lineno = $n+1;
   293             $lineno = $n + 1;
       
   294 
       
   295             $line = rtrim(preg_replace("/#.*$/", "", $line));
   288             if (strlen($line) == 0) {
   296             if (strlen($line) == 0) {
   289                 continue;
   297                 continue;
   290             }
   298             }
   291             if ($line[0] == '#') {
   299 
   292                 // comment
       
   293                 continue;
       
   294             }
       
   295             $indent = strspn($line, " ");
   300             $indent = strspn($line, " ");
   296 
   301 
   297             // line without the spaces
   302             // line without the spaces
   298             $line = trim($line);
   303             $line = trim($line);
   299             if (strlen($line) == 0) {
   304             if (strlen($line) == 0) {
   309             if (!$inIndent) {
   314             if (!$inIndent) {
   310                 $currentIndent = $indent;
   315                 $currentIndent = $indent;
   311                 $inIndent      = true;
   316                 $inIndent      = true;
   312             }
   317             }
   313 
   318 
   314             if (preg_match("/(\w+):\s*(.*)/", $line, $m)) {
   319             if (preg_match("/(?!-)([\w\-]+):\s*(.*)/", $line, $m)) {
   315                 // key: value
   320                 // key: value
   316                 if ($m[2]) {
   321                 if (strlen($m[2])) {
   317                     // simple key: value
   322                     // simple key: value
   318                     $value = $m[2];
   323                     $value = preg_replace("/#.*$/", "", $m[2]);
   319                     // Check for booleans and constants
   324                     $value = self::_parseValue($value);
   320                     if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
       
   321                         $value = true;
       
   322                     } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
       
   323                         $value = false;
       
   324                     } elseif (!self::$_ignoreConstants) {
       
   325                         // test for constants
       
   326                         $value = self::_replaceConstants($value);
       
   327                     }
       
   328                 } else {
   325                 } else {
   329                     // key: and then values on new lines
   326                     // key: and then values on new lines
   330                     $value = self::_decodeYaml($currentIndent + 1, $lines);
   327                     $value = self::_decodeYaml($currentIndent + 1, $lines);
   331                     if (is_array($value) && !count($value)) {
   328                     if (is_array($value) && !count($value)) {
   332                         $value = "";
   329                         $value = "";
   335                 $config[$m[1]] = $value;
   332                 $config[$m[1]] = $value;
   336             } elseif ($line[0] == "-") {
   333             } elseif ($line[0] == "-") {
   337                 // item in the list:
   334                 // item in the list:
   338                 // - FOO
   335                 // - FOO
   339                 if (strlen($line) > 2) {
   336                 if (strlen($line) > 2) {
   340                     $config[] = substr($line, 2);
   337                     $value = substr($line, 2);
       
   338 
       
   339                     $config[] = self::_parseValue($value);
   341                 } else {
   340                 } else {
   342                     $config[] = self::_decodeYaml($currentIndent + 1, $lines);
   341                     $config[] = self::_decodeYaml($currentIndent + 1, $lines);
   343                 }
   342                 }
   344             } else {
   343             } else {
   345                 require_once 'Zend/Config/Exception.php';
   344                 require_once 'Zend/Config/Exception.php';
   351         }
   350         }
   352         return $config;
   351         return $config;
   353     }
   352     }
   354 
   353 
   355     /**
   354     /**
       
   355      * Parse values
       
   356      *
       
   357      * @param string $value
       
   358      * @return string
       
   359      */
       
   360     protected static function _parseValue($value)
       
   361     {
       
   362         $value = trim($value);
       
   363 
       
   364         // remove quotes from string.
       
   365         if ('"' == $value['0']) {
       
   366             if ('"' == $value[count($value) -1]) {
       
   367                 $value = substr($value, 1, -1);
       
   368             }
       
   369         } elseif ('\'' == $value['0'] && '\'' == $value[count($value) -1]) {
       
   370             $value = strtr($value, array("''" => "'", "'" => ''));
       
   371         }
       
   372 
       
   373         // Check for booleans and constants
       
   374         if (preg_match('/^(t(rue)?|on|y(es)?)$/i', $value)) {
       
   375             $value = true;
       
   376         } elseif (preg_match('/^(f(alse)?|off|n(o)?)$/i', $value)) {
       
   377             $value = false;
       
   378         } elseif (strcasecmp($value, 'null') === 0) {
       
   379             $value = null;
       
   380         } elseif (!self::$_ignoreConstants) {
       
   381             // test for constants
       
   382             $value = self::_replaceConstants($value);
       
   383         }
       
   384 
       
   385         return $value;
       
   386     }
       
   387 
       
   388     /**
   356      * Replace any constants referenced in a string with their values
   389      * Replace any constants referenced in a string with their values
   357      *
   390      *
   358      * @param  string $value
   391      * @param  string $value
   359      * @return string
   392      * @return string
   360      */
   393      */