Skip to content

Commit

Permalink
Don't assume that file binary exists on *nix OS
Browse files Browse the repository at this point in the history
Certain lightweight distributions such as Alpine Linux (popular for smaller Docker images) do not include it by default.
  • Loading branch information
teohhanhui committed Apr 12, 2018
1 parent 1067468 commit 88b9434
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,33 @@ public function __construct($cmd = 'file -b --mime %s 2>/dev/null')
*/
public static function isSupported()
{
return '\\' !== DIRECTORY_SEPARATOR && function_exists('passthru') && function_exists('escapeshellarg');
static $supported;

if (isset($supported)) {
return $supported;
}

if ('\\' === DIRECTORY_SEPARATOR || !function_exists('passthru') || !function_exists('escapeshellarg')) {
return $supported = false;
}

ob_start();

passthru('command -v file', $return);
if ($return > 0) {
ob_end_clean();

return $supported = false;
}

$binPath = trim(ob_get_clean());

if (!preg_match('#^/[^\0/]#', $binPath)) {
// it's not a bin path, but an error message
return $supported = false;
}

return $supported = true;
}

/**
Expand Down

0 comments on commit 88b9434

Please sign in to comment.