Skip to content

Commit

Permalink
Issue mcustiel#16: Log file is not created with PCNTL disabled (mcust…
Browse files Browse the repository at this point in the history
  • Loading branch information
drudoi authored and mcustiel committed Sep 24, 2017
1 parent 9ba67a2 commit cb5537a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 58 deletions.
7 changes: 2 additions & 5 deletions composer.json
Expand Up @@ -26,13 +26,10 @@
"type" : "project",
"description" : "Codeception extension for PhireMock. Allows to stub remote services for HTTP requests.",
"license" : "GPL-3.0+",
"require" : {
"require" : {
"php" : ">=5.6",
"mcustiel/phiremock": "^1.3",
"codeception/codeception" : "^2.0",
"symfony/process": "^3.1|^2.7.15"
},
"suggest" : {
"ext-pcntl" : "Allows phiremock-codeception-extension to send system signals to phiremock process"
"symfony/process": "^3.3"
}
}
74 changes: 21 additions & 53 deletions src/Extension/PhiremockProcess.php
Expand Up @@ -52,32 +52,20 @@ public function start($ip, $port, $path, $logsPath, $debug, $expectationsPath)
$phiremockPath = is_file($path) ? $path : $path . DIRECTORY_SEPARATOR . 'phiremock';
$expectationsPath = is_dir($expectationsPath) ? $expectationsPath : '';

$this->logPhiremockCommand($ip, $port, $debug, $expectationsPath, $phiremockPath);
$this->initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath);
$this->logPhiremockCommand($debug);
$logFile = $logsPath . DIRECTORY_SEPARATOR . self::LOG_FILE_NAME;
$this->process->start(function ($type, $buffer) use ($logFile) {
file_put_contents($logFile, $buffer, FILE_APPEND);
});
$this->setUpProcessCompatibility();
}

/**
* Stops the process.
*/
public function stop()
{
if ($this->isPcntlEnabled()) {
$this->process->signal(SIGTERM);
$this->process->stop(3, SIGKILL);
}
}

private function setUpProcessCompatibility()
{
$this->process->setEnhanceSigchildCompatibility(true);
if ($this->isWindows()) {
$this->process->setEnhanceWindowsCompatibility(true);
}
$this->process->stop(3);
}

/**
Expand All @@ -89,52 +77,32 @@ private function setUpProcessCompatibility()
*/
private function initProcess($ip, $port, $debug, $expectationsPath, $phiremockPath)
{
$this->process = new Process(
$this->getCommandPrefix()
. "{$phiremockPath} -i {$ip} -p {$port}"
. ($debug ? ' -d' : '')
. ($expectationsPath ? " -e {$expectationsPath}" : '')
);
}

/**
* @param string $ip
* @param int $port
* @param bool $debug
* @param string $expectationsPath
* @param string $phiremockPath
*/
private function logPhiremockCommand($ip, $port, $debug, $expectationsPath, $phiremockPath)
{
$commandline = [
$phiremockPath,
'-i',
$ip,
'-p',
$port
];
if ($debug) {
echo 'Running ' . $this->getCommandPrefix()
. "{$phiremockPath} -i {$ip} -p {$port}"
. ($debug ? ' -d' : '')
. ($expectationsPath ? " -e {$expectationsPath}" : '') . PHP_EOL;
$commandline[] = '-d';
}
if ($expectationsPath) {
$commandline[] = '-e';
$commandline[] = $expectationsPath;
}
}

/**
* @return string
*/
private function getCommandPrefix()
{
return $this->isWindows() ? '' : 'exec ';
}

/**
* @return bool
*/
private function isWindows()
{
return PHP_OS === 'WIN32' || PHP_OS === 'WINNT' || PHP_OS === 'Windows';
// Process wraps the command with 'exec' in UNIX OSs.
$this->process = new Process($commandline);
}

/**
* @return bool
* @param bool $debug
*/
private function isPcntlEnabled()
private function logPhiremockCommand($debug)
{
return !$this->isWindows() && defined('SIGTERM');
if ($debug) {
echo 'Running ' . $this->process->getCommandLine() . PHP_EOL;
}
}
}

0 comments on commit cb5537a

Please sign in to comment.