Example #1
0
 public function write(Collection $items)
 {
     $path = $this->_getPath();
     $this->items = $items;
     //if (defined('JSON_PRETTY_PRINT')) { //PHP >= 5.4
     //	$json = json_encode($items->convertToArray(), JSON_PRETTY_PRINT); //JSON_PRETTY_PRINT: for debugging issues
     //} else {
     $json = json_encode($items->convertToArray());
     //}
     if ($json === false) {
         throw new \RuntimeException('Cannot encode data to JSON');
     }
     $parentPath = dirname($path);
     if (!is_dir($parentPath)) {
         mkdir($parentPath, 0777, true);
         chmod($parentPath, 0777);
     }
     $alreadyExists = file_exists($path);
     $result = file_put_contents($path, $json);
     if ($result === false) {
         throw new \RuntimeException('Cannot open data file "' . $this->index . '" ("' . $path . '" : error while writing)');
     }
     if (!$alreadyExists) {
         chmod($path, 0777);
     }
 }