Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    protected bool $includeCharts = false;
12
 
13
    /**
14
     * Pre-calculate formulas
15
     * Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
16
     * immediately available to MS Excel or other office spreadsheet viewer when opening the file.
17
     */
18
    protected bool $preCalculateFormulas = true;
19
 
20
    /**
21
     * Use disk caching where possible?
22
     */
23
    private bool $useDiskCaching = false;
24
 
25
    /**
26
     * Disk caching directory.
27
     */
28
    private string $diskCachingDirectory = './';
29
 
30
    /**
31
     * @var resource
32
     */
33
    protected $fileHandle;
34
 
35
    private bool $shouldCloseFile;
36
 
37
    public function getIncludeCharts(): bool
38
    {
39
        return $this->includeCharts;
40
    }
41
 
42
    public function setIncludeCharts(bool $includeCharts): self
43
    {
44
        $this->includeCharts = $includeCharts;
45
 
46
        return $this;
47
    }
48
 
49
    public function getPreCalculateFormulas(): bool
50
    {
51
        return $this->preCalculateFormulas;
52
    }
53
 
54
    public function setPreCalculateFormulas(bool $precalculateFormulas): self
55
    {
56
        $this->preCalculateFormulas = $precalculateFormulas;
57
 
58
        return $this;
59
    }
60
 
61
    public function getUseDiskCaching(): bool
62
    {
63
        return $this->useDiskCaching;
64
    }
65
 
66
    public function setUseDiskCaching(bool $useDiskCache, ?string $cacheDirectory = null): self
67
    {
68
        $this->useDiskCaching = $useDiskCache;
69
 
70
        if ($cacheDirectory !== null) {
71
            if (is_dir($cacheDirectory)) {
72
                $this->diskCachingDirectory = $cacheDirectory;
73
            } else {
74
                throw new Exception("Directory does not exist: $cacheDirectory");
75
            }
76
        }
77
 
78
        return $this;
79
    }
80
 
81
    public function getDiskCachingDirectory(): string
82
    {
83
        return $this->diskCachingDirectory;
84
    }
85
 
86
    protected function processFlags(int $flags): void
87
    {
88
        if (((bool) ($flags & self::SAVE_WITH_CHARTS)) === true) {
89
            $this->setIncludeCharts(true);
90
        }
91
        if (((bool) ($flags & self::DISABLE_PRECALCULATE_FORMULAE)) === true) {
92
            $this->setPreCalculateFormulas(false);
93
        }
94
    }
95
 
96
    /**
97
     * Open file handle.
98
     *
99
     * @param resource|string $filename
100
     */
101
    public function openFileHandle($filename): void
102
    {
103
        if (!is_string($filename)) {
104
            $this->fileHandle = $filename;
105
            $this->shouldCloseFile = false;
106
 
107
            return;
108
        }
109
 
110
        $mode = 'wb';
111
        $scheme = parse_url($filename, PHP_URL_SCHEME);
112
        if ($scheme === 's3') {
113
            // @codeCoverageIgnoreStart
114
            $mode = 'w';
115
            // @codeCoverageIgnoreEnd
116
        }
117
        $fileHandle = $filename ? fopen($filename, $mode) : false;
118
        if ($fileHandle === false) {
119
            throw new Exception('Could not open file "' . $filename . '" for writing.');
120
        }
121
 
122
        $this->fileHandle = $fileHandle;
123
        $this->shouldCloseFile = true;
124
    }
125
 
126
    /**
127
     * Close file handle only if we opened it ourselves.
128
     */
129
    protected function maybeCloseFileHandle(): void
130
    {
131
        if ($this->shouldCloseFile) {
132
            if (!fclose($this->fileHandle)) {
133
                throw new Exception('Could not close file after writing.');
134
            }
135
        }
136
    }
137
}