Skip to content

Instantly share code, notes, and snippets.

@kostajh
Created March 28, 2014 16:30
Show Gist options
  • Save kostajh/9837004 to your computer and use it in GitHub Desktop.
Save kostajh/9837004 to your computer and use it in GitHub Desktop.
<?php
/**
* Write JSON encoded data to disk.
*/
protected function writeJson($filename, $data) {
if (!file_exists($this->getJsonDirectory())) {
if (!drush_mkdir($this->getJsonDirectory())) {
return drush_set_error('NO_JSON_DIR', dt('Unable to create JSON directory at !file', array('!file' => $this->getJsonDirectory())));
}
}
// Remove headers.
array_shift($data);
$json_filename = str_replace('xml', 'json', $filename);
$file = $this->getJsonDirectory() . '/' . $json_filename;
if (file_exists($file)) {
unlink($file);
}
$json_data = json_encode($data);
if (!file_put_contents($file, $json_data)) {
return drush_set_error(dt('Failed to write JSON for file !file', array('!file' => $json_filename)));
}
drush_log(dt('Wrote JSON to file !file', array('!file' => $file)), 'ok');
return TRUE;
}
@phucle2901
Copy link

fdg g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment