Skip to content

Commit 4a3400c

Browse files
committed
Refactor: Create writers' Part folders and remove all static parts
1 parent 23db6fa commit 4a3400c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+459
-451
lines changed

src/PhpWord/Element/AbstractContainer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public function addObject($src, $style = null)
277277
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
278278
$ext = substr($ext, 0, -1);
279279
}
280-
$icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png");
280+
$icon = realpath(__DIR__ . "/../resources/{$ext}.png");
281281
$rId = Media::addElement($elementDocPart, 'object', $src);
282282
$object->setRelationId($rId);
283283
$rIdimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon));

src/PhpWord/Reader/ODText/AbstractPart.php

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,42 @@
99

1010
namespace PhpOffice\PhpWord\Reader\ODText;
1111

12-
use PhpOffice\PhpWord\PhpWord;
1312
use PhpOffice\PhpWord\Shared\XMLReader;
1413

1514
/**
1615
* Abstract part reader
1716
*/
18-
abstract class AbstractPart
17+
abstract class AbstractPart extends \PhpOffice\PhpWord\Reader\Word2007\AbstractPart
1918
{
2019
/**
21-
* Document file
20+
* Read w:r (override)
2221
*
23-
* @var string
22+
* @param mixed $parent
23+
* @param string $docPart
24+
* @param mixed $pStyle
2425
*/
25-
protected $docFile;
26-
27-
/**
28-
* XML file
29-
*
30-
* @var string
31-
*/
32-
protected $xmlFile;
33-
34-
/**
35-
* Part relationships
36-
*
37-
* @var array
38-
*/
39-
protected $rels = array();
26+
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $pStyle = null)
27+
{
28+
}
4029

4130
/**
42-
* Read part
31+
* Read w:pPr (override)
4332
*/
44-
abstract public function read(PhpWord &$phpWord);
33+
protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
34+
{
35+
}
4536

4637
/**
47-
* Create new instance
48-
*
49-
* @param string $docFile
50-
* @param string $xmlFile
38+
* Read w:rPr (override)
5139
*/
52-
public function __construct($docFile, $xmlFile)
40+
protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
5341
{
54-
$this->docFile = $docFile;
55-
$this->xmlFile = $xmlFile;
5642
}
5743

5844
/**
59-
* Set relationships
60-
*
61-
* @param array $value
45+
* Read w:tblPr (override)
6246
*/
63-
public function setRels($value)
47+
protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
6448
{
65-
$this->rels = $value;
6649
}
6750
}

src/PhpWord/Writer/ODText.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
use PhpOffice\PhpWord\Media;
1313
use PhpOffice\PhpWord\PhpWord;
1414
use PhpOffice\PhpWord\Exception\Exception;
15-
use PhpOffice\PhpWord\Writer\ODText\Content;
16-
use PhpOffice\PhpWord\Writer\ODText\Manifest;
17-
use PhpOffice\PhpWord\Writer\ODText\Meta;
18-
use PhpOffice\PhpWord\Writer\ODText\Mimetype;
19-
use PhpOffice\PhpWord\Writer\ODText\Styles;
2015

2116
/**
2217
* ODText writer
@@ -35,14 +30,16 @@ public function __construct(PhpWord $phpWord = null)
3530
// Assign PhpWord
3631
$this->setPhpWord($phpWord);
3732

38-
// Set writer parts
39-
$this->writerParts['content'] = new Content();
40-
$this->writerParts['manifest'] = new Manifest();
41-
$this->writerParts['meta'] = new Meta();
42-
$this->writerParts['mimetype'] = new Mimetype();
43-
$this->writerParts['styles'] = new Styles();
44-
foreach ($this->writerParts as $writer) {
45-
$writer->setParentWriter($this);
33+
// Create parts
34+
$parts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
35+
foreach ($parts as $part) {
36+
$partName = strtolower($part);
37+
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $part;
38+
if (class_exists($partClass)) {
39+
$partObject = new $partClass();
40+
$partObject->setParentWriter($this);
41+
$this->writerParts[$partName] = $partObject;
42+
}
4643
}
4744

4845
// Set package paths

src/PhpWord/Writer/ODText/AbstractWriterPart.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/PhpWord/Writer/ODText/Element/Element.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use PhpOffice\PhpWord\Element\AbstractElement;
1313
use PhpOffice\PhpWord\Shared\XMLWriter;
14-
use PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart;
14+
use PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart;
1515

1616
/**
1717
* Generic element writer
@@ -30,7 +30,7 @@ class Element
3030
/**
3131
* Parent writer
3232
*
33-
* @var \PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart
33+
* @var \PhpOffice\PhpWord\Writer\ODText\AbstractPart
3434
*/
3535
protected $parentWriter;
3636

@@ -53,7 +53,7 @@ class Element
5353
*
5454
* @param bool $withoutP
5555
*/
56-
public function __construct(XMLWriter $xmlWriter, AbstractWriterPart $parentWriter, AbstractElement $element, $withoutP = false)
56+
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, AbstractElement $element, $withoutP = false)
5757
{
5858
$this->xmlWriter = $xmlWriter;
5959
$this->parentWriter = $parentWriter;

src/PhpWord/Writer/ODText/Element/TextRun.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PhpOffice\PhpWord\Writer\ODText\Element;
1111

1212
use PhpOffice\PhpWord\Element\Text as TextElement;
13+
use PhpOffice\PhpWord\Element\Link as LinkElement;
1314
use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter;
1415

1516
/**

src/PhpWord/Writer/ODText/Base.php renamed to src/PhpWord/Writer/ODText/Part/AbstractPart.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
use PhpOffice\PhpWord\PhpWord;
1313
use PhpOffice\PhpWord\Style;
1414
use PhpOffice\PhpWord\Style\Font;
1515
use PhpOffice\PhpWord\Shared\XMLWriter;
1616

1717
/**
18-
* ODT base part writer
19-
*
20-
* @since 0.10.0
18+
* ODText writer part abstract
2119
*/
22-
class Base extends AbstractWriterPart
20+
abstract class AbstractPart extends \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart
2321
{
2422
/**
2523
* Write common root attributes

src/PhpWord/Writer/ODText/Content.php renamed to src/PhpWord/Writer/ODText/Part/Content.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
use PhpOffice\PhpWord\Media;
1313
use PhpOffice\PhpWord\Style;
@@ -23,7 +23,7 @@
2323
/**
2424
* ODText content part writer
2525
*/
26-
class Content extends Base
26+
class Content extends AbstractPart
2727
{
2828
/**
2929
* Write content file to XML format

src/PhpWord/Writer/ODText/Manifest.php renamed to src/PhpWord/Writer/ODText/Part/Manifest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
use PhpOffice\PhpWord\Media;
1313

1414
/**
1515
* ODText manifest part writer
1616
*/
17-
class Manifest extends AbstractWriterPart
17+
class Manifest extends AbstractPart
1818
{
1919
/**
2020
* Write Manifest file to XML format

src/PhpWord/Writer/ODText/Meta.php renamed to src/PhpWord/Writer/ODText/Part/Meta.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
use PhpOffice\PhpWord\Exception\Exception;
1313
use PhpOffice\PhpWord\PhpWord;
1414

1515
/**
1616
* ODText meta part writer
1717
*/
18-
class Meta extends AbstractWriterPart
18+
class Meta extends AbstractPart
1919
{
2020
/**
2121
* Write Meta file to XML format

src/PhpWord/Writer/ODText/Mimetype.php renamed to src/PhpWord/Writer/ODText/Part/Mimetype.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
/**
1313
* ODText mimetype part writer
1414
*/
15-
class Mimetype extends AbstractWriterPart
15+
class Mimetype extends AbstractPart
1616
{
1717
/**
1818
* Write Mimetype to Text format

src/PhpWord/Writer/ODText/Styles.php renamed to src/PhpWord/Writer/ODText/Part/Styles.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88
*/
99

10-
namespace PhpOffice\PhpWord\Writer\ODText;
10+
namespace PhpOffice\PhpWord\Writer\ODText\Part;
1111

1212
use PhpOffice\PhpWord\PhpWord;
1313
use PhpOffice\PhpWord\Style;
@@ -16,7 +16,7 @@
1616
/**
1717
* ODText styloes part writer
1818
*/
19-
class Styles extends Base
19+
class Styles extends AbstractPart
2020
{
2121
/**
2222
* Write Styles file to XML format

src/PhpWord/Writer/Word2007.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
1313
use PhpOffice\PhpWord\Media;
1414
use PhpOffice\PhpWord\Element\Section;
1515
use PhpOffice\PhpWord\Exception\Exception;
16-
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
17-
use PhpOffice\PhpWord\Writer\Word2007\DocProps;
18-
use PhpOffice\PhpWord\Writer\Word2007\Document;
19-
use PhpOffice\PhpWord\Writer\Word2007\Footer;
20-
use PhpOffice\PhpWord\Writer\Word2007\Header;
21-
use PhpOffice\PhpWord\Writer\Word2007\Notes;
22-
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
23-
use PhpOffice\PhpWord\Writer\Word2007\Rels;
24-
use PhpOffice\PhpWord\Writer\Word2007\Settings;
25-
use PhpOffice\PhpWord\Writer\Word2007\Styles;
26-
use PhpOffice\PhpWord\Writer\Word2007\WebSettings;
2716

2817
/**
2918
* Word2007 writer
@@ -54,21 +43,18 @@ public function __construct(PhpWord $phpWord = null)
5443
// Assign PhpWord
5544
$this->setPhpWord($phpWord);
5645

57-
// Set writer parts
58-
$this->writerParts['contenttypes'] = new ContentTypes();
59-
$this->writerParts['rels'] = new Rels();
60-
$this->writerParts['docprops'] = new DocProps();
61-
$this->writerParts['document'] = new Document();
62-
$this->writerParts['styles'] = new Styles();
63-
$this->writerParts['numbering'] = new Numbering();
64-
$this->writerParts['settings'] = new Settings();
65-
$this->writerParts['websettings'] = new WebSettings();
66-
$this->writerParts['header'] = new Header();
67-
$this->writerParts['footer'] = new Footer();
68-
$this->writerParts['footnotes'] = new Notes();
69-
$this->writerParts['endnotes'] = new Notes();
70-
foreach ($this->writerParts as $writer) {
71-
$writer->setParentWriter($this);
46+
// Create parts
47+
$parts = array('ContentTypes', 'Rels', 'DocProps', 'Document', 'Styles',
48+
'Numbering', 'Settings', 'WebSettings', 'Header', 'Footer', 'Footnotes',
49+
'Endnotes', 'FontTable', 'Theme');
50+
foreach ($parts as $part) {
51+
$partName = strtolower($part);
52+
$partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $part;
53+
if (class_exists($partClass)) {
54+
$partObject = new $partClass();
55+
$partObject->setParentWriter($this);
56+
$this->writerParts[$partName] = $partObject;
57+
}
7258
}
7359

7460
// Set package paths
@@ -117,7 +103,7 @@ public function save($filename = null)
117103
$this->addNotes($objZip, $rId, 'footnote');
118104
$this->addNotes($objZip, $rId, 'endnote');
119105

120-
// Write dynamic files
106+
// Write parts
121107
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->cTypes));
122108
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels());
123109
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord));
@@ -128,10 +114,8 @@ public function save($filename = null)
128114
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering());
129115
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings());
130116
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings());
131-
132-
// Write static files
133-
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
134-
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
117+
$objZip->addFromString('word/fontTable.xml', $this->getWriterPart('fonttable')->write());
118+
$objZip->addFromString('word/theme/theme1.xml', $this->getWriterPart('theme')->write());
135119

136120
// Close file
137121
if ($objZip->close() === false) {
@@ -215,7 +199,7 @@ private function addNotes($objZip, &$rId, $notesType = 'footnote')
215199
$objZip->addFromString($relsFile, $this->getWriterPart('rels')->writeMediaRels($media));
216200
}
217201
$elements = $collection::getElements();
218-
$objZip->addFromString($xmlPath, $this->getWriterPart($notesTypes)->writeNotes($elements, $notesTypes));
202+
$objZip->addFromString($xmlPath, $this->getWriterPart($notesTypes)->write($elements));
219203
$this->cTypes['override']["/{$xmlPath}"] = $notesTypes;
220204
$this->docRels[] = array('target' => $xmlFile, 'type' => $notesTypes, 'rID' => ++$rId);
221205
}

0 commit comments

Comments
 (0)