Development

Changeset 10832

You must first sign up to be able to contribute.

Changeset 10832

Show
Ignore:
Timestamp:
08/13/08 09:46:08 (11 months ago)
Author:
fabien
Message:

[1.2] added full merge key support (closes #3615 - patch from lsmith)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r10830 r10832  
    9797 
    9898The browser class also automatically expires cookies as per the `expire` value of each cookie. 
     99 
     100YAML 
     101---- 
     102 
     103The YAML parser now supports full merge key (see http://yaml.org/type/merge.html for more examples). 
     104 
     105    [php] 
     106    $yaml = new sfYamlParser(); 
     107 
     108    print_r($yaml->parse(<<<EOF 
     109    default_param: &default_param 
     110      datasource: propel 
     111      phptype:    mysql 
     112      hostspec:   localhost 
     113      database:   db 
     114      username:   user 
     115      password:   pass 
     116 
     117    param: 
     118      <<: *default_param 
     119      username:   myuser 
     120      password:   mypass 
     121    EOF 
     122    )); 
     123 
     124    // displays 
     125    Array 
     126    ( 
     127        [default_param] => Array 
     128            ( 
     129                [datasource] => propel 
     130                [phptype]    => mysql 
     131                [hostspec]   => localhost 
     132                [database]   => db 
     133                [username]   => user 
     134                [password]   => pass 
     135            ) 
     136 
     137        [param] => Array 
     138            ( 
     139                [datasource] => propel 
     140                [phptype]    => mysql 
     141                [hostspec]   => localhost 
     142                [database]   => db 
     143                [username]   => myuser 
     144                [password]   => mypass 
     145            ) 
     146 
     147    ) 
  • branches/1.2/lib/yaml/sfYamlParser.class.php

    r10740 r10832  
    6767      } 
    6868 
    69       $isRef = $isInPlace = false; 
     69      $isRef = $isInPlace = $isProcessed = false; 
    7070      if (preg_match('#^\-(\s+(?P<value>.+?))?\s*$#', $this->currentLine, $values)) 
    7171      { 
     
    112112          else 
    113113          { 
    114             throw new InvalidArgumentException(sprintf('In place substitution must point to a reference at line %s (%s).', $this->getRealCurrentLineNb() + 1, $this->currentLine)); 
     114            if (isset($values['value']) && $values['value'] !== '') 
     115            { 
     116              $value = $values['value']; 
     117            } 
     118            else 
     119            { 
     120              $value = $this->getNextEmbedBlock(); 
     121            } 
     122            $c = $this->getRealCurrentLineNb() + 1; 
     123            $parser = new sfYamlParser($c); 
     124            $parser->refs =& $this->refs; 
     125            $parsed = $parser->parse($value); 
     126 
     127            $merged = array(); 
     128            if (!is_array($parsed)) 
     129            { 
     130              throw new InvalidArgumentException(sprintf("YAML merge keys used with a scalar value instead of an array at line %s (%s)", $this->getRealCurrentLineNb() + 1, $this->currentLine)); 
     131            } 
     132            else if (isset($parsed[0])) 
     133            { 
     134              // Numeric array, merge individual elements 
     135              foreach (array_reverse($parsed) as $parsedItem) 
     136              { 
     137                if (!is_array($parsedItem)) 
     138                { 
     139                  throw new InvalidArgumentException(sprintf("Merge items must be arrays at line %s (%s).", $this->getRealCurrentLineNb() + 1, $parsedItem)); 
     140                } 
     141                $merged = array_merge($parsedItem, $merged); 
     142              } 
     143            } 
     144            else 
     145            { 
     146              // Associative array, merge 
     147              $merged = array_merge($merge, $parsed); 
     148            } 
     149 
     150            $isProcessed = $merged; 
    115151          } 
    116152        } 
     
    121157        } 
    122158 
     159        if ($isProcessed) 
     160        { 
     161          // Merge keys 
     162          $data = $isProcessed; 
     163        } 
    123164        // hash 
    124         if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) 
     165        else if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) 
    125166        { 
    126167          // if next line is less indented or equal, then it means that the current value is null 
     
    154195        if (1 == count(explode("\n", rtrim($this->value, "\n")))) 
    155196        { 
    156           return sfYamlInline::load($this->lines[0]); 
     197          $value = sfYamlInline::load($this->lines[0]); 
     198          if (is_array($value)) 
     199          { 
     200            $first = reset($value); 
     201            if ('*' === substr($first, 0, 1)) 
     202            { 
     203              $data = array(); 
     204              foreach ($value as $alias) 
     205              { 
     206                $data[] = $this->refs[substr($alias, 1)]; 
     207              } 
     208              $value = $data; 
     209            } 
     210          } 
     211 
     212          return $value; 
    157213        } 
    158214 
  • branches/1.2/test/unit/yaml/fixtures/sfMergeKey.yml

    r8111 r10832  
    88yaml: | 
    99    foo: &foo 
    10         - Steve 
    11         - Clark 
    12         - Brian 
    13     bar: 
     10        a: Steve 
     11        b: Clark 
     12        c: Brian 
     13    bar: &bar 
    1414        <<: *foo 
    15         - Oren 
     15        x: Oren 
     16    foo2: &foo2 
     17        a: Ballmer 
     18    ding: &dong [ fi, fei, fo, fam] 
     19    check: 
     20        <<: 
     21            - *foo 
     22            - *dong 
     23        isit: tested 
     24    head: 
     25        <<: [ *foo , *dong , *foo2 ] 
    1626php: | 
    17     array('foo' => array('Steve', 'Clark', 'Brian'), 'bar' => array('Steve', 'Clark', 'Brian', 'Oren')) 
     27    array('foo' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian'), 'bar' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'x' => 'Oren'), 'foo2' => array('a' => 'Ballmer'), 'ding' => array('fi', 'fei', 'fo', 'fam'), 'check' => array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam', 'isit' => 'tested'), 'head' => array('a' => 'Ballmer', 'b' => 'Clark', 'c' => 'Brian', 'fi', 'fei', 'fo', 'fam')) 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.