Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Created January 14, 2013 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlebkowski/4528954 to your computer and use it in GitHub Desktop.
Save mlebkowski/4528954 to your computer and use it in GitHub Desktop.
<?php
function compress($source, $type) {
global $lastError;
global $yuiPath;
$cmd = sprintf('java -jar %s --type %s --charset UTF-8 --line-break 1000', escapeshellarg($yuiPath), $type);
$process = proc_open(
$cmd,
array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
),
$pipes
);
fwrite($pipes[0], $source);
fclose($pipes[0]);
$output = array (
"stdout" => "",
"stderr" => "",
);
$readSockets = array (
"stdout" => $pipes[1],
"stderr" => $pipes[2]
);
$empty = array ();
while (false !== stream_select($readSockets, $empty, $empty, 1))
{
$eof = true;
foreach ($readSockets as $key => $stream)
{
$output[$key] = stream_get_contents($stream);
$meta = stream_get_meta_data($stream);
$eof = $eof && true === $meta['eof'];
}
if ($eof)
{
break;
}
$readSockets = array (
"stdout" => $pipes[1],
"stderr" => $pipes[2]
);
}
$compressed = $output['stdout'];
$errors = $output['stderr'];
$lastError = "" !== $errors;
if ($lastError)
{
$compressed = "";
$lastError = sprintf(
"alert('compression errors, check your source and console for details'); console.error(%s); ",
json_encode($errors)
);
}
proc_close($process);
return $compressed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment