Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ProjectLibre
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This file is part of PHPProject - A pure PHP library for reading and writing
5 * project management files.
6 *
7 * PHPProject is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPProject/contributors.
13 *
14 * @link        https://github.com/PHPOffice/PHPProject
15 * @copyright   2009-2014 PHPProject contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpProject\Writer;
22
23use PhpOffice\PhpProject\PhpProject;
24use PhpOffice\PhpProject\Reader\ProjectLibre as ProjectLibreReader;
25
26/**
27 * ProjectLibre writer
28 *
29 * A ProjectLibre `.pod` file is made of serialized Java data, a separator, then
30 * an MSPDI file. This writer outputs a minimal `.pod`: the Java stream signature,
31 * the separator, then an MSPDI file produced by the MSPDI writer. The file can be
32 * read back by the ProjectLibre reader.
33 */
34class ProjectLibre implements WriterInterface
35{
36    /**
37     * Java serialization stream signature placed at the beginning of a `.pod` file
38     */
39    const JAVA_PREAMBLE = "\xAC\xED\x00\x05";
40
41    /**
42     * PHPProject object
43     *
44     * @var PhpProject
45     */
46    protected $phpProject;
47
48    /**
49     * Create a new ProjectLibre writer
50     *
51     * @param PhpProject $phpProject
52     */
53    public function __construct(PhpProject $phpProject)
54    {
55        $this->phpProject = $phpProject;
56    }
57
58    /**
59     * @param string $pFilename
60     * @throws \Exception
61     */
62    public function save(string $pFilename): void
63    {
64        if (file_exists($pFilename) && !is_writable($pFilename)) {
65            throw new \Exception("Could not open file $pFilename for writing.");
66        }
67
68        // The MSPDI writer writes to a file, so we let it produce the MSPDI part
69        // in a temporary file before reading it back.
70        $tempFile = (string) tempnam(sys_get_temp_dir(), 'PHPPROJECT');
71        (new MSPDI($this->phpProject))->save($tempFile);
72        $mspdi = (string) file_get_contents($tempFile);
73        unlink($tempFile);
74
75        // Assemble the minimal .pod: preamble + separator + MSPDI.
76        $content = self::JAVA_PREAMBLE.ProjectLibreReader::MSPDI_SEPARATOR.$mspdi;
77
78        $fileHandle = fopen($pFilename, 'wb+');
79        fwrite($fileHandle, $content);
80        fclose($fileHandle);
81    }
82}