1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer; |
4
|
|
|
|
5
|
|
|
abstract class BaseWriter implements IWriter |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Write charts that are defined in the workbook? |
9
|
|
|
* Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object. |
10
|
|
|
* |
11
|
|
|
* @var bool |
12
|
|
|
*/ |
13
|
|
|
protected $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
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $preCalculateFormulas = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Use disk caching where possible? |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $useDiskCaching = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Disk caching directory. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $diskCachingDirectory = './'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var resource |
40
|
|
|
*/ |
41
|
|
|
protected $fileHandle; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $shouldCloseFile; |
47
|
|
|
|
48
|
|
|
public function getIncludeCharts() |
49
|
|
|
{ |
50
|
|
|
return $this->includeCharts; |
51
|
|
|
} |
52
|
|
|
|
53
|
69 |
|
public function setIncludeCharts($includeCharts) |
54
|
|
|
{ |
55
|
69 |
|
$this->includeCharts = (bool) $includeCharts; |
56
|
|
|
|
57
|
69 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
77 |
|
public function getPreCalculateFormulas() |
61
|
|
|
{ |
62
|
77 |
|
return $this->preCalculateFormulas; |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
public function setPreCalculateFormulas($precalculateFormulas) |
66
|
|
|
{ |
67
|
14 |
|
$this->preCalculateFormulas = (bool) $precalculateFormulas; |
68
|
|
|
|
69
|
14 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
319 |
|
public function getUseDiskCaching() |
73
|
|
|
{ |
74
|
319 |
|
return $this->useDiskCaching; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setUseDiskCaching($useDiskCache, $cacheDirectory = null) |
78
|
|
|
{ |
79
|
|
|
$this->useDiskCaching = $useDiskCache; |
80
|
|
|
|
81
|
|
|
if ($cacheDirectory !== null) { |
82
|
|
|
if (is_dir($cacheDirectory)) { |
83
|
|
|
$this->diskCachingDirectory = $cacheDirectory; |
84
|
|
|
} else { |
85
|
|
|
throw new Exception("Directory does not exist: $cacheDirectory"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getDiskCachingDirectory() |
93
|
|
|
{ |
94
|
|
|
return $this->diskCachingDirectory; |
95
|
|
|
} |
96
|
|
|
|
97
|
709 |
|
protected function processFlags(int $flags): void |
98
|
|
|
{ |
99
|
709 |
|
if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) { |
100
|
|
|
$this->setIncludeCharts(true); |
101
|
|
|
} |
102
|
709 |
|
if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) { |
103
|
|
|
$this->setPreCalculateFormulas(false); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Open file handle. |
109
|
|
|
* |
110
|
|
|
* @param resource|string $filename |
111
|
|
|
*/ |
112
|
721 |
|
public function openFileHandle($filename): void |
113
|
|
|
{ |
114
|
721 |
|
if (is_resource($filename)) { |
115
|
8 |
|
$this->fileHandle = $filename; |
116
|
8 |
|
$this->shouldCloseFile = false; |
117
|
|
|
|
118
|
8 |
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
713 |
|
$mode = 'wb'; |
122
|
713 |
|
$scheme = parse_url($filename, PHP_URL_SCHEME); |
123
|
713 |
|
if ($scheme === 's3') { |
124
|
|
|
// @codeCoverageIgnoreStart |
125
|
|
|
$mode = 'w'; |
126
|
|
|
// @codeCoverageIgnoreEnd |
127
|
|
|
} |
128
|
713 |
|
$fileHandle = $filename ? fopen($filename, $mode) : false; |
129
|
713 |
|
if ($fileHandle === false) { |
130
|
3 |
|
throw new Exception('Could not open file "' . $filename . '" for writing.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
710 |
|
$this->fileHandle = $fileHandle; |
134
|
710 |
|
$this->shouldCloseFile = true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Close file handle only if we opened it ourselves. |
139
|
|
|
*/ |
140
|
718 |
|
protected function maybeCloseFileHandle(): void |
141
|
|
|
{ |
142
|
718 |
|
if ($this->shouldCloseFile) { |
143
|
710 |
|
if (!fclose($this->fileHandle)) { |
144
|
|
|
throw new Exception('Could not close file after writing.'); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|