This commit is contained in:
Roman Syroeshko 2014-03-19 18:00:19 +04:00
parent 4e546d1a21
commit 32ed6a3b19
3 changed files with 63 additions and 146 deletions

View File

@ -25,134 +25,55 @@
* @version 0.8.0 * @version 0.8.0
*/ */
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Exceptions\Exception; use PhpOffice\PhpWord\Exceptions\Exception;
/** abstract class IOFactory
* Class PHPWord_IOFactory
*/
class PHPWord_IOFactory
{ {
/** /**
* Search locations * @param PHPWord $phpWord
* * @param string $name
* @var array * @return PhpOffice\PhpWord\Writer\IWriter
* @throws PhpOffice\PhpWord\Exceptions\Exception
*/ */
private static $_searchLocations = array( public static function createWriter(PHPWord $phpWord, $name)
array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}'),
array('type' => 'IReader', 'path' => 'PHPWord/Reader/{0}.php', 'class' => 'PHPWord_Reader_{0}' ),
);
/**
* Autoresolve classes
*
* @var array
*/
private static $_autoResolveClasses = array(
'Serialized'
);
/**
* Private constructor for PHPWord_IOFactory
*/
private function __construct()
{ {
} try {
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
/** return new $fqName($phpWord);
* Get search locations } catch (\Exception $ex) {
* throw new Exception("Could not instantiate \"{$name}\" class.");
* @return array
*/
public static function getSearchLocations()
{
return self::$_searchLocations;
}
/**
* Set search locations
*
* @param array $value
* @throws Exception
*/
public static function setSearchLocations(array $value)
{
self::$_searchLocations = $value;
}
/**
* Add search location
*
* @param string $type Example: IWriter
* @param string $location Example: PHPWord/Writer/{0}.php
* @param string $classname Example: PHPWord_Writer_{0}
*/
public static function addSearchLocation($type = '', $location = '', $classname = '')
{
self::$_searchLocations[] = array('type' => $type, 'path' => $location, 'class' => $classname);
}
/**
* Create PHPWord_Writer_IWriter
*
* @param PHPWord $PHPWord
* @param string $writerType Example: Word2007
* @return PHPWord_Writer_IWriter
* @throws Exception
*/
public static function createWriter(PHPWord $PHPWord, $writerType = '')
{
$searchType = 'IWriter';
foreach (self::$_searchLocations as $searchLocation) {
if ($searchLocation['type'] == $searchType) {
$className = str_replace('{0}', $writerType, $searchLocation['class']);
$classFile = str_replace('{0}', $writerType, $searchLocation['path']);
$instance = new $className($PHPWord);
if (!is_null($instance)) {
return $instance;
}
}
} }
throw new Exception("No $searchType found for type $writerType");
} }
/** /**
* Create PHPWord_Reader_IReader * @param string $name
* * @return PhpOffice\PhpWord\Reader\IReader
* @param string $readerType Example: Word2007 * @throws PhpOffice\PhpWord\Exceptions\Exception
* @return PHPWord_Reader_IReader
* @throws Exception
*/ */
public static function createReader($readerType = '') public static function createReader($name)
{ {
$searchType = 'IReader'; try {
$fqName = "PhpOffice\\PhpWord\\Reader\\{$name}";
foreach (self::$_searchLocations as $searchLocation) { return new $fqName();
if ($searchLocation['type'] == $searchType) { } catch (\Exception $ex) {
$className = str_replace('{0}', $readerType, $searchLocation['class']); throw new Exception("Could not instantiate \"{$name}\" class.");
$instance = new $className();
if ($instance !== null) {
return $instance;
}
}
} }
throw new Exception("No $searchType found for type $readerType");
} }
/** /**
* Loads PHPWord from file * Loads PhpWord from file
* *
* @param string $pFilename The name of the file * @param string $filename The name of the file
* @param string $readerType * @param string $readerName
* @return PHPWord * @return PhpOffice\PHPWord
*/ */
public static function load($pFilename, $readerType = 'Word2007') public static function load($filename, $readerName = 'Word2007')
{ {
$reader = self::createReader($readerType); $reader = self::createReader($readerName);
return $reader->load($pFilename); return $reader->load($filename);
} }
} }

View File

@ -1,59 +1,55 @@
<?php <?php
namespace PHPWord\Tests; namespace PHPWord\Tests;
use PHPWord; use PhpOffice\PHPWord;
use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Writer\Word2007;
use Exception;
/** /**
* @@coversDefaultClass PhpOffice\PhpWord\IOFactory
* @package PHPWord\Tests * @package PHPWord\Tests
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
*/ */
class IOFactoryTest extends \PHPUnit_Framework_TestCase final class IOFactoryTest extends \PHPUnit_Framework_TestCase
{ {
public function testGetSearchLocations() /**
* @covers ::createWriter
*/
final public function testExistingWriterCanBeCreated()
{ {
$this->assertAttributeEquals( $this->assertInstanceOf(
IOFactory::getSearchLocations(), 'PhpOffice\\PhpWord\\Writer\\Word2007',
'_searchLocations', IOFactory::createWriter(new PHPWord(), 'Word2007')
'PhpOffice\\PhpWord\\IOFactory'
);
}
public function testSetSearchLocationsWithArray()
{
IOFactory::setSearchLocations(array());
$this->assertAttributeEquals(array(), '_searchLocations', 'PhpOffice\\PhpWord\\IOFactory');
}
public function testAddSearchLocation()
{
IOFactory::setSearchLocations(array());
IOFactory::addSearchLocation('interface', 'classname');
$this->assertAttributeEquals(
array(array('interface' => 'interface', 'class' => 'classname')),
'_searchLocations',
'PhpOffice\\PhpWord\\IOFactory'
); );
} }
/** /**
* @expectedException Exception * @covers ::createWriter
* @expectedExceptionMessage No IWriter found for type * @expectedException PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not instantiate "Word2006" class.
*/ */
public function testCreateWriterException() final public function testNonexistentWriterCanNotBeCreated()
{ {
$oPHPWord = new PHPWord(); IOFactory::createWriter(new PHPWord(), 'Word2006');
IOFactory::setSearchLocations(array());
IOFactory::createWriter($oPHPWord);
} }
public function testCreateWriter() /**
* @covers ::createReader
*/
final public function testExistingReaderCanBeCreated()
{ {
$oPHPWord = new PHPWord(); $this->assertInstanceOf(
'PhpOffice\\PhpWord\\Reader\\Word2007',
IOFactory::createReader('Word2007')
);
}
$this->assertEquals(IOFactory::createWriter($oPHPWord, 'Word2007'), new Word2007($oPHPWord)); /**
* @covers ::createReader
* @expectedException PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not instantiate "Word2006" class.
*/
final public function testNonexistentReaderCanNotBeCreated()
{
IOFactory::createReader('Word2006');
} }
} }

View File

@ -13,14 +13,14 @@ class TestHelperDOCX
* @param \PHPWord $PHPWord * @param \PHPWord $PHPWord
* @return \PHPWord\Tests\XmlDocument * @return \PHPWord\Tests\XmlDocument
*/ */
public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007') public static function getDocument(PHPWord $PHPWord, $writerName = 'Word2007')
{ {
self::$file = tempnam(sys_get_temp_dir(), 'PHPWord'); self::$file = tempnam(sys_get_temp_dir(), 'PHPWord');
if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) { if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) {
mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
} }
$xmlWriter = IOFactory::createWriter($PHPWord, $writer); $xmlWriter = IOFactory::createWriter($PHPWord, $writerName);
$xmlWriter->save(self::$file); $xmlWriter->save(self::$file);
$zip = new \ZipArchive; $zip = new \ZipArchive;