chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
 
namespace PhpOffice\PhpSpreadsheet\Shared;
 
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
 
class XMLWriter extends \XMLWriter
{
    public static bool $debugEnabled = false;
 
    /** Temporary storage method */
    const STORAGE_MEMORY = 1;
    const STORAGE_DISK = 2;
 
    /**
     * Temporary filename.
     */
    private string $tempFileName = '';
 
    /**
     * Create a new XMLWriter instance.
     *
     * @param int $temporaryStorage Temporary storage location
     * @param ?string $temporaryStorageFolder Temporary storage folder
     */
    public function __construct(int $temporaryStorage = self::STORAGE_MEMORY, ?string $temporaryStorageFolder = null)
    {
        // Open temporary storage
        if ($temporaryStorage == self::STORAGE_MEMORY) {
            $this->openMemory();
        } else {
            // Create temporary filename
            if ($temporaryStorageFolder === null) {
                $temporaryStorageFolder = File::sysGetTempDir();
            }
            $this->tempFileName = (string) @tempnam($temporaryStorageFolder, 'xml');
 
            // Open storage
            if (empty($this->tempFileName) || $this->openUri($this->tempFileName) === false) {
                // Fallback to memory...
                $this->openMemory();
            }
        }
 
        // Set default values
        if (self::$debugEnabled) {
            $this->setIndent(true);
        }
    }
 
    /**
     * Destructor.
     */
    public function __destruct()
    {
        // Unlink temporary files
        // There is nothing reasonable to do if unlink fails.
        if ($this->tempFileName != '') {
            @unlink($this->tempFileName);
        }
    }
 
    public function __wakeup(): void
    {
        $this->tempFileName = '';
 
        throw new SpreadsheetException('Unserialize not permitted');
    }
 
    /**
     * Get written data.
     */
    public function getData(): string
    {
        if ($this->tempFileName == '') {
            return $this->outputMemory(true);
        }
        $this->flush();
 
        return file_get_contents($this->tempFileName) ?: '';
    }
 
    /**
     * Wrapper method for writeRaw.
     *
     * @param null|string|string[] $rawTextData
     */
    public function writeRawData($rawTextData): bool
    {
        if (is_array($rawTextData)) {
            $rawTextData = implode("\n", $rawTextData);
        }
 
        return $this->writeRaw(htmlspecialchars($rawTextData ?? ''));
    }
}