/** * Initializes the new file manager. * * @param string $path The path to the file. * @param string $mode The file open mode. * * @throws ResourceException If the file could not be opened. */ public function __construct($path, $mode) { $stream = fopen($path, $mode); if (!$stream) { throw new ResourceException("The file \"{$path}\" could not be opened ({$mode})."); } parent::__construct($stream); $this->mode = $mode; $this->path = $path; }
/** * Initializes the new in memory file manager. * * @param string $string The string contents. * @param boolean $append Append new contents? * * @throws ResourceException If the stream could not be created. */ public function __construct($string, $append) { $stream = fopen('php://memory', $append ? 'a+' : 'w+'); if (!$stream) { throw new ResourceException('A new in memory file stream could not be created.'); } parent::__construct($stream); $this->write($string); $this->seek(0); }
/** * Verify that the contents of the file can be written. */ public function testWriteTheFileContents() { $this->manager->write('test'); fseek($this->stream, 0); self::assertEquals('test', fread($this->stream, 4), 'The contents of the file were not written properly.'); }