Skip to content

Instantly share code, notes, and snippets.

@kitsunet
Created June 9, 2015 16:52
Show Gist options
  • Save kitsunet/21910a8725a764029057 to your computer and use it in GitHub Desktop.
Save kitsunet/21910a8725a764029057 to your computer and use it in GitHub Desktop.
<?php
class Scripts {
/**
* @param string $commandIdentifier E.g. typo3.flow:cache:flush
* @param array $settings The TYPO3.Flow settings
* @param array $commandArguments Command arguments
*
* @return string A command line command ready for being exec()uted
*/
protected static function buildSubprocessCommand($commandIdentifier, $settings, array $commandArguments = array()) {
$subRequestEnvironmentVariables = array(
'FLOW_ROOTPATH' => FLOW_PATH_ROOT,
'FLOW_CONTEXT' => $settings['core']['context']
);
if (isset($settings['core']['subRequestEnvironmentVariables'])) {
$subRequestEnvironmentVariables = array_merge($subRequestEnvironmentVariables, $settings['core']['subRequestEnvironmentVariables']);
}
$command = '';
foreach ($subRequestEnvironmentVariables as $argumentKey => $argumentValue) {
if (DIRECTORY_SEPARATOR === '/') {
$command .= sprintf('%s=%s ', $argumentKey, escapeshellarg($argumentValue));
} else {
$command .= sprintf('SET %s=%s&', $argumentKey, escapeshellarg($argumentValue));
}
}
if (DIRECTORY_SEPARATOR === '/') {
$phpBinaryPathAndFilename = '"' . escapeshellcmd(\TYPO3\Flow\Utility\Files::getUnixStylePath($settings['core']['phpBinaryPathAndFilename'])) . '"';
} else {
$phpBinaryPathAndFilename = escapeshellarg(\TYPO3\Flow\Utility\Files::getUnixStylePath($settings['core']['phpBinaryPathAndFilename']));
}
$command .= $phpBinaryPathAndFilename;
if (!isset($settings['core']['subRequestPhpIniPathAndFilename']) || $settings['core']['subRequestPhpIniPathAndFilename'] !== FALSE) {
if (!isset($settings['core']['subRequestPhpIniPathAndFilename'])) {
$useIniFile = php_ini_loaded_file();
} else {
$useIniFile = $settings['core']['subRequestPhpIniPathAndFilename'];
}
$command .= ' -c ' . escapeshellarg($useIniFile);
}
$escapedArguments = '';
if ($commandArguments !== array()) {
foreach ($commandArguments as $argument=>$argumentValue) {
$escapedArguments .= ' ' . escapeshellarg('--' . trim($argument));
if (trim($argumentValue) !== '') {
$escapedArguments .= ' ' . escapeshellarg(trim($argumentValue));
}
}
}
$command .= sprintf(' %s %s %s', escapeshellarg(FLOW_PATH_FLOW . 'Scripts/flow.php'), escapeshellarg($commandIdentifier), trim($escapedArguments));
return trim($command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment