PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Use service container to get instance for new node

Open ovr opened this issue 8 years ago • 6 comments

For now it's not possible to use you own decorator for node in easy way by Parser component and it's rly hard to redeclare

class MyVariable extends Variable {
}
$serviceContainer = new ServiceContainer();
$serviceContainer->set('Node\Expr\Variable', 'MyVariable')

It's only an idea

ovr avatar Sep 14 '15 04:09 ovr

You could just use any container and add the nodes you use to it...

tleb avatar Sep 15 '15 18:09 tleb

Yeap I can use any container, but how are you going to redeclare it in parser?

ovr avatar Sep 15 '15 23:09 ovr

Redeclare? Could you explain what you are trying to do?

tleb avatar Sep 16 '15 12:09 tleb

@tleb I guess the idea here is to tell the parser to generate MyVariable objects instead of normal Variable objects for $var nodes (and similarly allow changing the object type for all other nodes as well).

I haven't tried yet, but I suspect that doing this will adversely affect performance. The current alternative is to translate normal nodes to custom ones in a visitor -- is there any problem with doing that?

nikic avatar Sep 16 '15 13:09 nikic

@nikic https://github.com/ovr/phpsa/blob/master/src/Definition/ClassDefinition.php I would like to write more functions to standart Class_ and use it by default in parser is it clear now? because I am not interested to write decorator or smthg else

I haven't tried yet, but I suspect that doing this will adversely affect performance. The current alternative is to translate normal nodes to custom ones in a visitor -- is there any problem with doing that?

I need to set/get all values from old node to new one :(

ovr avatar Sep 17 '15 07:09 ovr

Is this issue still valid in php-parser 4.0-alpha?

On second read, you might make use of setAttribute() method on node to get the extra work.

I do this instead of extending nodes with extra logic in like dozens of cases.

$node->setAttribute('metadata', '...');

What exactly do you need achieve?

TomasVotruba avatar Oct 28 '17 16:10 TomasVotruba