Skip to content

Commit 32df5b7

Browse files
committed
Resolve attribute names according to imports (still requiring attributes to be unique)
1 parent 44340c4 commit 32df5b7

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

Zend/tests/attributes_002.phpt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ namespace Doctrine\ORM {
1515
$reflClass = new \ReflectionClass($class_name);
1616
$attrs = $reflClass->getAttributes();
1717
foreach ($attrs as $name => $values) {
18-
$name = "Doctrine\\" . $name;
19-
$attrs[$name] = new $name($values);
18+
$attrs[$name] = new $name($values[0][0]);
2019
}
2120
return $attrs;
2221
}
2322
}
2423

2524
namespace Doctrine\ORM\Mapping {
2625
class Entity {
26+
public $tableName;
27+
public $repository;
28+
29+
public function __construct(array $values)
30+
{
31+
foreach ($values as $k => $v) {
32+
$this->$k = $v;
33+
}
34+
}
2735
}
2836
}
2937

Zend/tests/attributes_003.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ array(1) {
2525
array(1) {
2626
[0]=>
2727
array(1) {
28-
["foo"]=>
29-
string(3) "bar"
28+
[0]=>
29+
array(1) {
30+
["foo"]=>
31+
string(3) "bar"
32+
}
3033
}
3134
}
3235
}

Zend/zend_compile.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6216,6 +6216,42 @@ static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_as
62166216
}
62176217
/* }}} */
62186218

6219+
static HashTable* zend_compile_resolve_attributes(HashTable *declared_attributes)
6220+
{
6221+
zval *attr, *attrname, *val, tmp;
6222+
zend_string *resolved_name;
6223+
zend_string *key;
6224+
// declared attributes are in structure: idx => ["class" => "...", 0 => arg1, 1 => arg2, ...]
6225+
// resolved attributes are in structure fqcn => [ [0 => arg1, 1 => arg2], [0 => arg12, 1 => arg22] ]
6226+
HashTable *resolved_attributes;
6227+
6228+
ALLOC_HASHTABLE(resolved_attributes);
6229+
zend_hash_init(resolved_attributes, 8, NULL, ZVAL_PTR_DTOR, 0);
6230+
6231+
ZEND_HASH_FOREACH_STR_KEY_VAL(declared_attributes, key, attr) {
6232+
// @todo not yet "class" key with ast that stores ZEND_NAME_*,
6233+
// first step here is to have class name as key of attributes and always assume relative to imports
6234+
resolved_name = zend_resolve_class_name(key, ZEND_NAME_NOT_FQ);
6235+
6236+
ZVAL_NULL(&tmp);
6237+
val = zend_hash_add(resolved_attributes, resolved_name, &tmp);
6238+
6239+
if (val) {
6240+
array_init(val);
6241+
} else {
6242+
val = zend_hash_find(resolved_attributes, resolved_name);
6243+
}
6244+
6245+
zend_hash_next_index_insert(Z_ARRVAL_P(val), attr);
6246+
Z_TRY_ADDREF_P(attr);
6247+
6248+
} ZEND_HASH_FOREACH_END();
6249+
6250+
zend_hash_destroy(declared_attributes);
6251+
6252+
return resolved_attributes;
6253+
}
6254+
62196255
void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* {{{ */
62206256
{
62216257
zend_ast_decl *decl = (zend_ast_decl *) ast;
@@ -6251,7 +6287,7 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
62516287
op_array->doc_comment = zend_string_copy(decl->doc_comment);
62526288
}
62536289
if (decl->attributes) {
6254-
op_array->attributes = decl->attributes;
6290+
op_array->attributes = zend_compile_resolve_attributes(decl->attributes);
62556291
decl->attributes = NULL;
62566292
}
62576293
if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
@@ -6680,7 +6716,7 @@ zend_op *zend_compile_class_decl(zend_ast *ast, zend_bool toplevel) /* {{{ */
66806716
ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
66816717
}
66826718
if (decl->attributes) {
6683-
ce->info.user.attributes = decl->attributes;
6719+
ce->info.user.attributes = zend_compile_resolve_attributes(decl->attributes);
66846720
decl->attributes = NULL;
66856721
}
66866722

0 commit comments

Comments
 (0)