Changeset 279 for trunk/ext

Show
Ignore:
Timestamp:
10/05/2007 10:34:34 (14 months ago)
Author:
indeyets
Message:

- changed naming of tags to php/(object|array|hash)::ClassName?
- added ability to emit Serializable objects

Location:
trunk/ext/php
Files:
1 added
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/ext/php/CHANGELOG

    r278 r279  
    88    - fixed loading of maps with numeric keys 
    99    - short-and-flat arrays are dumped using inline-mode now (for readability) 
     10    - changed explicit type of DateTime class to be !php/object::DateTime 
    1011    - added support for loading explicitly typed DateTime (with timestamp-compatible value) 
    11     - added support for loading sequences into classes which implement ArrayAccess (!php/array:ClassName) 
    12     - added support for loading maps into classes which implement ArrayAccess (!php/hash:ClassName) 
    13     - added support for loading classes which implement Serializable (!php:ClassName) 
    14     - changed explicit type of DateTime class to be !php:DateTime 
     12    - added support for loading sequences into classes which implement ArrayAccess (!php/array::ClassName) 
     13    - added support for loading maps into classes which implement ArrayAccess (!php/hash::ClassName) 
     14    - added support for loading classes which implement Serializable (!php/object::ClassName) 
     15    - added support for dumping classes which implement Serializable (!php/object::ClassName) 
    1516 
    1617- version: 0.9.1 
  • trunk/ext/php/TODO

    r277 r279  
    1313    - Iterator: 
    1414      as: map 
    15       label: "!php/hash:ClassName" 
     15      label: "!php/hash::ClassName" 
    1616    - ArrayAccess: 
    1717      as: sequence 
    18       label: "!php/array:ClassName" 
     18      label: "!php/array::ClassName" 
    1919    - Generic object (?) 
  • trunk/ext/php/phpext.c

    r278 r279  
    360360                        } else if (strcmp(n->type_id, "float#neginf") == 0) { 
    361361                                ZVAL_DOUBLE(o, -inf()); 
    362                         } else if (strncmp(n->type_id, "timestamp", 9) == 0 || strcmp(n->type_id, "php:Datetime") == 0) { 
     362                        } else if (strncmp(n->type_id, "timestamp", 9) == 0 || strcmp(n->type_id, "php/object::Datetime") == 0) { 
    363363                                zval fname, param, *params[1]; 
    364364                                TSRMLS_FETCH(); 
     
    373373                                zval_dtor(&fname); 
    374374                                zval_dtor(params[0]); 
    375                         } else if (strncmp(n->type_id, "php:", 4) == 0) { 
     375                        } else if (strncmp(n->type_id, "php/object::", 12) == 0) { 
    376376                                /* Some custom php-class packed in a string. Only ones implementing Serializable ar supported */ 
    377                                 size_t classname_len = strlen(n->type_id) - 4; 
     377                                size_t classname_len = strlen(n->type_id) - 12; 
    378378                                char *classname = emalloc(classname_len + 1); 
    379379                                zend_class_entry **ce; 
     
    381381                                TSRMLS_FETCH(); 
    382382 
    383                                 strncpy(classname, n->type_id + 4, classname_len + 1); 
     383                                strncpy(classname, n->type_id + 12, classname_len + 1); 
    384384 
    385385                                if (FAILURE == zend_lookup_class_ex(classname, classname_len, 1, &ce TSRMLS_CC)) { 
     
    421421                                        add_index_zval(o, i, o2); 
    422422                                } 
    423                         } else if (strncmp(n->type_id, "php/array:", 10) == 0) { 
     423                        } else if (strncmp(n->type_id, "php/array::", 11) == 0) { 
    424424                                /* some class which implements ArrayAccess */ 
    425425                                size_t i; 
    426                                 size_t classname_len = strlen(n->type_id) - 10; 
     426                                size_t classname_len = strlen(n->type_id) - 11; 
    427427                                char *classname = emalloc(classname_len + 1); 
    428428                                zend_class_entry **ce; 
    429429                                TSRMLS_FETCH(); 
    430430 
    431                                 strncpy(classname, n->type_id + 10, classname_len + 1); 
     431                                strncpy(classname, n->type_id + 11, classname_len + 1); 
    432432 
    433433                                if (FAILURE == zend_lookup_class_ex(classname, classname_len, 1, &ce TSRMLS_CC)) { 
     
    501501                                        zval_ptr_dtor(&o2); 
    502502                                } 
    503                         } else if (strncmp(n->type_id, "php/hash:", 9) == 0) { 
     503                        } else if (strncmp(n->type_id, "php/hash::", 10) == 0) { 
    504504                                /* some classm which implements ArrayAccess */ 
    505505                                SYMID oid; 
    506506                                size_t i; 
    507507                                zval *o2, *o3; 
    508                                 size_t classname_len = strlen(n->type_id) - 9; 
     508                                size_t classname_len = strlen(n->type_id) - 10; 
    509509                                char *classname = emalloc(classname_len + 1); 
    510510                                zend_class_entry **ce; 
    511511                                TSRMLS_FETCH(); 
    512512 
    513                                 strncpy(classname, n->type_id + 9, classname_len + 1); 
     513                                strncpy(classname, n->type_id + 10, classname_len + 1); 
    514514 
    515515                                if (FAILURE == zend_lookup_class_ex(classname, classname_len, 1, &ce TSRMLS_CC)) { 
     
    733733                        zend_get_object_classname(data, &name, &name_len TSRMLS_CC); 
    734734 
     735                        /* DateTime is encoded as timestamp */ 
    735736                        if (strncmp(name, "DateTime", name_len) == 0) { 
    736737                                zval *retval; 
     
    745746                                zval_dtor(retval); 
    746747                                efree(retval); 
     748                        } else { 
     749                                if (0 != instanceof_function_ex(ce, zend_ce_serializable, 1 TSRMLS_CC)) { 
     750                                        char *prefix = "tag:php:object::"; 
     751                                        size_t prefix_len = strlen(prefix) + 1; 
     752                                        char *tagname = emalloc(name_len + prefix_len); 
     753                                        zval *serialized; 
     754 
     755                                        snprintf(tagname, name_len + prefix_len, "%s%s", prefix, name); 
     756 
     757                                        zend_call_method_with_0_params(&data, ce, NULL, "serialize", &serialized); 
     758                                        syck_emit_scalar(e, tagname, scalar_2quote, 0, 0, 0, Z_STRVAL_P(serialized), Z_STRLEN_P(serialized)); 
     759 
     760                                        efree(tagname); 
     761                                } 
    747762                        } 
    748763 
  • trunk/ext/php/phpunit-tests/TestDump.php

    r273 r279  
    55 
    66require_once "PHPUnit/Framework/TestCase.php"; 
     7require 'helpers.php'; 
    78 
    89error_reporting(E_ALL); 
     
    1011class TestDump extends PHPUnit_Framework_TestCase 
    1112{ 
     13    public function testNull() 
     14    { 
     15        $arr = array('qq' => null); 
     16        $this->assertEquals($arr, syck_load(syck_dump($arr))); 
     17    } 
     18 
    1219    public function testArray() 
    1320    { 
     
    4754        $this->assertEquals($arr2, $arr); 
    4855    } 
     56 
     57    public function testSerializable() 
     58    { 
     59        $obj = new MySerializable('some string'); 
     60        $this->assertEquals($obj, syck_load(syck_dump($obj))); 
     61    } 
    4962} 
  • trunk/ext/php/phpunit-tests/TestLoad.php

    r278 r279  
    55 
    66require_once "PHPUnit/Framework/TestCase.php"; 
     7require 'helpers.php'; 
    78 
    89error_reporting(E_ALL); 
    910 
    10 class SyckTestSomeClass {} 
    11  
    12 class MySerializable implements Serializable 
    13 { 
    14     private $string = null; 
    15  
    16     public function __construct() 
    17     { 
    18         throw new Exception('This is not supposed to be called'); 
    19     } 
    20  
    21     public function serialize() 
    22     { 
    23         return 'test'; 
    24     } 
    25  
    26     public function unserialize($string) 
    27     { 
    28         $this->string = $string; 
    29     } 
    30  
    31     public function test() 
    32     { 
    33         return $this->string; 
    34     } 
    35 } 
    3611 
    3712class TestLoad extends PHPUnit_Framework_TestCase 
     
    165140 
    166141        // explicit 
    167         $this->assertType('DateTime', syck_load("!php:Datetime 2002-12-14")); 
     142        $this->assertType('DateTime', syck_load("!php/object::Datetime 2002-12-14")); 
    168143    } 
    169144 
     
    175150 
    176151        // ArrayObject implements ArrayAccess: OK 
    177         $this->assertEquals(syck_load('!php/array:ArrayObject []'), new ArrayObject()); 
    178         $this->assertEquals(syck_load('!php/array:ArrayObject [1, 2, 3]'), new ArrayObject(array(1, 2, 3))); 
     152        $this->assertEquals(syck_load('!php/array::ArrayObject []'), new ArrayObject()); 
     153        $this->assertEquals(syck_load('!php/array::ArrayObject [1, 2, 3]'), new ArrayObject(array(1, 2, 3))); 
    179154 
    180155        // SyckTestSomeClass doesn't implement ArrayAccess: FAILURE 
    181156        try { 
    182             syck_load('!php/array:SyckTestSomeClass []'); 
     157            syck_load('!php/array::SyckTestSomeClass []'); 
    183158            $this->assertTrue(false); 
    184159        } catch (SyckException $e) { 
     
    188163        // SyckTestOtherClass doesn't exist: FAILURE 
    189164        try { 
    190             syck_load('!php/array:SyckTestOtherClass []'); 
     165            syck_load('!php/array::SyckTestOtherClass []'); 
    191166            $this->assertTrue(false); 
    192167        } catch (SyckException $e) { 
     
    201176 
    202177        // ArrayObject implements ArrayAccess: OK 
    203         $this->assertEquals(new ArrayObject(), syck_load('!php/hash:ArrayObject {}')); 
     178        $this->assertEquals(new ArrayObject(), syck_load('!php/hash::ArrayObject {}')); 
    204179        $this->assertEquals( 
    205180            new ArrayObject(array('a' => 1, 'b' => 2, 3 => 3, 4 => 'd', 'e' => 5)), 
    206             syck_load('!php/hash:ArrayObject {a: 1, b: 2, 3: 3, 4: d, e: 5}') 
     181            syck_load('!php/hash::ArrayObject {a: 1, b: 2, 3: 3, 4: d, e: 5}') 
    207182        ); 
    208183 
    209184        // SyckTestSomeClass doesn't implement ArrayAccess: FAILURE 
    210185        try { 
    211             syck_load('!php/hash:SyckTestSomeClass {}'); 
     186            syck_load('!php/hash::SyckTestSomeClass {}'); 
    212187            $this->assertTrue(false); 
    213188        } catch (SyckException $e) { 
     
    217192        // SyckTestOtherClass doesn't exist: FAILURE 
    218193        try { 
    219             syck_load('!php/hash:SyckTestOtherClass {}'); 
     194            syck_load('!php/hash::SyckTestOtherClass {}'); 
    220195            $this->assertTrue(false); 
    221196        } catch (SyckException $e) { 
     
    226201    public function testSerializable() 
    227202    { 
    228         $obj = syck_load('!php:MySerializable teststring'); 
     203        $obj = syck_load('!php/object::MySerializable teststring'); 
    229204 
    230205        $this->assertType('MySerializable', $obj);