Created
January 14, 2013 09:47
-
-
Save mlebkowski/4528954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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