I'm logging this here for the sake of others being able to find/recognise the issue.
Symptom
My script:
#!/usr/bin/env php
<?php
function cv(...) // From the latest README.
// This call fails
eval(cv('php:boot', 'phpcode'));
// This succeeds:
eval(`cv php:boot`);
The error is:
[Exception]
Failed to locate civicrm.settings.php. By default, this tool searches the parent directories for a standard CMS (Drupal, WordPress, etal) and standard civicrm.settings.php. Symlinks andmul
tisite configurations may interfere. To customize, set variable CIVICRM_SETTINGS to point to the preferred civicrm.settings.php.
The environment is D7, CiviCRM 5.15.1, PHP 7.3 (in a VirtualBox).
Problem
The problem seems to be that the cv() function assumes that the script itself lives inside the CiviCRM webroot.
If your script is somewhere else (e.g. ~/bin/myscript) then because the proc_open call passes in __DIR__ the process runs from your ~/bin dir, and of course then it can't find CiviCRM.
Fix ideas
We could document that the script MUST be in the CiviCRM webroot to work.
in which case I think we should add some guards against it being run over http, e.g. I regularly use this at the top of cli scripts:
if (php_sapi_name() !== 'cli') {
// Fail with 404 if not called from CLI.
if (isset($_SERVER['HTTP_PROTOCOL'])) {
header("$_SERVER[HTTP_PROTOCOL] 404 Not Found");
}
exit;
}
Or we could make it an option to the demo cv function, e.g.
I'm logging this here for the sake of others being able to find/recognise the issue.
Symptom
My script:
The error is:
The environment is D7, CiviCRM 5.15.1, PHP 7.3 (in a VirtualBox).
Problem
The problem seems to be that the
cv()
function assumes that the script itself lives inside the CiviCRM webroot.If your script is somewhere else (e.g.
~/bin/myscript
) then because theproc_open
call passes in__DIR__
the process runs from your~/bin
dir, and of course then it can't find CiviCRM.Fix ideas
We could document that the script MUST be in the CiviCRM webroot to work.
in which case I think we should add some guards against it being run over http, e.g. I regularly use this at the top of cli scripts:
Or we could make it an option to the demo cv function, e.g.
That feels messy though.
How about using a constant: we could include this at the top: