Interactive PHP script with composer

1.5k Views Asked by At

I want to run one interactive PHP script after composer update or composer install automatically from composer.json file like:

"scripts": {
    "raven-cmd": [
        "php bin/console kolesar:boilerplate:setup"
    ],
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts",
        "@kolesar-cmd"
    ],
    "post-update-cmd": [
        "@auto-scripts",
        "@kolesar-cmd"
    ]
},

Symfony command php bin/console kolesar:boilerplate:setup is interactive script what expect some user inputs, but this does not work. The interactive mode does not works, exactly.

Any hint, how to solve this? Or maybe have another idea to force set up after composer update/install?

2

There are 2 best solutions below

0
On

On windows add < CON to the end of the command

0
On

You can simply put "< /dev/tty" after your script-call.

https://pantheon.io/blog/writing-composer-scripts:

There is another difference that may affect some commands, and that is that standard input will be attached to a TTY when the script is ran directly from the shell, but will be a redirected input stream when run through Composer. This can have various effects; for example, Symfony Console will not call the interact() method if there is no attached TTY. This could get in your way if, for example, you were trying to write functional tests that test interaction, as the consolidation/annotated-command project does. There are a couple of options for fixing this situation. The first is to explicitly specify that standard input should come from a TTY when running the command

This effectively gets Symfony Console to call the interact() method again; however, the down-side to this option is that it is not as portable

I just tried it and it works as expected.

Please note that the linked article also provides a more portable version, in case that is important for you.