Created
March 28, 2014 16:30
-
-
Save kostajh/9837004 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 | |
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fdg g