Completed
Push — develop ( 312b7a...53bbae )
by Franck
16s
created

PowerPoint2007::getLayoutPack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Writer;
19
20
use DirectoryIterator;
21
use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter;
22
use PhpOffice\PhpPresentation\HashTable;
23
use PhpOffice\PhpPresentation\PhpPresentation;
24
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack;
25
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
26
27
/**
28
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007
29
 */
30
class PowerPoint2007 extends AbstractWriter implements WriterInterface
31
{
32
    /**
33
     * Use disk caching where possible?
34
     *
35
     * @var boolean
36
     */
37
    protected $useDiskCaching = false;
38
39
    /**
40
     * Disk caching directory
41
     *
42
     * @var string
43
     */
44
    protected $diskCachingDir;
45
46
    /**
47
     * Layout pack to use
48
     * @deprecated 0.7
49
     * @var \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
50
     */
51
    protected $layoutPack;
52
53
    /**
54
     * Create a new PowerPoint2007 file
55
     *
56
     * @param PhpPresentation $pPhpPresentation
57
     */
58 120
    public function __construct(PhpPresentation $pPhpPresentation = null)
59
    {
60
        // Assign PhpPresentation
61 120
        $this->setPhpPresentation($pPhpPresentation);
62
63
        // Set up disk caching location
64 120
        $this->diskCachingDir = './';
65
66
        // Set layout pack
67 120
        $this->layoutPack = new PackDefault();
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The class PhpOffice\PhpPresentatio...\LayoutPack\PackDefault has been deprecated with message: 0.7

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
68
69
        // Set HashTable variables
70 120
        $this->oDrawingHashTable = new HashTable();
71
72 120
        $this->setZipAdapter(new ZipArchiveAdapter());
73 120
    }
74
75
    /**
76
     * Save PhpPresentation to file
77
     *
78
     * @param  string    $pFilename
79
     * @throws \Exception
80
     */
81 114
    public function save($pFilename)
82
    {
83 114
        if (empty($pFilename)) {
84 1
            throw new \Exception("Filename is empty");
85
        }
86 113
        $oPresentation = $this->getPhpPresentation();
87
88
        // If $pFilename is php://output or php://stdout, make it a temporary file...
89 112
        $originalFilename = $pFilename;
90 112
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
91
            $pFilename = @tempnam('./', 'phppttmp');
92
            if ($pFilename == '') {
93
                $pFilename = $originalFilename;
94
            }
95
        }
96
97
        // Create drawing dictionary
98 112
        $this->getDrawingHashTable()->addFromSource($this->allDrawings());
99
100 112
        $oZip = $this->getZipAdapter();
101 112
        $oZip->open($pFilename);
102
103 112
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
104 112
        $arrayFiles = array();
105 112
        foreach ($oDir as $oFile) {
106 112
            if (!$oFile->isFile()) {
107 112
                continue;
108
            }
109
110 112
            $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php');
111 112
            $o = new \ReflectionClass($class);
112
113 112
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
114 112
                continue;
115
            }
116 112
            $arrayFiles[$oFile->getBasename('.php')] = $o;
117 112
        }
118
119 112
        ksort($arrayFiles);
120
121 112
        foreach ($arrayFiles as $o) {
122 112
            $oService = $o->newInstance();
123 112
            $oService->setZip($oZip);
124 112
            $oService->setPresentation($oPresentation);
125 112
            $oService->setDrawingHashTable($this->getDrawingHashTable());
126 112
            $oZip = $oService->render();
127 112
            unset($oService);
128 112
        }
129
130
        // Close file
131 110
        $oZip->close();
132
133
        // If a temporary file was used, copy it to the correct file stream
134 110
        if ($originalFilename != $pFilename) {
135
            if (copy($pFilename, $originalFilename) === false) {
136
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
137
            }
138
            if (@unlink($pFilename) === false) {
139
                throw new \Exception('The file '.$pFilename.' could not be removed.');
140
            }
141
        }
142 110
    }
143
144
    /**
145
     * Get use disk caching where possible?
146
     *
147
     * @return boolean
148
     */
149 1
    public function hasDiskCaching()
150
    {
151 1
        return $this->useDiskCaching;
152
    }
153
154
    /**
155
     * Set use disk caching where possible?
156
     *
157
     * @param  boolean $pValue
158
     * @param  string $pDirectory Disk caching directory
159
     * @throws \Exception
160
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
161
     */
162 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
163
    {
164 2
        $this->useDiskCaching = $pValue;
165
166 2
        if (!is_null($pDirectory)) {
167 2
            if (!is_dir($pDirectory)) {
168 1
                throw new \Exception("Directory does not exist: $pDirectory");
169
            }
170 1
            $this->diskCachingDir = $pDirectory;
171 1
        }
172
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get disk caching directory
178
     *
179
     * @return string
180
     */
181 2
    public function getDiskCachingDirectory()
182
    {
183 2
        return $this->diskCachingDir;
184
    }
185
186
    /**
187
     * Get layout pack to use
188
     *
189
     * @deprecated 0.7
190
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
191
     */
192 2
    public function getLayoutPack()
193
    {
194 2
        return $this->layoutPack;
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
195
    }
196
197
    /**
198
     * Set layout pack to use
199
     *
200
     * @deprecated 0.7
201
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
202
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
203
     */
204 1
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
205
    {
206 1
        $this->layoutPack = $pValue;
0 ignored issues
show
Deprecated Code introduced by
The property PhpOffice\PhpPresentatio...rPoint2007::$layoutPack has been deprecated with message: 0.7

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
207
208 1
        return $this;
209
    }
210
}
211