Refactored usages of "tempnam()" function.
This commit is contained in:
parent
56c3d8eda2
commit
e6d88a27e8
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
namespace PhpOffice\PhpWord\Element;
|
namespace PhpOffice\PhpWord\Element;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||||
use PhpOffice\PhpWord\Exception\InvalidImageException;
|
use PhpOffice\PhpWord\Exception\InvalidImageException;
|
||||||
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
|
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Settings;
|
||||||
@ -423,13 +424,18 @@ class Image extends AbstractElement
|
|||||||
*
|
*
|
||||||
* @param string $source
|
* @param string $source
|
||||||
* @return array|null
|
* @return array|null
|
||||||
|
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
|
||||||
*/
|
*/
|
||||||
private function getArchiveImageSize($source)
|
private function getArchiveImageSize($source)
|
||||||
{
|
{
|
||||||
$imageData = null;
|
$imageData = null;
|
||||||
$source = substr($source, 6);
|
$source = substr($source, 6);
|
||||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||||
|
|
||||||
$tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
|
$tempFilename = tempnam(Settings::getTempDir(), 'PHPWordImage');
|
||||||
|
if (false === $tempFilename) {
|
||||||
|
throw new CreateTemporaryFileException();
|
||||||
|
}
|
||||||
|
|
||||||
$zip = new ZipArchive();
|
$zip = new ZipArchive();
|
||||||
if ($zip->open($zipFilename) !== false) {
|
if ($zip->open($zipFilename) !== false) {
|
||||||
@ -437,7 +443,7 @@ class Image extends AbstractElement
|
|||||||
$imageContent = $zip->getFromName($imageFilename);
|
$imageContent = $zip->getFromName($imageFilename);
|
||||||
if ($imageContent !== false) {
|
if ($imageContent !== false) {
|
||||||
file_put_contents($tempFilename, $imageContent);
|
file_put_contents($tempFilename, $imageContent);
|
||||||
$imageData = @getimagesize($tempFilename);
|
$imageData = getimagesize($tempFilename);
|
||||||
unlink($tempFilename);
|
unlink($tempFilename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -66,12 +66,12 @@ class XMLWriter
|
|||||||
$this->xmlWriter->openMemory();
|
$this->xmlWriter->openMemory();
|
||||||
} else {
|
} else {
|
||||||
// Create temporary filename
|
// Create temporary filename
|
||||||
$this->tempFile = @tempnam($tempFolder, 'xml');
|
$this->tempFile = tempnam($tempFolder, 'xml');
|
||||||
|
|
||||||
// Fallback to memory when temporary file cannot be used
|
// Fallback to memory when temporary file cannot be used
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
// Can't find any test case. Uncomment when found.
|
// Can't find any test case. Uncomment when found.
|
||||||
if ($this->xmlWriter->openUri($this->tempFile) === false) {
|
if (false === $this->tempFile || false === $this->xmlWriter->openUri($this->tempFile)) {
|
||||||
$this->xmlWriter->openMemory();
|
$this->xmlWriter->openMemory();
|
||||||
}
|
}
|
||||||
// @codeCoverageIgnoreEnd
|
// @codeCoverageIgnoreEnd
|
||||||
|
|||||||
@ -220,13 +220,10 @@ abstract class AbstractWriter implements WriterInterface
|
|||||||
// Temporary file
|
// Temporary file
|
||||||
$this->originalFilename = $filename;
|
$this->originalFilename = $filename;
|
||||||
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
|
if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
|
||||||
$filename = tempnam(Settings::getTempDir(), 'phpword_');
|
$filename = tempnam(Settings::getTempDir(), 'PhpWord');
|
||||||
// @codeCoverageIgnoreStart
|
if (false === $filename) {
|
||||||
// Can't find any test case. Uncomment when found.
|
|
||||||
if ($filename == '') {
|
|
||||||
$filename = $this->originalFilename;
|
$filename = $this->originalFilename;
|
||||||
}
|
}
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
}
|
}
|
||||||
$this->tempFilename = $filename;
|
$this->tempFilename = $filename;
|
||||||
|
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
namespace PhpOffice\PhpWord\Tests;
|
namespace PhpOffice\PhpWord\Tests;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||||
use PhpOffice\PhpWord\IOFactory;
|
use PhpOffice\PhpWord\IOFactory;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Settings;
|
||||||
@ -39,10 +40,15 @@ class TestHelperDOCX
|
|||||||
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
||||||
* @param string $writerName
|
* @param string $writerName
|
||||||
* @return \PhpOffice\PhpWord\Tests\XmlDocument
|
* @return \PhpOffice\PhpWord\Tests\XmlDocument
|
||||||
|
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
|
||||||
*/
|
*/
|
||||||
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
|
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
|
||||||
{
|
{
|
||||||
self::$file = tempnam(Settings::getTempDir(), 'PhpWord');
|
self::$file = tempnam(Settings::getTempDir(), 'PhpWord');
|
||||||
|
if (false === self::$file) {
|
||||||
|
throw new CreateTemporaryFileException();
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
|
if (!is_dir(Settings::getTempDir() . '/PhpWord_Unit_Test/')) {
|
||||||
mkdir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
mkdir(Settings::getTempDir() . '/PhpWord_Unit_Test/');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user