Ok, found some sort of a method which seemingly works - but I'd still like someone more knowledgeable to answer.
Anyways, the trick is - zombie
can accept a path to the nodejs
binary; so if you cannot really pass environment variables for nodejs
from PHP, then make a shell script which will set these environment variables, and then call nodejs
.
First this was my install:
# remove previous
sudo npm uninstall -g zombie --save-dev
sudo apt-get remove --purge nodejs && sudo apt-get autoremove --purge
# install new
curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
nvm install iojs-v3.3.1
npm list -g --depth=0
nvm install 4.0.0
npm list -g --depth=0
npm -g install zombie --save-dev
The problem with nvm
is that it installs in a user directory, and I'd like to test my scripts both on my user machine and remote server, where my uids are completely different. Regardless, using a custom executable helps a bit with that. So, create a script in a "global" location, I chose /home
, so I'll need sudo
to create files there:
sudo touch /home/node_pth.sh
... then paste in the following content:
#!/bin/bash
export NODE_PATH=/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules
#echo ARGS ARE "$@" | tee -a /tmp/node.log
/home/USERNAME/.nvm/versions/node/v4.0.0/bin/node "$@"
... of course, replacing the paths with your correct ones; then finally make it executable:
sudo chmod +x /home/node_pth.sh
Now we can use the following test_php_mink.php
PHP file:
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help
$nodePath = "/home/node_pth.sh"; # shell script that sets NODE_PATH, then calls node executable
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'
"; #
putenv("NODE_PATH=".$nodeModPath);
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'
"; # is there - but still doesn't help with call
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
echo "safe_mode: '" . ini_get("safe_mode") ."'
"; # have PHP 5.5.9, safe_mode is removed
$driver = new BehatMinkDriverombieDriver(
//~ new BehatMinkDriverNodeJSServerombieServer()
# copy defaults here for everything but nodeBin;
# see vendor/behat/mink-zombie-driver/src/NodeJS/Server.php
new BehatMinkDriverNodeJSServerombieServer("127.0.0.1", 8124, $nodePath, null)
);
$session = new BehatMinkSession($driver);
// start the session
$session->start();
?>
... OR, I just realized there is setNodeModulesPath($nodeModulesPath)
in vendor/behat/mink-zombie-driver/src/NodeJS/Server.php
, so we can drop the proxy bash executable altogether:
<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules"; # correct NODE_PATH, but will not help via putenv
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'
"; #
putenv("NODE_PATH=".$nodeModPath);
echo "NODE_PATH is: '" . getenv ( "NODE_PATH" ) . "'
"; # is there - but still doesn't help with call
# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';
echo "safe_mode: '" . ini_get("safe_mode") ."'
"; # have PHP 5.5.9, safe_mode is removed
$zsrv = new BehatMinkDriverNodeJSServerombieServer();
$zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
$driver = new BehatMinkDriverombieDriver( $zsrv );
$session = new BehatMinkSession($driver);
// start the session
$session->start();
?>
Anyways, when this script is called, it outputs:
$ php test_php_mink.php
NODE_PATH is: ''
NODE_PATH is: '/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules'
safe_mode: ''
... and as there are no errors, I'm assuming it is all fine now...