Skip to content

Commit

Permalink
Refactor: Create writers' Part folders and remove all static parts
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed Apr 30, 2014
1 parent 23db6fa commit 4a3400c
Show file tree
Hide file tree
Showing 48 changed files with 459 additions and 451 deletions.
2 changes: 1 addition & 1 deletion src/PhpWord/Element/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function addObject($src, $style = null)
if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') {
$ext = substr($ext, 0, -1);
}
$icon = realpath(__DIR__ . "/../_staticDocParts/_{$ext}.png");
$icon = realpath(__DIR__ . "/../resources/{$ext}.png");
$rId = Media::addElement($elementDocPart, 'object', $src);
$object->setRelationId($rId);
$rIdimg = Media::addElement($elementDocPart, 'image', $icon, new Image($icon));
Expand Down
49 changes: 16 additions & 33 deletions src/PhpWord/Reader/ODText/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,42 @@

namespace PhpOffice\PhpWord\Reader\ODText;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;

/**
* Abstract part reader
*/
abstract class AbstractPart
abstract class AbstractPart extends \PhpOffice\PhpWord\Reader\Word2007\AbstractPart
{
/**
* Document file
* Read w:r (override)
*
* @var string
* @param mixed $parent
* @param string $docPart
* @param mixed $pStyle
*/
protected $docFile;

/**
* XML file
*
* @var string
*/
protected $xmlFile;

/**
* Part relationships
*
* @var array
*/
protected $rels = array();
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $pStyle = null)
{
}

/**
* Read part
* Read w:pPr (override)
*/
abstract public function read(PhpWord &$phpWord);
protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
{
}

/**
* Create new instance
*
* @param string $docFile
* @param string $xmlFile
* Read w:rPr (override)
*/
public function __construct($docFile, $xmlFile)
protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
{
$this->docFile = $docFile;
$this->xmlFile = $xmlFile;
}

/**
* Set relationships
*
* @param array $value
* Read w:tblPr (override)
*/
public function setRels($value)
protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
{
$this->rels = $value;
}
}
23 changes: 10 additions & 13 deletions src/PhpWord/Writer/ODText.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Writer\ODText\Content;
use PhpOffice\PhpWord\Writer\ODText\Manifest;
use PhpOffice\PhpWord\Writer\ODText\Meta;
use PhpOffice\PhpWord\Writer\ODText\Mimetype;
use PhpOffice\PhpWord\Writer\ODText\Styles;

/**
* ODText writer
Expand All @@ -35,14 +30,16 @@ public function __construct(PhpWord $phpWord = null)
// Assign PhpWord
$this->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);
// Create parts
$parts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
foreach ($parts as $part) {
$partName = strtolower($part);
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $part;
if (class_exists($partClass)) {
$partObject = new $partClass();
$partObject->setParentWriter($this);
$this->writerParts[$partName] = $partObject;
}
}

// Set package paths
Expand Down
17 changes: 0 additions & 17 deletions src/PhpWord/Writer/ODText/AbstractWriterPart.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/PhpWord/Writer/ODText/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart;
use PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart;

/**
* Generic element writer
Expand All @@ -30,7 +30,7 @@ class Element
/**
* Parent writer
*
* @var \PhpOffice\PhpWord\Writer\ODText\AbstractWriterPart
* @var \PhpOffice\PhpWord\Writer\ODText\AbstractPart
*/
protected $parentWriter;

Expand All @@ -53,7 +53,7 @@ class Element
*
* @param bool $withoutP
*/
public function __construct(XMLWriter $xmlWriter, AbstractWriterPart $parentWriter, AbstractElement $element, $withoutP = false)
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, AbstractElement $element, $withoutP = false)
{
$this->xmlWriter = $xmlWriter;
$this->parentWriter = $parentWriter;
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Writer/ODText/Element/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Element;

use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\Element\Link as LinkElement;
use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Shared\XMLWriter;

/**
* ODT base part writer
*
* @since 0.10.0
* ODText writer part abstract
*/
class Base extends AbstractWriterPart
abstract class AbstractPart extends \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart
{
/**
* Write common root attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Style;
Expand All @@ -23,7 +23,7 @@
/**
* ODText content part writer
*/
class Content extends Base
class Content extends AbstractPart
{
/**
* Write content file to XML format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

use PhpOffice\PhpWord\Media;

/**
* ODText manifest part writer
*/
class Manifest extends AbstractWriterPart
class Manifest extends AbstractPart
{
/**
* Write Manifest file to XML format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\PhpWord;

/**
* ODText meta part writer
*/
class Meta extends AbstractWriterPart
class Meta extends AbstractPart
{
/**
* Write Meta file to XML format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

/**
* ODText mimetype part writer
*/
class Mimetype extends AbstractWriterPart
class Mimetype extends AbstractPart
{
/**
* Write Mimetype to Text format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Writer\ODText;
namespace PhpOffice\PhpWord\Writer\ODText\Part;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style;
Expand All @@ -16,7 +16,7 @@
/**
* ODText styloes part writer
*/
class Styles extends Base
class Styles extends AbstractPart
{
/**
* Write Styles file to XML format
Expand Down
48 changes: 16 additions & 32 deletions src/PhpWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
use PhpOffice\PhpWord\Writer\Word2007\DocProps;
use PhpOffice\PhpWord\Writer\Word2007\Document;
use PhpOffice\PhpWord\Writer\Word2007\Footer;
use PhpOffice\PhpWord\Writer\Word2007\Header;
use PhpOffice\PhpWord\Writer\Word2007\Notes;
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\Settings;
use PhpOffice\PhpWord\Writer\Word2007\Styles;
use PhpOffice\PhpWord\Writer\Word2007\WebSettings;

/**
* Word2007 writer
Expand Down Expand Up @@ -54,21 +43,18 @@ public function __construct(PhpWord $phpWord = null)
// Assign PhpWord
$this->setPhpWord($phpWord);

// Set writer parts
$this->writerParts['contenttypes'] = new ContentTypes();
$this->writerParts['rels'] = new Rels();
$this->writerParts['docprops'] = new DocProps();
$this->writerParts['document'] = new Document();
$this->writerParts['styles'] = new Styles();
$this->writerParts['numbering'] = new Numbering();
$this->writerParts['settings'] = new Settings();
$this->writerParts['websettings'] = new WebSettings();
$this->writerParts['header'] = new Header();
$this->writerParts['footer'] = new Footer();
$this->writerParts['footnotes'] = new Notes();
$this->writerParts['endnotes'] = new Notes();
foreach ($this->writerParts as $writer) {
$writer->setParentWriter($this);
// Create parts
$parts = array('ContentTypes', 'Rels', 'DocProps', 'Document', 'Styles',
'Numbering', 'Settings', 'WebSettings', 'Header', 'Footer', 'Footnotes',
'Endnotes', 'FontTable', 'Theme');
foreach ($parts as $part) {
$partName = strtolower($part);
$partClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Part\\' . $part;
if (class_exists($partClass)) {
$partObject = new $partClass();
$partObject->setParentWriter($this);
$this->writerParts[$partName] = $partObject;
}
}

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

// Write dynamic files
// Write parts
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->cTypes));
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels());
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord));
Expand All @@ -128,10 +114,8 @@ public function save($filename = null)
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering());
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings());
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings());

// Write static files
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
$objZip->addFromString('word/fontTable.xml', $this->getWriterPart('fonttable')->write());
$objZip->addFromString('word/theme/theme1.xml', $this->getWriterPart('theme')->write());

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

0 comments on commit 4a3400c

Please sign in to comment.