/** * Minify function. Returns a minified content, or writes it to a file if path is specified. * * @param null|string $file * @return string|true */ public function compile($path = null) { // create a file from the given path, or a temproary resource of a max of 20 Mb $this->res = $path ? new FileObject($path, 'w+') : new TempFileObject($this->options['tmp'] . '/' . StringUtils::randString(50)); // merge all resources in the minfier's resource foreach ($this->resources as $handle) { while (!$handle->eof()) { $chunk = $handle->read(8192); $this->res->write($chunk); } unset($handle); $this->res->write("\n\n"); } $this->res->rewind(); // return the minified text as a string or as a file $string = null !== $path ? true : $this->res->read(); return $string; }
/** * @expectedException OutOfBoundsException */ public function testIterator() { $write = 'Ana are mere'; $this->delete()->open(); for ($i = 1; $i < 10; $i++) { $this->assertEquals($this->file->writeLine($write . $i), fwrite($this->handle, $write . $i . "\n")); } $this->file->fileSeek(0); fseek($this->handle, 0); $this->assertEquals($this->file->tell(), ftell($this->handle)); $i = 0; foreach ($this->file as $line) { $this->assertEquals($this->file->key(), $i); $i++; } $this->file->rewind(); fseek($this->handle, 0); $this->assertEquals($this->file->tell(), ftell($this->handle)); $i = 0; foreach ($this->file as $line) { $linef = fgets($this->handle); $this->assertEquals($line, preg_replace("/[\n\r]+\$/", '', $linef)); $this->assertEquals($this->file->key(), $i); $i++; } $this->file->rewind(); $this->file->next(); fseek($this->handle, 0); $this->assertEquals($this->file->__toString(), preg_replace("/[\n\r]+\$/", '', fgets($this->handle))); $this->file->rewind(); $this->file->seek(3); $this->assertEquals($this->file->current(), $write . '3'); $this->file->seek(1000); $this->assertEquals($this->getExpectedException()->getMessage(), 'Could not jump to line 1000. Number of file lines is 9.'); $this->close(); }