/** * Save our modified .yml file. * * @param boolean $makebackup Back up the file before commiting changes to it * * @throws IOException * * @return boolean true if save was successful */ protected function save($makebackup) { if (!$this->verify()) { return false; } // If we're backing up do it, if we can if ($makebackup) { $this->backup(); } // Update the YAML file if we can, or throw an error $this->file->update($this->yaml); return true; }
/** * Handle a file edit POST. * * @param Request $request * @param Form $form * @param File $file * @param string $type * * @return JsonResponse */ private function handleEdit(Request $request, Form $form, File $file, $type) { $form->submit($request->get($form->getName())); if ($form->isValid()) { $data = $form->getData(); $contents = Input::cleanPostedData($data['contents']) . "\n"; $result = ['ok' => true, 'msg' => 'Unhandled state.']; // Before trying to save a yaml file, check if it's valid. if ($type === 'yml') { $yamlparser = new Parser(); try { $yamlparser->parse($contents); } catch (ParseException $e) { $result['ok'] = false; $result['msg'] = Trans::__('page.file-management.message.save-failed-colon', ['%s' => $file->getPath()]) . $e->getMessage(); } } if ($result['ok']) { // Remove ^M (or \r) characters from the file. $contents = str_ireplace("\r", '', $contents); try { $file->update($contents); $result['msg'] = Trans::__('page.file-management.message.save-success', ['%s' => $file->getPath()]); $result['datechanged'] = $file->getCarbon()->toIso8601String(); } catch (ExceptionInterface $e) { $result['msg'] = Trans::__('page.file-management.message.save-failed-unknown', ['%s' => $file->getPath()]); } } } else { $result = ['ok' => false, 'msg' => Trans::__('page.file-management.message.save-failed-invalid-form', ['%s' => $file->getPath()])]; } return $this->json($result); }
public function testUpdate() { $path = 'temp/Spiderbait.txt'; $file = new File($this->filesystem, $path); $file->write(null); $file->update('Buy me a pony'); $newFile = new File($this->filesystem, $path); $this->assertSame('Buy me a pony', $newFile->read()); $file = new File($this->filesystem, $path); $file->update('Calypso'); $newFile = new File($this->filesystem, $path); $this->assertSame('Calypso', $newFile->read()); }