Changeset 277 for trunk

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

added support for loading classes which implement Serializable (php:ClassName)

Location:
trunk/ext/php
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/ext/php/CHANGELOG

    r276 r277  
    1111    - added support for loading sequences into classes which implement ArrayAccess (!php/array:ClassName) 
    1212    - added support for loading maps into classes which implement ArrayAccess (!php/hash:ClassName) 
     13    - added support for loading classes which implement Serializable (!php:ClassName) 
    1314 
    1415- version: 0.9.1 
  • trunk/ext/php/TODO

    r276 r277  
    77  - "add PHP6 (unicode strings) support" 
    88load: 
    9   - objects: 
    10     - "!php:ClassName (?)" 
    119  - allow user to assign custom handler for loading any other type of objects (java, ruby, etc.) 
    1210dump: 
  • trunk/ext/php/phpext.c

    r276 r277  
    142142 *   0 for sequential numeric arrays 
    143143 *   1 for insequential or associative arrays 
    144  */ 
     144**/ 
    145145static int psex_determine_array_type(HashTable *myht TSRMLS_DC) /* {{{ */ 
    146146{ 
     
    373373                                zval_dtor(&fname); 
    374374                                zval_dtor(params[0]); 
     375                        } else if (strncmp(n->type_id, "php:", 4) == 0) { 
     376                                size_t classname_len = strlen(n->type_id) - 4; 
     377                                char *classname = emalloc(classname_len + 1); 
     378                                zend_class_entry **ce; 
     379                                zval param; 
     380                                TSRMLS_FETCH(); 
     381 
     382                                strncpy(classname, n->type_id + 4, classname_len + 1); 
     383 
     384                                if (FAILURE == zend_lookup_class_ex(classname, classname_len, 1, &ce TSRMLS_CC)) { 
     385                                        zend_throw_exception_ex(syck_exception_entry, 0 TSRMLS_CC, "Couldn't find %s class on line %d, col %d: '%s'", classname, p->linect, p->cursor - p->lineptr, p->lineptr); 
     386                                        efree(classname); 
     387                                        break; 
     388                                } 
     389 
     390                                if (0 == instanceof_function_ex(*ce, zend_ce_serializable, 1 TSRMLS_CC)) { 
     391                                        zend_throw_exception_ex(syck_exception_entry, 0 TSRMLS_CC, "Class %s doesn't implement Serializable on line %d, col %d: '%s'", classname, p->linect, p->cursor - p->lineptr, p->lineptr); 
     392                                        efree(classname); 
     393                                        break; 
     394                                } 
     395                                efree(classname); 
     396 
     397                                object_init_ex(o, *ce); 
     398                                ZVAL_STRINGL(&param, n->data.str->ptr, n->data.str->len, 1); 
     399                                zend_call_method_with_1_params(&o, *ce, NULL, "unserialize", NULL, &param); 
    375400                        } else { 
    376401                                php_error(E_NOTICE, "syck extension didn't handle %s type => treating as a string", n->type_id); 
  • trunk/ext/php/phpunit-tests/TestLoad.php

    r276 r277  
    1010class SyckTestSomeClass {} 
    1111 
     12class 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} 
    1236 
    1337class TestLoad extends PHPUnit_Framework_TestCase 
     
    199223        } 
    200224    } 
     225 
     226    public function testSerializable() 
     227    { 
     228        $obj = syck_load('!php:MySerializable teststring'); 
     229 
     230        $this->assertType('MySerializable', $obj); 
     231        $this->assertSame('teststring', $obj->test()); 
     232    } 
    201233}