1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
|
7
|
|
|
abstract class BaseWriter implements IWriter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Write charts that are defined in the workbook? |
11
|
|
|
* Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object. |
12
|
|
|
*/ |
13
|
|
|
protected bool $includeCharts = false; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Pre-calculate formulas |
17
|
|
|
* Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are |
18
|
|
|
* immediately available to MS Excel or other office spreadsheet viewer when opening the file. |
19
|
|
|
*/ |
20
|
|
|
protected bool $preCalculateFormulas = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Table formats |
24
|
|
|
* Enables table formats in writer, disabled here, must be enabled in writer via a setter. |
25
|
|
|
*/ |
26
|
|
|
protected bool $tableFormats = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Conditional Formatting |
30
|
|
|
* Enables conditional formatting in writer, disabled here, must be enabled in writer via a setter. |
31
|
|
|
*/ |
32
|
|
|
protected bool $conditionalFormatting = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Use disk caching where possible? |
36
|
|
|
*/ |
37
|
|
|
private bool $useDiskCaching = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Disk caching directory. |
41
|
|
|
*/ |
42
|
|
|
private string $diskCachingDirectory = './'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var resource |
46
|
|
|
*/ |
47
|
|
|
protected $fileHandle; |
48
|
|
|
|
49
|
|
|
private bool $shouldCloseFile; |
50
|
|
|
|
51
|
|
|
public function getIncludeCharts(): bool |
52
|
|
|
{ |
53
|
|
|
return $this->includeCharts; |
54
|
|
|
} |
55
|
|
|
|
56
|
187 |
|
public function setIncludeCharts(bool $includeCharts): self |
57
|
|
|
{ |
58
|
187 |
|
$this->includeCharts = $includeCharts; |
59
|
|
|
|
60
|
187 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
465 |
|
public function getPreCalculateFormulas(): bool |
64
|
|
|
{ |
65
|
465 |
|
return $this->preCalculateFormulas; |
66
|
|
|
} |
67
|
|
|
|
68
|
36 |
|
public function setPreCalculateFormulas(bool $precalculateFormulas): self |
69
|
|
|
{ |
70
|
36 |
|
$this->preCalculateFormulas = $precalculateFormulas; |
71
|
|
|
|
72
|
36 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getTableFormats(): bool |
76
|
|
|
{ |
77
|
|
|
return $this->tableFormats; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setTableFormats(bool $tableFormats): self |
81
|
|
|
{ |
82
|
|
|
if ($tableFormats) { |
83
|
|
|
throw new PhpSpreadsheetException('Table formatting not implemented for this writer'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getConditionalFormatting(): bool |
90
|
|
|
{ |
91
|
|
|
return $this->conditionalFormatting; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setConditionalFormatting(bool $conditionalFormatting): self |
95
|
|
|
{ |
96
|
|
|
if ($conditionalFormatting) { |
97
|
|
|
throw new PhpSpreadsheetException('Conditional Formatting not implemented for this writer'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
480 |
|
public function getUseDiskCaching(): bool |
104
|
|
|
{ |
105
|
480 |
|
return $this->useDiskCaching; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setUseDiskCaching(bool $useDiskCache, ?string $cacheDirectory = null): self |
109
|
|
|
{ |
110
|
|
|
$this->useDiskCaching = $useDiskCache; |
111
|
|
|
|
112
|
|
|
if ($cacheDirectory !== null) { |
113
|
|
|
if (is_dir($cacheDirectory)) { |
114
|
|
|
$this->diskCachingDirectory = $cacheDirectory; |
115
|
|
|
} else { |
116
|
|
|
throw new Exception("Directory does not exist: $cacheDirectory"); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getDiskCachingDirectory(): string |
124
|
|
|
{ |
125
|
|
|
return $this->diskCachingDirectory; |
126
|
|
|
} |
127
|
|
|
|
128
|
964 |
|
protected function processFlags(int $flags): void |
129
|
|
|
{ |
130
|
964 |
|
if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) { |
131
|
|
|
$this->setIncludeCharts(true); |
132
|
|
|
} |
133
|
964 |
|
if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) { |
134
|
|
|
$this->setPreCalculateFormulas(false); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Open file handle. |
140
|
|
|
* |
141
|
|
|
* @param resource|string $filename |
142
|
|
|
*/ |
143
|
978 |
|
public function openFileHandle($filename): void |
144
|
|
|
{ |
145
|
978 |
|
if (!is_string($filename)) { |
146
|
9 |
|
$this->fileHandle = $filename; |
147
|
9 |
|
$this->shouldCloseFile = false; |
148
|
|
|
|
149
|
9 |
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
969 |
|
$mode = 'wb'; |
153
|
969 |
|
$scheme = parse_url($filename, PHP_URL_SCHEME); |
154
|
969 |
|
if ($scheme === 's3') { |
155
|
|
|
// @codeCoverageIgnoreStart |
156
|
|
|
$mode = 'w'; |
157
|
|
|
// @codeCoverageIgnoreEnd |
158
|
|
|
} |
159
|
969 |
|
$fileHandle = $filename ? fopen($filename, $mode) : false; |
160
|
969 |
|
if ($fileHandle === false) { |
161
|
3 |
|
throw new Exception('Could not open file "' . $filename . '" for writing.'); |
162
|
|
|
} |
163
|
|
|
|
164
|
966 |
|
$this->fileHandle = $fileHandle; |
165
|
966 |
|
$this->shouldCloseFile = true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Close file handle only if we opened it ourselves. |
170
|
|
|
*/ |
171
|
975 |
|
protected function maybeCloseFileHandle(): void |
172
|
|
|
{ |
173
|
975 |
|
if ($this->shouldCloseFile) { |
174
|
966 |
|
if (!fclose($this->fileHandle)) { |
175
|
|
|
throw new Exception('Could not close file after writing.'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|