I lost completely by finding out why my PHP script produces different results when running through the CLI (getting the correct results) and when running in Node.js through the exec function.
Here is my php. Notice the line fopen('blah.txt', 'w')I'm using to check if the script reaches this place.
calculator.php
class calculator{
public static $_var = array();
public function calculate($xml, $xsl, $frequency){
$reader = new DOMDocument;
if(!$reader->load($xsl)) throw new Exception('XSL Could not be loaded: ' . $xsl);
$processor = new XSLTProcessor;
$processor->importStyleSheet($reader);
$processor->registerPHPFunctions();
$processor->setParameter('', 'frequency', $frequency);
$dictionary = new DOMDocument;
if(!$dictionary->load($xml)) throw new Exception('XML Could not be loaded: ' . $xml);
$processor->transformToXML($dictionary);
}
public static function connectNodes($name, $dataset, $processor, $destination, $frequency){
$var = self::$_var;
self::$_var = array();
$test = fopen('blah.txt', 'w');
fclose($test);
ob_start();
require 'templates/' . $processor . '.php';
$xsltSheet = new DOMDocument;
$xsltSheet->loadXML(ob_get_clean());
$xsltProc = new XSLTProcessor;
$xsltProc->importStyleSheet($xsltSheet);
$dataSet = new DOMDocument;
if(is_file('../dataFiles/xml/' . $dataset . '.xml') && calculator::makedirs('../dataFiles/json/' . $destination . '/' . $frequency)){
$dataSet->load('../dataFiles/xml/' . $dataset . '.xml');
if(!is_file('../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json')){
$filemaker = fopen('../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json', 'w');
fclose($filemaker);
}
$xsltProc->transformToURI($dataSet, '../dataFiles/json/' . $destination . '/' . $frequency . '/' . $name . '.json');
} else {
throw new Exception('Either dataset does not exist('. $dataset .'), or directories could not be created (dataFiles/json/'. $destination . '/' . $frequency .')');
}
}
public static function populateVarWithXPath($var){
self::$_var[] = $var;
}
public static function makedirs($dirpath, $mode=0777) {
return is_dir($dirpath) || mkdir($dirpath, $mode, true);
}
}
When I run this through the CLI, blah.txt is created in the directory where the script is running. But when I run it through Node.js, it does not create blah.txt!
Here is my node script:
script runner
var ipc = require('./ipc');
var exec = require('child_process').exec;
ipc.listen(function(frequency){
if(frequency.toString().match(/integrator /)){
var freq = frequency.toString().replace(/integrator /, '');
console.log('Received message: ' + freq);
var cmd = exec('php use_calculator.php metricsDictionary.xml metricsXmlReader.xsl ' + frequency);
cmd.on('close', function(exitCode, stdout, stderr){
if(exitCode === 0){
console.log('exitcode: ' + exitCode + ' ; stdout: ' + stdout + ' ; stderr: ' + stderr);
} else {
console.log('Try again?');
}
});
cmd.on('error', function(err){
throw err;
});
}
});
The funny thing is, if I translate my blah.txt into the calculate () function (which will be run first), both CLI and node will create a file.
A bit more context:
use_calculator.php calculate() . , calculate(), XML XSL .
XSL , XML, , connectNodes(), XML XSL. XML/XSL.
XML , , XML-. calculate(). , , connectNode() .
(), metricsXmlReader.xsl, connectNodes()
metricsXmlReader.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="metrics">
<xsl:for-each select="metric">
<xsl:if test="frequency = $frequency">
<xsl:for-each select="xpath/var">
<xsl:variable name="pusher" select="php:functionString('calculator::populateVarWithXPath', .)" />
</xsl:for-each>
<xsl:variable name="trigger" select="php:functionString('calculator::connectNodes', name, dataset, processor, destination, frequency)" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>