PHP plugin extension

Hi everyone, I'm new to the forum so please be nice!

At my workplace we use a product that uses PHP scripts to extend functionality.  The scripts that it allows have a very specific variable naming format that is against the PHP specification, instead of starting with $, they start with something else.  The script is pre-processed before it's executed to change these instances into PHP variable names and do some basic transformation.

I'm just really not sure how to solve this problem and would appreciate some advice.

One approach I tried was writing a custom lexer and parser which basically splits the script into two token types: PHP and SPECIAL_VARIABLE, however for this to work I'd have to access the PHP plugin's lexer and parser, syntax highlighting, etc. - none of which is public API.

Another approach I was considering was writing a custom language that *only* matches the special variable token, and then using the language injection features of the IntelliJ platform to make it format these for PHP scripts.  The downsides of this approach would be that I don't think I'd be able to use a custom file format any more, and all PHP scripts would get this special behaviour.


For an example of the type of script that will be used:

$phpVar = 'whatever';
@@SPECIAL_VARIABLE = $phpVar;


After processing, it would end up like this before being executed by PHP:

$phpVar = 'whatever';
$this->specialVariables['SPECIAL_VARIABLE'] = $phpVar;


As you can see it's an extremely basic modification, I'm just unsure how to do this with the plugin framework!


Any advice on how to solve this problem would be really greatly appreciated.

Thanks for taking the time to read this!

Please sign in to leave a comment.