Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.11% covered (warning)
85.11%
40 / 47
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PowerPoint2007
85.11% covered (warning)
85.11%
40 / 47
80.00% covered (warning)
80.00%
4 / 5
20.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 save
80.00% covered (warning)
80.00%
28 / 35
0.00% covered (danger)
0.00%
0 / 1
14.35
 hasDiskCaching
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUseDiskCaching
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getDiskCachingDirectory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Writer;
21
22use DirectoryIterator;
23use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
24use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException;
25use PhpOffice\PhpPresentation\Exception\FileCopyException;
26use PhpOffice\PhpPresentation\Exception\FileRemoveException;
27use PhpOffice\PhpPresentation\Exception\InvalidParameterException;
28use PhpOffice\PhpPresentation\HashTable;
29use PhpOffice\PhpPresentation\PhpPresentation;
30use PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter;
31use ReflectionClass;
32
33/**
34 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007.
35 */
36class PowerPoint2007 extends AbstractWriter implements WriterInterface
37{
38    /**
39     * Use disk caching where possible?
40     *
41     * @var bool
42     */
43    protected $useDiskCaching = false;
44
45    /**
46     * Disk caching directory.
47     *
48     * @var string
49     */
50    protected $diskCachingDir;
51
52    /**
53     * Create a new PowerPoint2007 file.
54     */
55    public function __construct(?PhpPresentation $pPhpPresentation = null)
56    {
57        // Assign PhpPresentation
58        $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation());
59
60        // Set up disk caching location
61        $this->diskCachingDir = './';
62
63        // Set HashTable variables
64        $this->oDrawingHashTable = new HashTable();
65
66        $this->setZipAdapter(new ZipArchiveAdapter());
67    }
68
69    /**
70     * Save PhpPresentation to file.
71     */
72    public function save(string $pFilename): void
73    {
74        if (empty($pFilename)) {
75            throw new InvalidParameterException('pFilename', '');
76        }
77        $oPresentation = $this->getPhpPresentation();
78
79        // If $pFilename is php://output or php://stdout, make it a temporary file...
80        $originalFilename = $pFilename;
81        if ('php://output' == strtolower($pFilename) || 'php://stdout' == strtolower($pFilename)) {
82            $pFilename = @tempnam($this->diskCachingDir, 'phppttmp');
83            if ('' == $pFilename) {
84                $pFilename = $originalFilename;
85            }
86        }
87
88        // Create drawing dictionary
89        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
90
91        $oZip = $this->getZipAdapter();
92        $oZip->open($pFilename);
93
94        $oDir = new DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'PowerPoint2007');
95        $arrayFiles = [];
96        foreach ($oDir as $oFile) {
97            if (!$oFile->isFile()) {
98                continue;
99            }
100
101            $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php');
102            $class = new ReflectionClass($class);
103
104            if ($class->isAbstract() || !$class->isSubclassOf(AbstractDecoratorWriter::class)) {
105                continue;
106            }
107            $arrayFiles[$oFile->getBasename('.php')] = $class;
108        }
109
110        ksort($arrayFiles);
111
112        foreach ($arrayFiles as $o) {
113            $oService = $o->newInstance();
114            $oService->setZip($oZip);
115            $oService->setPresentation($oPresentation);
116            $oService->setDrawingHashTable($this->getDrawingHashTable());
117            $oZip = $oService->render();
118            unset($oService);
119        }
120
121        // Close file
122        $oZip->close();
123
124        // If a temporary file was used, copy it to the correct file stream
125        if ($originalFilename != $pFilename) {
126            if (false === copy($pFilename, $originalFilename)) {
127                throw new FileCopyException($pFilename, $originalFilename);
128            }
129            if (false === @unlink($pFilename)) {
130                throw new FileRemoveException($pFilename);
131            }
132        }
133    }
134
135    /**
136     * Get use disk caching where possible?
137     *
138     * @return bool
139     */
140    public function hasDiskCaching()
141    {
142        return $this->useDiskCaching;
143    }
144
145    /**
146     * Set use disk caching where possible?
147     *
148     * @param string $directory Disk caching directory
149     *
150     * @return PowerPoint2007
151     */
152    public function setUseDiskCaching(bool $useDiskCaching = false, ?string $directory = null)
153    {
154        $this->useDiskCaching = $useDiskCaching;
155
156        if (null !== $directory) {
157            if (!is_dir($directory)) {
158                throw new DirectoryNotFoundException($directory);
159            }
160            $this->diskCachingDir = $directory;
161        }
162
163        return $this;
164    }
165
166    /**
167     * Get disk caching directory.
168     *
169     * @return string
170     */
171    public function getDiskCachingDirectory()
172    {
173        return $this->diskCachingDir;
174    }
175}