Skip to content

feat: '-r' option for php-cli #1482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 1, 2025
Merged

feat: '-r' option for php-cli #1482

merged 8 commits into from
May 1, 2025

Conversation

AlliBalliBaba
Copy link
Collaborator

This PR adds the -r option for directly executing code with php-cli

frankenphp php-cli -r "echo 'Hello world'";

This can be useful in some situations where PHP is not directly installed on the system, but some library setup code uses the -r flag

The main reason I'm adding this option though is to have another sanity check for the static binaries after compilation.

@AlliBalliBaba
Copy link
Collaborator Author

As expected mac/arm currently fails the sanity check https://github.com/dunglas/frankenphp/actions/runs/14283719364/job/40036289078?pr=1482

@withinboredom
Copy link
Collaborator

withinboredom commented Apr 12, 2025

I wonder if it would make sense to just enter the cli sapi if argv[0] is frankenphp-cli? Thus creating a soft-link to frankenphp with the name frankenphp-cli would enable you to just run it as though it were statically built, normal php.

Edit: Or actually, if it is just php. So then scripts don't even need to be modified.

@AlliBalliBaba
Copy link
Collaborator Author

Yeah I thought of that too, though I'm not sure what would be the best way to do this, maybe by somehow exposing the main function directly in php-src?

@@ -19,13 +19,13 @@ func init() {
Long: `
Executes a PHP script similarly to the CLI SAPI.`,
CobraFunc: func(cmd *cobra.Command) {
cmd.DisableFlagParsing = true
cmd.Flags().StringP("code", "r", "", "Execute PHP code directly without <php ... ?> tags")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php you mean

@AlliBalliBaba
Copy link
Collaborator Author

Nvm mac sanity check was failing due to a typo 😅

@withinboredom
Copy link
Collaborator

Yeah I thought of that too, though I'm not sure what would be the best way to do this, maybe by somehow exposing the main function directly in php-src?

Or just copy-paste, er, vendor it.

@henderkes
Copy link
Contributor

@AlliBalliBaba any chance you can verify that the -march-native fixes the issue? I don't have a piece of hardware that can recreate the illegal instruction, so that was only a shot in the dark by me as a possible explanation.

@AlliBalliBaba
Copy link
Collaborator Author

Sadly, I don't have a mac either, but I can try asking a colleague tomorrow.
It looks fine in the pipeline though.

@Anemys
Copy link

Anemys commented Apr 22, 2025

Hey!
Your PR is very useful, it would fix an issue I just encoutered, with that everything would work perfectly.
While we’re waiting for it to be merged, I’ve put together a little workaround that extends the script mentioned in the docs:
👉 https://frankenphp.dev/docs/known-issues/#composer-scripts-referencing-php

It’s a bit of a ninja patchwork, but it gets the job done for now 😄

#!/usr/bin/env bash
args=("$@")
index=0
filename=
for i in "$@"
do
    if [ "$i" == "-d" ]; then
        unset 'args[$index]'
        unset 'args[$index+1]'
    fi

    if [ "$i" == "-r" ]; then
	filename=$(cat /proc/sys/kernel/random/uuid).temp.php
	echo "<?php ${args[$((index+1))]} ?>" | tee "$filename" > /dev/null
        unset 'args[$index]'
        unset 'args[$index+1]'
	args=("$filename")
	break
    fi

    index=$((index+1))
done

/usr/bin/frankenphp php-cli ${args[@]}

if [ -n "$filename" ]; then
    rm -f "$filename"
    echo
fi

Thank you @Lolozendev for your help

@dunglas
Copy link
Owner

dunglas commented Apr 29, 2025

Can we merge this one @AlliBalliBaba?

@AlliBalliBaba
Copy link
Collaborator Author

Yeah I think so 👍 . Just forwarding the input to php-cli is something for a different PR

@dunglas dunglas merged commit 3741782 into main May 1, 2025
58 checks passed
@dunglas dunglas deleted the feat/php-cli-code branch May 1, 2025 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants