private function writeToFile($path, $contents)
{
$dir = dirname($path);
if (!is_dir($dir)) {
if (file_exists($dir)) {
throw new \UnexpectedValueException($dir . ' exists and is not a directory.');
}
if (!@mkdir($dir, 0777, true)) {
throw new \UnexpectedValueException($dir . ' does not exist and could not be created.');
}
}
$retries = 3;
while ($retries--) {
try {
file_put_contents($path, $contents);
break;
} catch (\Exception $e) {
if ($retries) {
usleep(500000);
continue;
}
throw $e;
}
}
}