Skip to content

Commit 4127860

Browse files
committed
Refactor writers and readers
- Create Writer abstract class - Inherit writers class from Writer - Inherit ODText\WriterPart from Word2007\WriterPart - Rename AbstractReader > Reader
1 parent 0e2f476 commit 4127860

23 files changed

+311
-502
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
2121
### Miscellaneous
2222

2323
- Documentation: Simplify page level docblock - @ivanlanin GH-179
24+
- Writer: Refactor writer classes and make a new Writer abstract class - @ivanlanin GH-160
25+
- Reader: Rename AbstractReader > Reader - @ivanlanin
2426

2527
## 0.9.1 - 27 Mar 2014
2628

src/PhpWord/Reader/AbstractReader.php renamed to src/PhpWord/Reader/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @codeCoverageIgnore Abstract class
1818
*/
19-
abstract class AbstractReader implements IReader
19+
abstract class Reader implements IReader
2020
{
2121
/**
2222
* Read data only?

src/PhpWord/Reader/Word2007.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* Reader for Word2007
1818
*/
19-
class Word2007 extends AbstractReader implements IReader
19+
class Word2007 extends Reader implements IReader
2020
{
2121
/**
2222
* Can the current IReader read the file?

src/PhpWord/Writer/ODText.php

Lines changed: 19 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,14 @@
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
}

src/PhpWord/Writer/ODText/Content.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ class Content extends WriterPart
4141
public function writeContent(PhpWord $phpWord = null)
4242
{
4343
// Create XML writer
44-
$xmlWriter = null;
45-
if ($this->getParentWriter()->getUseDiskCaching()) {
46-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
47-
} else {
48-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
49-
}
44+
$xmlWriter = $this->getXmlWriter();
5045

5146
// XML header
5247
$xmlWriter->startDocument('1.0', 'UTF-8');

src/PhpWord/Writer/ODText/Manifest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ class Manifest extends WriterPart
2727
public function writeManifest(PhpWord $phpWord = null)
2828
{
2929
// Create XML writer
30-
$xmlWriter = null;
31-
if ($this->getParentWriter()->getUseDiskCaching()) {
32-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
33-
} else {
34-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
35-
}
30+
$xmlWriter = $this->getXmlWriter();
3631

3732
// XML header
3833
$xmlWriter->startDocument('1.0', 'UTF-8');

src/PhpWord/Writer/ODText/Meta.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ class Meta extends WriterPart
2626
public function writeMeta(PhpWord $phpWord = null)
2727
{
2828
// Create XML writer
29-
$xmlWriter = null;
30-
if ($this->getParentWriter()->getUseDiskCaching()) {
31-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
32-
} else {
33-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
34-
}
29+
$xmlWriter = $this->getXmlWriter();
3530

3631
// XML header
3732
$xmlWriter->startDocument('1.0', 'UTF-8');

src/PhpWord/Writer/ODText/Styles.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ class Styles extends WriterPart
3030
public function writeStyles(PhpWord $phpWord = null)
3131
{
3232
// Create XML writer
33-
$xmlWriter = null;
34-
if ($this->getParentWriter()->getUseDiskCaching()) {
35-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
36-
} else {
37-
$xmlWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
38-
}
33+
$xmlWriter = $this->getXmlWriter();
3934

4035
// XML header
4136
$xmlWriter->startDocument('1.0', 'UTF-8');

src/PhpWord/Writer/ODText/WriterPart.php

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,9 @@
99

1010
namespace PhpOffice\PhpWord\Writer\ODText;
1111

12-
use PhpOffice\PhpWord\Exceptions\Exception;
13-
use PhpOffice\PhpWord\Writer\IWriter;
14-
1512
/**
1613
* ODText writer part abstract
1714
*/
18-
abstract class WriterPart
15+
abstract class WriterPart extends \PhpOffice\PhpWord\Writer\Word2007\WriterPart
1916
{
20-
/**
21-
* Parent IWriter object
22-
*
23-
* @var \PhpOffice\PhpWord\Writer\IWriter
24-
*/
25-
private $_parentWriter;
26-
27-
/**
28-
* Set parent IWriter object
29-
*
30-
* @param \PhpOffice\PhpWord\Writer\IWriter $pWriter
31-
*/
32-
public function setParentWriter(IWriter $pWriter = null)
33-
{
34-
$this->_parentWriter = $pWriter;
35-
}
36-
37-
/**
38-
* Get parent IWriter object
39-
*
40-
* @return \PhpOffice\PhpWord\Writer\IWriter
41-
* @throws \PhpOffice\PhpWord\Exceptions\Exception
42-
*/
43-
public function getParentWriter()
44-
{
45-
if (!is_null($this->_parentWriter)) {
46-
return $this->_parentWriter;
47-
} else {
48-
throw new Exception("No parent IWriter assigned.");
49-
}
50-
}
5117
}

0 commit comments

Comments
 (0)