Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.00% |
43 / 50 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
ODPresentation | |
86.00% |
43 / 50 |
|
80.00% |
4 / 5 |
19.99 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
save | |
81.58% |
31 / 38 |
|
0.00% |
0 / 1 |
14.06 | |||
hasDiskCaching | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setUseDiskCaching | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getDiskCachingDirectory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPPresentation - A pure PHP library for reading and writing |
4 | * presentations documents. |
5 | * |
6 | * PHPPresentation is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPPresentation |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | declare(strict_types=1); |
19 | |
20 | namespace PhpOffice\PhpPresentation\Writer; |
21 | |
22 | use DirectoryIterator; |
23 | use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; |
24 | use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; |
25 | use PhpOffice\PhpPresentation\Exception\FileCopyException; |
26 | use PhpOffice\PhpPresentation\Exception\FileRemoveException; |
27 | use PhpOffice\PhpPresentation\Exception\InvalidParameterException; |
28 | use PhpOffice\PhpPresentation\HashTable; |
29 | use PhpOffice\PhpPresentation\PhpPresentation; |
30 | use ReflectionClass; |
31 | |
32 | /** |
33 | * ODPresentation writer. |
34 | */ |
35 | class ODPresentation extends AbstractWriter implements WriterInterface |
36 | { |
37 | /** |
38 | * @var \PhpOffice\PhpPresentation\Shape\Chart[] |
39 | */ |
40 | public $chartArray = []; |
41 | |
42 | /** |
43 | * Use disk caching where possible? |
44 | * |
45 | * @var bool |
46 | */ |
47 | private $useDiskCaching = false; |
48 | |
49 | /** |
50 | * Disk caching directory. |
51 | * |
52 | * @var string |
53 | */ |
54 | private $diskCachingDirectory; |
55 | |
56 | /** |
57 | * Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation. |
58 | */ |
59 | public function __construct(?PhpPresentation $pPhpPresentation = null) |
60 | { |
61 | // Assign PhpPresentation |
62 | $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation()); |
63 | |
64 | // Set up disk caching location |
65 | $this->diskCachingDirectory = './'; |
66 | |
67 | // Set HashTable variables |
68 | $this->oDrawingHashTable = new HashTable(); |
69 | |
70 | $this->setZipAdapter(new ZipArchiveAdapter()); |
71 | } |
72 | |
73 | /** |
74 | * Save PhpPresentation to file. |
75 | */ |
76 | public function save(string $pFilename): void |
77 | { |
78 | if (empty($pFilename)) { |
79 | throw new InvalidParameterException('pFilename', ''); |
80 | } |
81 | // If $pFilename is php://output or php://stdout, make it a temporary file... |
82 | $originalFilename = $pFilename; |
83 | if ('php://output' == strtolower($pFilename) || 'php://stdout' == strtolower($pFilename)) { |
84 | $pFilename = @tempnam($this->diskCachingDirectory, 'phppttmp'); |
85 | if ('' == $pFilename) { |
86 | $pFilename = $originalFilename; |
87 | } |
88 | } |
89 | |
90 | // Initialize HashTable |
91 | $this->getDrawingHashTable()->addFromSource($this->allDrawings()); |
92 | |
93 | // Initialize Zip |
94 | $oZip = $this->getZipAdapter(); |
95 | $oZip->open($pFilename); |
96 | |
97 | // Variables |
98 | $oPresentation = $this->getPhpPresentation(); |
99 | $arrayChart = []; |
100 | |
101 | $arrayFiles = []; |
102 | $oDir = new DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'ODPresentation'); |
103 | foreach ($oDir as $oFile) { |
104 | if (!$oFile->isFile()) { |
105 | continue; |
106 | } |
107 | |
108 | $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php'); |
109 | $class = new ReflectionClass($class); |
110 | |
111 | if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) { |
112 | continue; |
113 | } |
114 | $arrayFiles[$oFile->getBasename('.php')] = $class; |
115 | } |
116 | |
117 | ksort($arrayFiles); |
118 | |
119 | foreach ($arrayFiles as $o) { |
120 | $oService = $o->newInstance(); |
121 | $oService->setZip($oZip); |
122 | $oService->setPresentation($oPresentation); |
123 | $oService->setDrawingHashTable($this->getDrawingHashTable()); |
124 | $oService->setArrayChart($arrayChart); |
125 | $oZip = $oService->render(); |
126 | $arrayChart = $oService->getArrayChart(); |
127 | unset($oService); |
128 | } |
129 | |
130 | // Close file |
131 | $oZip->close(); |
132 | |
133 | // If a temporary file was used, copy it to the correct file stream |
134 | if ($originalFilename != $pFilename) { |
135 | if (false === copy($pFilename, $originalFilename)) { |
136 | throw new FileCopyException($pFilename, $originalFilename); |
137 | } |
138 | if (false === @unlink($pFilename)) { |
139 | throw new FileRemoveException($pFilename); |
140 | } |
141 | } |
142 | } |
143 | |
144 | /** |
145 | * Get use disk caching where possible? |
146 | * |
147 | * @return bool |
148 | */ |
149 | public function hasDiskCaching() |
150 | { |
151 | return $this->useDiskCaching; |
152 | } |
153 | |
154 | /** |
155 | * Set use disk caching where possible? |
156 | * |
157 | * @param string $directory Disk caching directory |
158 | * |
159 | * @return ODPresentation |
160 | */ |
161 | public function setUseDiskCaching(bool $pValue = false, ?string $directory = null) |
162 | { |
163 | $this->useDiskCaching = $pValue; |
164 | |
165 | if (null !== $directory) { |
166 | if (!is_dir($directory)) { |
167 | throw new DirectoryNotFoundException($directory); |
168 | } |
169 | $this->diskCachingDirectory = $directory; |
170 | } |
171 | |
172 | return $this; |
173 | } |
174 | |
175 | /** |
176 | * Get disk caching directory. |
177 | * |
178 | * @return string |
179 | */ |
180 | public function getDiskCachingDirectory() |
181 | { |
182 | return $this->diskCachingDirectory; |
183 | } |
184 | } |