在composer包中,我有安装后的cmd脚本,如下所示:
#!/bin/bash
echo 'Hello!'
read -p 'Database password: ' DB_PASS
php setup/index.php database_password=$DB_PASS
echo 'Complete!'
但在composer install
之后,我得到了这个错误:
...
Generating autoload files
> post-install-cmd: _scripts/ask_db_data.sh
Executing command (CWD): _scripts/ask_db_data.sh
Hello!
Script _scripts/ask_db_data.sh handling the post-install-cmd event returned with an error
[RuntimeException]
Error Output: Hello!
Exception trace:
() at phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:196
ComposerEventDispatcherEventDispatcher->doDispatch() at phar:///usr/local/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php:94
ComposerEventDispatcherEventDispatcher->dispatchScript() at phar:///usr/local/bin/composer/src/Composer/Installer.php:350
ComposerInstaller->run() at phar:///usr/local/bin/composer/src/Composer/Command/InstallCommand.php:134
ComposerCommandInstallCommand->execute() at phar:///usr/local/bin/composer/vendor/symfony/console/Command/Command.php:256
SymfonyComponentConsoleCommandCommand->run() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:838
SymfonyComponentConsoleApplication->doRunCommand() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:189
SymfonyComponentConsoleApplication->doRun() at phar:///usr/local/bin/composer/src/Composer/Console/Application.php:167
ComposerConsoleApplication->doRun() at phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:120
SymfonyComponentConsoleApplication->run() at phar:///usr/local/bin/composer/src/Composer/Console/Application.php:98
ComposerConsoleApplication->run() at phar:///usr/local/bin/composer/bin/composer:43
require() at /usr/local/bin/composer:25
如何在composer脚本中请求用户输入?
好的,您正在尝试执行bash/shell脚本来读取字符串并将其传递到php文件。我不确定,为什么不成功。
另一种选择是留在post-install-cmd
的PHP代码中。这是一个事件,它可以访问Composer和IO。要使用Console,您需要Composer中的$io
对象。您可以使用getIO()
获取它。
然后,您可以使用方法ask()
或askConfirmation()
以交互方式询问用户"缺少"的值。
非常简短:
$io = $this->getIO();
$user = $io->ask('Enter your Username: ');
/// Its better not displaying the password:
$pw = $io->askAndHideAnswer('Enter your Password: ');
exec('php setup/index.php database_password=' . escapeshellarg($pw));
获取用户输入的一种方法是将数据放入文件中,并在bash脚本中读取:
#!/bin/bash
source .env