/** * Saves the current connection to `$this->file` if set. */ public function __destruct() { if (isset($this->file) && $this->file->isWritable()) { $this->file->ftruncate(0); $this->file->rewind(); $this->file->fwrite($this->toJson()); } }
/** * @return void */ public function testInit() { $filePath = __DIR__ . '/example_write1.txt'; $fileObject = new FileWriter($filePath); $this->assertEquals($filePath, $fileObject->getPathname()); $this->assertTrue($fileObject->isWritable()); $filePath = __DIR__ . '/example_write2.txt'; $fileObject = new \SplFileObject($filePath, 'r+'); $this->assertEquals($filePath, $fileObject->getPathname()); $this->assertTrue($fileObject->isWritable()); }
public function write($filename = null, Config $config = null, $exclusiveLock = null) { if (null !== $filename) { $this->setFilename($filename); } if (null !== $config) { $this->setConfig($config); } if (null !== $exclusiveLock) { $this->setExclusiveLock($exclusiveLock); } if (null === $this->_filename) { require_once '/Exception/Exception.php'; throw new MPFConfigException("没有设置文件名"); } if (null === $this->_filename) { require_once 'MPF/Core/Config/Exception.php'; throw new MPFConfigException("没有设置配置"); } $data = $this->_config->toArray(); $sectionName = $this->_config->getSectionName(); if (is_string($sectionName)) { $data = array($sectionName => $data); } $arrayString = "<?php\n" . "return " . var_export($data, true) . ";\n"; $flags = 0; if ($this->_exclusiveLock) { $flags |= LOCK_EX; } $file = new SplFileObject($this->_filename, 'w+'); $file->setFlags($flags); if ($file->isWritable()) { $result = $file->fwrite($arrayString); } else { require_once 'MPF/Core/Config/Exception.php'; throw new MPFConfigException("文件不可写"); } return $result; }
public static function create($file, $data, $has_clumn_name = false) { touch($file); $file = new SplFileObject($file, 'w'); if (count($data) > 0 && !$file->isWritable()) { delete($file); return false; } if ($has_clumn_name) { $file->fputcsv(array_keys($data[0])); } foreach ($data as $line) { $file->fputcsv($line); } return true; }
/** * Build RSpec teardown * @TODO: Complete function comment * * @param SplFileObject $rspecFileObj The file object of the test suite * * @throws {BadMethodCallException} If the $rspecFileObj param, wich is an SplFileObject object, stands for a non writable file. * * @return Void */ public function bindRspecTearDown($rspecFileObj) { if ($rspecFileObj->isWritable()) { $content = " after(:all) do\n"; $content .= " @valid.teardown()\n"; $content .= " end\n\n"; $rspecFileObj->fwrite($content); } else { throw new BadMethodCallException('Something went wrong when trying to write to test suite file "' . $this->_testSuiteFile . '".'); } }