Development

Changeset 7981

You must first sign up to be able to contribute.

Changeset 7981

Show
Ignore:
Timestamp:
03/19/08 18:25:29 (1 year ago)
Author:
fabien
Message:

added basic support for dumping/loading PHP objects in YAML (closes #3137)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/util/sfYamlInline.class.php

    r7923 r7981  
    4949   * Dumps PHP array to YAML. 
    5050   * 
    51    * @param mixed PHP 
     51   * @param mixed   PHP 
    5252   * 
    5353   * @return string YAML 
     
    6060        throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.'); 
    6161      case is_object($value): 
    62         throw new InvalidArgumentException('Unable to dump objects to a YAML string.'); 
     62        return '!!php/object:'.serialize($value); 
    6363      case is_array($value): 
    6464        return self::dumpArray($value); 
     
    368368      case 0 === strpos($scalar, '! '): 
    369369        return intval(self::parseScalar(substr($scalar, 2))); 
     370      case 0 === strpos($scalar, '!!php/object:'): 
     371        return unserialize(substr($scalar, 13)); 
    370372      case ctype_digit($scalar): 
    371373        return '0' == $scalar[0] ? octdec($scalar) : intval($scalar); 
  • branches/1.1/test/unit/util/fixtures/yaml/index.yml

    r7923 r7981  
    11- sfComments 
    22- sfTests 
     3- sfObjects 
    34#- YtsAnchorAlias 
    45- YtsBasicTests 
  • branches/1.1/test/unit/util/sfYamlDumperTest.php

    r7923 r7981  
    1313require_once(dirname(__FILE__).'/../../../lib/util/sfYamlDumper.class.php'); 
    1414 
    15 $t = new lime_test(134, new lime_output_color()); 
     15$t = new lime_test(136, new lime_output_color()); 
    1616 
    1717$parser = new sfYamlParser(); 
     
    134134$t->is(sfYaml::dump($array, 4), $expected, '::dump() takes an inline level argument'); 
    135135$t->is(sfYaml::dump($array, 10), $expected, '::dump() takes an inline level argument'); 
     136 
     137// objects 
     138$t->diag('Objects support'); 
     139class A 
     140{ 
     141  public $a = 'foo'; 
     142} 
     143$a = array('foo' => new A(), 'bar' => 1); 
     144$t->is(sfYaml::dump($a), <<<EOF 
     145foo: !!php/object:O:1:"A":1:{s:1:"a";s:3:"foo";} 
     146bar: 1 
     147 
     148EOF 
     149, '::dump() is able to dump objects'); 
  • branches/1.1/test/unit/util/sfYamlParserTest.php

    r7923 r7981  
    1212require_once(dirname(__FILE__).'/../../../lib/util/sfYamlParser.class.php'); 
    1313 
    14 $t = new lime_test(133, new lime_output_color()); 
     14$t = new lime_test(135, new lime_output_color()); 
    1515 
    1616$parser = new sfYamlParser(); 
     
    6666  } 
    6767} 
     68 
     69// objects 
     70$t->diag('Objects support'); 
     71class A 
     72{ 
     73  public $a = 'foo'; 
     74} 
     75$a = array('foo' => new A(), 'bar' => 1); 
     76$t->is(sfYaml::load(<<<EOF 
     77foo: !!php/object:O:1:"A":1:{s:1:"a";s:3:"foo";} 
     78bar: 1 
     79EOF 
     80), $a, '::dump() is able to dump objects'); 

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.