Skip to content

Commit 56c3d8e

Browse files
author
Roman Syroeshko
committed
https://github.com/PHPOffice/PHPWord/issues/310
1 parent f25833c commit 56c3d8e

File tree

11 files changed

+85
-18
lines changed

11 files changed

+85
-18
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
4747
- Autoloader: Add the ability to set the autoloader options - @bskrtich GH-267
4848
- Element: Refactor elements to move set relation Id from container to element - @ivanlanin
4949
- Introduced CreateTemporaryFileException, CopyFileException - @RomanSyroeshko
50+
- Settings: added method to set user defined temporary directory - @RomanSyroeshko GH-310
5051

5152
## 0.11.1 - 2 June 2014
5253

phpword.ini.dist

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ compatibility = true
77
zipClass = ZipArchive
88
pdfRendererName = DomPDF
99
pdfRendererPath =
10+
; tempDir = "C:\PhpWordTemp"
1011

1112
[Font]
1213

samples/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$requirements = array(
44
'php' => array('PHP 5.3.0', version_compare(phpversion(), '5.3.0', '>=')),
55
'xml' => array('PHP extension XML', extension_loaded('xml')),
6-
'temp' => array('Temp folder "<code>' . sys_get_temp_dir() . '</code>" is writable', is_writable(sys_get_temp_dir())),
6+
'temp' => array('Temp folder "<code>' . Settings::getTempDir() . '</code>" is writable', is_writable(Settings::getTempDir())),
77
'zip' => array('PHP extension ZipArchive (optional)', extension_loaded('zip')),
88
'gd' => array('PHP extension GD (optional)', extension_loaded('gd')),
99
'xmlw' => array('PHP extension XMLWriter (optional)', extension_loaded('xmlwriter')),

src/PhpWord/Element/Image.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PhpOffice\PhpWord\Exception\InvalidImageException;
2121
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
22+
use PhpOffice\PhpWord\Settings;
2223
use PhpOffice\PhpWord\Shared\ZipArchive;
2324
use PhpOffice\PhpWord\Style\Image as ImageStyle;
2425

@@ -313,8 +314,8 @@ public function getImageStringData($base64 = false)
313314
if ($zip->open($zipFilename) !== false) {
314315
if ($zip->locateName($imageFilename)) {
315316
$isTemp = true;
316-
$zip->extractTo(sys_get_temp_dir(), $imageFilename);
317-
$actualSource = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $imageFilename;
317+
$zip->extractTo(Settings::getTempDir(), $imageFilename);
318+
$actualSource = Settings::getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
318319
}
319320
}
320321
$zip->close();
@@ -428,7 +429,7 @@ private function getArchiveImageSize($source)
428429
$imageData = null;
429430
$source = substr($source, 6);
430431
list($zipFilename, $imageFilename) = explode('#', $source);
431-
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
432+
$tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
432433

433434
$zip = new ZipArchive();
434435
if ($zip->open($zipFilename) !== false) {

src/PhpWord/Settings.php

+36
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class Settings
119119
*/
120120
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
121121

122+
/**
123+
* The user defined temporary directory.
124+
*
125+
* @var string
126+
*/
127+
private static $tempDir = '';
128+
122129
/**
123130
* Return the compatibility option used by the XMLWriter
124131
*
@@ -269,6 +276,35 @@ public static function setMeasurementUnit($value)
269276
return true;
270277
}
271278

279+
/**
280+
* Sets the user defined path to temporary directory.
281+
*
282+
* @param string $tempDir The user defined path to temporary directory.
283+
* @return void
284+
* @since 0.12.0
285+
*/
286+
public static function setTempDir($tempDir)
287+
{
288+
self::$tempDir = $tempDir;
289+
}
290+
291+
/**
292+
* Returns path to temporary directory.
293+
*
294+
* @return string
295+
* @since 0.12.0
296+
*/
297+
public static function getTempDir()
298+
{
299+
$tempDir = sys_get_temp_dir();
300+
301+
if (!empty(self::$tempDir)) {
302+
$tempDir = self::$tempDir;
303+
}
304+
305+
return $tempDir;
306+
}
307+
272308
/**
273309
* Get default font name
274310
*

src/PhpWord/Shared/ZipArchive.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct()
8383
$this->usePclzip = (Settings::getZipClass() != 'ZipArchive');
8484
if ($this->usePclzip) {
8585
if (!defined('PCLZIP_TEMPORARY_DIR')) {
86-
define('PCLZIP_TEMPORARY_DIR', sys_get_temp_dir() . '/');
86+
define('PCLZIP_TEMPORARY_DIR', Settings::getTempDir() . '/');
8787
}
8888
require_once 'PCLZip/pclzip.lib.php';
8989
}
@@ -139,7 +139,7 @@ public function open($filename, $flags = null)
139139
$this->numFiles = $zip->numFiles;
140140
} else {
141141
$zip = new \PclZip($this->filename);
142-
$this->tempDir = sys_get_temp_dir();
142+
$this->tempDir = Settings::getTempDir();
143143
$this->numFiles = count($zip->listContent());
144144
}
145145
$this->zip = $zip;

src/PhpWord/Template.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Template
6767
*/
6868
public function __construct($fileName)
6969
{
70-
$this->tempFileName = tempnam(sys_get_temp_dir(), '');
70+
$this->tempFileName = tempnam(Settings::getTempDir(), 'PhpWord');
7171
if (false === $this->tempFileName) {
7272
throw new CreateTemporaryFileException();
7373
}

src/PhpWord/Writer/AbstractWriter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpOffice\PhpWord\Exception\CopyFileException;
2121
use PhpOffice\PhpWord\Exception\Exception;
2222
use PhpOffice\PhpWord\PhpWord;
23+
use PhpOffice\PhpWord\Settings;
2324
use PhpOffice\PhpWord\Shared\ZipArchive;
2425

2526
/**
@@ -214,12 +215,12 @@ public function setTempDir($value)
214215
protected function getTempFile($filename)
215216
{
216217
// Temporary directory
217-
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
218+
$this->setTempDir(Settings::getTempDir() . '/PHPWordWriter/');
218219

219220
// Temporary file
220221
$this->originalFilename = $filename;
221222
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
222-
$filename = @tempnam(sys_get_temp_dir(), 'phpword_');
223+
$filename = tempnam(Settings::getTempDir(), 'phpword_');
223224
// @codeCoverageIgnoreStart
224225
// Can't find any test case. Uncomment when found.
225226
if ($filename == '') {

tests/PhpWord/Tests/SettingsTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/**
2323
* Test class for PhpOffice\PhpWord\Settings
2424
*
25+
* @coversDefaultClass \PhpOffice\PhpWord\Settings
2526
* @runTestsInSeparateProcesses
2627
*/
2728
class SettingsTest extends \PHPUnit_Framework_TestCase
@@ -70,6 +71,31 @@ public function testSetGetMeasurementUnit()
7071
$this->assertFalse(Settings::setMeasurementUnit('foo'));
7172
}
7273

74+
/**
75+
* @covers ::getTempDir
76+
* @test
77+
*/
78+
public function testPhpTempDirIsUsedByDefault()
79+
{
80+
$this->assertEquals(sys_get_temp_dir(), Settings::getTempDir());
81+
}
82+
83+
84+
/**
85+
* @covers ::setTempDir
86+
* @covers ::getTempDir
87+
* @depends testPhpTempDirIsUsedByDefault
88+
* @test
89+
*/
90+
public function testTempDirCanBeSet()
91+
{
92+
$userDefinedTempDir = 'C:\PhpWordTemp';
93+
Settings::setTempDir($userDefinedTempDir);
94+
$currentTempDir = Settings::getTempDir();
95+
$this->assertEquals($userDefinedTempDir, $currentTempDir);
96+
$this->assertNotEquals(sys_get_temp_dir(), $currentTempDir);
97+
}
98+
7399
/**
74100
* Test set/get default font name
75101
*/

tests/PhpWord/Tests/Writer/PDF/DomPDFTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testSetGetAbstractRendererProperties()
7171
$writer->setOrientation();
7272
$this->assertEquals('default', $writer->getOrientation());
7373

74-
$writer->setTempDir(sys_get_temp_dir());
75-
$this->assertEquals(sys_get_temp_dir(), $writer->getTempDir());
74+
$writer->setTempDir(Settings::getTempDir());
75+
$this->assertEquals(Settings::getTempDir(), $writer->getTempDir());
7676
}
7777
}

tests/PhpWord/Tests/_includes/TestHelperDOCX.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PhpOffice\PhpWord\IOFactory;
2121
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Settings;
2223

2324
/**
2425
* Test helper class
@@ -41,9 +42,9 @@ class TestHelperDOCX
4142
*/
4243
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
4344
{
44-
self::$file = tempnam(sys_get_temp_dir(), 'PhpWord');
45-
if (!is_dir(sys_get_temp_dir() . '/PhpWord_Unit_Test/')) {
46-
mkdir(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
45+
self::$file = tempnam(Settings::getTempDir(), 'PhpWord');
46+
if (!is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
47+
mkdir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
4748
}
4849

4950
$xmlWriter = IOFactory::createWriter($phpWord, $writerName);
@@ -52,11 +53,11 @@ public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
5253
$zip = new \ZipArchive;
5354
$res = $zip->open(self::$file);
5455
if ($res === true) {
55-
$zip->extractTo(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
56+
$zip->extractTo(Settings::getTempDir() . '/PhpWord_Unit_Test/');
5657
$zip->close();
5758
}
5859

59-
return new XmlDocument(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
60+
return new XmlDocument(Settings::getTempDir() . '/PhpWord_Unit_Test/');
6061
}
6162

6263
/**
@@ -67,8 +68,8 @@ public static function clear()
6768
if (file_exists(self::$file)) {
6869
unlink(self::$file);
6970
}
70-
if (is_dir(sys_get_temp_dir() . '/PhpWord_Unit_Test/')) {
71-
self::deleteDir(sys_get_temp_dir() . '/PhpWord_Unit_Test/');
71+
if (is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
72+
self::deleteDir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
7273
}
7374
}
7475

0 commit comments

Comments
 (0)