setPhpWord($phpWord); // Set writer parts $this->writerParts['content'] = new Content(); $this->writerParts['manifest'] = new Manifest(); $this->writerParts['meta'] = new Meta(); $this->writerParts['mimetype'] = new Mimetype(); $this->writerParts['styles'] = new Styles(); foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } } /** * Save PhpWord to file * * @param string $pFilename * @throws Exception */ public function save($pFilename = null) { if (!is_null($this->phpWord)) { $pFilename = $this->getTempFile($pFilename); // Create new ZIP file and open it for writing $zipClass = Settings::getZipClass(); $objZip = new $zipClass(); // Retrieve OVERWRITE and CREATE constants from the instantiated zip class // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP $ro = new \ReflectionObject($objZip); $zipOverWrite = $ro->getConstant('OVERWRITE'); $zipCreate = $ro->getConstant('CREATE'); // Remove any existing file if (file_exists($pFilename)) { unlink($pFilename); } // Try opening the ZIP file if ($objZip->open($pFilename, $zipOverWrite) !== true) { if ($objZip->open($pFilename, $zipCreate) !== true) { throw new Exception("Could not open " . $pFilename . " for writing."); } } // Add mimetype to ZIP file //@todo Not in \ZipArchive::CM_STORE mode $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->phpWord)); // Add content.xml to ZIP file $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord)); // Add meta.xml to ZIP file $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord)); // Add styles.xml to ZIP file $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Add META-INF/manifest.xml $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->phpWord)); // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file $pFilename."); } $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } } }