2121/**
2222 * ODText writer
2323 */
24- class ODText implements IWriter
24+ class ODText extends Writer implements IWriter
2525{
26- /**
27- * PHPWord object
28- *
29- * @var \PhpOffice\PhpWord\PhpWord
30- */
31- private $ _document ;
32-
33- /**
34- * Individual writers
35- *
36- * @var \PhpOffice\PhpWord\Writer\ODText\WriterPart[]
37- */
38- private $ _writerParts ;
39-
4026 /**
4127 * Private unique PHPWord_Worksheet_BaseDrawing HashTable
4228 *
4329 * @var \PhpOffice\PhpWord\HashTable
4430 */
45- private $ _drawingHashTable ;
46-
47- /**
48- * Use disk caching where possible?
49- *
50- * @var boolean
51- */
52- private $ _useDiskCaching = false ;
53-
54- /**
55- * Disk caching directory
56- *
57- * @var string
58- */
59- private $ _diskCachingDirectory ;
31+ private $ drawingHashTable ;
6032
6133 /**
6234 * Create new ODText writer
@@ -67,24 +39,18 @@ public function __construct(PhpWord $phpWord = null)
6739 // Assign PhpWord
6840 $ this ->setPhpWord ($ phpWord );
6941
70- // Set up disk caching location
71- $ this ->_diskCachingDirectory = './ ' ;
72-
73- // Initialise writer parts
74- $ this ->_writerParts ['content ' ] = new Content ();
75- $ this ->_writerParts ['manifest ' ] = new Manifest ();
76- $ this ->_writerParts ['meta ' ] = new Meta ();
77- $ this ->_writerParts ['mimetype ' ] = new Mimetype ();
78- $ this ->_writerParts ['styles ' ] = new Styles ();
79-
80-
81- // Assign parent IWriter
82- foreach ($ this ->_writerParts as $ writer ) {
42+ // Set writer parts
43+ $ this ->writerParts ['content ' ] = new Content ();
44+ $ this ->writerParts ['manifest ' ] = new Manifest ();
45+ $ this ->writerParts ['meta ' ] = new Meta ();
46+ $ this ->writerParts ['mimetype ' ] = new Mimetype ();
47+ $ this ->writerParts ['styles ' ] = new Styles ();
48+ foreach ($ this ->writerParts as $ writer ) {
8349 $ writer ->setParentWriter ($ this );
8450 }
8551
8652 // Set HashTable variables
87- $ this ->_drawingHashTable = new HashTable ();
53+ $ this ->drawingHashTable = new HashTable ();
8854 }
8955
9056 /**
@@ -95,17 +61,8 @@ public function __construct(PhpWord $phpWord = null)
9561 */
9662 public function save ($ pFilename = null )
9763 {
98- if (!is_null ($ this ->_document )) {
99- // If $pFilename is php://output or php://stdout, make it a temporary file...
100- $ originalFilename = $ pFilename ;
101- if (strtolower ($ pFilename ) == 'php://output ' || strtolower ($ pFilename ) == 'php://stdout ' ) {
102- $ pFilename = @tempnam (sys_get_temp_dir (), 'phpword_ ' );
103- if ($ pFilename == '' ) {
104- $ pFilename = $ originalFilename ;
105- }
106- }
107-
108- // Create drawing dictionary
64+ if (!is_null ($ this ->phpWord )) {
65+ $ pFilename = $ this ->getTempFile ($ pFilename );
10966
11067 // Create new ZIP file and open it for writing
11168 $ objZip = new \ZipArchive ();
@@ -119,19 +76,19 @@ public function save($pFilename = null)
11976
12077 // Add mimetype to ZIP file
12178 //@todo Not in \ZipArchive::CM_STORE mode
122- $ objZip ->addFromString ('mimetype ' , $ this ->getWriterPart ('mimetype ' )->writeMimetype ($ this ->_document ));
79+ $ objZip ->addFromString ('mimetype ' , $ this ->getWriterPart ('mimetype ' )->writeMimetype ($ this ->phpWord ));
12380
12481 // Add content.xml to ZIP file
125- $ objZip ->addFromString ('content.xml ' , $ this ->getWriterPart ('content ' )->writeContent ($ this ->_document ));
82+ $ objZip ->addFromString ('content.xml ' , $ this ->getWriterPart ('content ' )->writeContent ($ this ->phpWord ));
12683
12784 // Add meta.xml to ZIP file
128- $ objZip ->addFromString ('meta.xml ' , $ this ->getWriterPart ('meta ' )->writeMeta ($ this ->_document ));
85+ $ objZip ->addFromString ('meta.xml ' , $ this ->getWriterPart ('meta ' )->writeMeta ($ this ->phpWord ));
12986
13087 // Add styles.xml to ZIP file
131- $ objZip ->addFromString ('styles.xml ' , $ this ->getWriterPart ('styles ' )->writeStyles ($ this ->_document ));
88+ $ objZip ->addFromString ('styles.xml ' , $ this ->getWriterPart ('styles ' )->writeStyles ($ this ->phpWord ));
13289
13390 // Add META-INF/manifest.xml
134- $ objZip ->addFromString ('META-INF/manifest.xml ' , $ this ->getWriterPart ('manifest ' )->writeManifest ($ this ->_document ));
91+ $ objZip ->addFromString ('META-INF/manifest.xml ' , $ this ->getWriterPart ('manifest ' )->writeManifest ($ this ->phpWord ));
13592
13693 // Add media. Has not used yet. Legacy from PHPExcel.
13794 // @codeCoverageIgnoreStart
@@ -173,111 +130,19 @@ public function save($pFilename = null)
173130 throw new Exception ("Could not close zip file $ pFilename. " );
174131 }
175132
176- // If a temporary file was used, copy it to the correct file stream
177- if ($ originalFilename != $ pFilename ) {
178- if (copy ($ pFilename , $ originalFilename ) === false ) {
179- throw new Exception ("Could not copy temporary zip file $ pFilename to $ originalFilename. " );
180- }
181- @unlink ($ pFilename );
182- }
183-
133+ $ this ->cleanupTempFile ();
184134 } else {
185135 throw new Exception ("PhpWord object unassigned. " );
186136 }
187137 }
188138
189- /**
190- * Get PhpWord object
191- *
192- * @return \PhpOffice\PhpWord\PhpWord
193- * @throws \PhpOffice\PhpWord\Exceptions\Exception
194- */
195- public function getPhpWord ()
196- {
197- if (!is_null ($ this ->_document )) {
198- return $ this ->_document ;
199- } else {
200- throw new Exception ("No PhpWord assigned. " );
201- }
202- }
203-
204- /**
205- * Set PhpWord object
206- *
207- * @param \PhpOffice\PhpWord\PhpWord $phpWord
208- * @return \PhpOffice\PhpWord\Writer\ODText
209- */
210- public function setPhpWord (PhpWord $ phpWord = null )
211- {
212- $ this ->_document = $ phpWord ;
213- return $ this ;
214- }
215-
216139 /**
217140 * Get PHPWord_Worksheet_BaseDrawing HashTable
218141 *
219142 * @return \PhpOffice\PhpWord\HashTable
220143 */
221144 public function getDrawingHashTable ()
222145 {
223- return $ this ->_drawingHashTable ;
224- }
225-
226- /**
227- * Get writer part
228- *
229- * @param string $pPartName Writer part name
230- * @return \PhpOffice\PhpWord\Writer\ODText\WriterPart
231- */
232- public function getWriterPart ($ pPartName = '' )
233- {
234- if ($ pPartName != '' && isset ($ this ->_writerParts [strtolower ($ pPartName )])) {
235- return $ this ->_writerParts [strtolower ($ pPartName )];
236- } else {
237- return null ;
238- }
239- }
240-
241- /**
242- * Get use disk caching where possible?
243- *
244- * @return boolean
245- */
246- public function getUseDiskCaching ()
247- {
248- return $ this ->_useDiskCaching ;
249- }
250-
251- /**
252- * Set use disk caching where possible?
253- *
254- * @param boolean $pValue
255- * @param string $pDirectory Disk caching directory
256- * @throws \PhpOffice\PhpWord\Exceptions\Exception Exception when directory does not exist
257- * @return \PhpOffice\PhpWord\Writer\ODText
258- */
259- public function setUseDiskCaching ($ pValue = false , $ pDirectory = null )
260- {
261- $ this ->_useDiskCaching = $ pValue ;
262-
263- if (!is_null ($ pDirectory )) {
264- if (is_dir ($ pDirectory )) {
265- $ this ->_diskCachingDirectory = $ pDirectory ;
266- } else {
267- throw new Exception ("Directory does not exist: $ pDirectory " );
268- }
269- }
270-
271- return $ this ;
272- }
273-
274- /**
275- * Get disk caching directory
276- *
277- * @return string
278- */
279- public function getDiskCachingDirectory ()
280- {
281- return $ this ->_diskCachingDirectory ;
146+ return $ this ->drawingHashTable ;
282147 }
283148}
0 commit comments