wp-cli / eval-command

Executes arbitrary PHP code or files.
MIT License
8 stars 15 forks source link

Add --use-include flag in order to overcome eval() limitations #49

Closed mklepaczewski closed 1 year ago

mklepaczewski commented 4 years ago

Feature Request

Describe your use case and the problem you are facing

eval-file uses eval() to execute code in provided script. Unfortunately, this has some limitations:

Describe the solution you'd like

I propose to add --use-require flag to eval-file to indicate that the script should be executed using require().

If the proposal is accepted I'm willing to implement it.

danielbachhuber commented 2 years ago

Thanks for the request, @mklepaczewski !

I propose to add --use-require flag to eval-file to indicate that the script should be executed using require().

Given the limitations you've described, your proposal seems reasonable.

Here's where wp eval-file switched from include( $file ) to eval(): https://github.com/wp-cli/eval-command/pull/21

Let's restore include( $file ) as an option, instead of using require().

kozer commented 1 year ago

@danielbachhuber I'll start working on it! Can you assign it to me, please? Thanks!

kozer commented 1 year ago

@danielbachhuber I have a question on this issue:

The eval command handles two scenarios:

include takes a file path as an input, so the --use-require makes sense in the first case. How we should handle the STDIN?

Let's say for example something like:

cat script.php | wp eval-file - x y z --skip-wordpress --use-require
danielbachhuber commented 1 year ago

include takes a file path as an input, so the --use-require makes sense in the first case. How we should handle the STDIN?

@kozer If the user provides wp eval-file --use-include -, then the command should call WP_CLI::error() and explain that both arguments can't be used at the same time.