| 148 |
lars |
1 |
<?php declare(strict_types = 1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PharIo\Manifest.
|
|
|
4 |
*
|
|
|
5 |
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PharIo\Manifest;
|
|
|
11 |
|
|
|
12 |
use PharIo\Version\AnyVersionConstraint;
|
|
|
13 |
use PharIo\Version\Version;
|
|
|
14 |
use PharIo\Version\VersionConstraint;
|
|
|
15 |
use XMLWriter;
|
|
|
16 |
|
|
|
17 |
/** @psalm-suppress MissingConstructor */
|
|
|
18 |
class ManifestSerializer {
|
|
|
19 |
/** @var XMLWriter */
|
|
|
20 |
private $xmlWriter;
|
|
|
21 |
|
|
|
22 |
public function serializeToFile(Manifest $manifest, string $filename): void {
|
|
|
23 |
\file_put_contents(
|
|
|
24 |
$filename,
|
|
|
25 |
$this->serializeToString($manifest)
|
|
|
26 |
);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
public function serializeToString(Manifest $manifest): string {
|
|
|
30 |
$this->startDocument();
|
|
|
31 |
|
|
|
32 |
$this->addContains($manifest->getName(), $manifest->getVersion(), $manifest->getType());
|
|
|
33 |
$this->addCopyright($manifest->getCopyrightInformation());
|
|
|
34 |
$this->addRequirements($manifest->getRequirements());
|
|
|
35 |
$this->addBundles($manifest->getBundledComponents());
|
|
|
36 |
|
|
|
37 |
return $this->finishDocument();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private function startDocument(): void {
|
|
|
41 |
$xmlWriter = new XMLWriter();
|
|
|
42 |
$xmlWriter->openMemory();
|
|
|
43 |
$xmlWriter->setIndent(true);
|
|
|
44 |
$xmlWriter->setIndentString(\str_repeat(' ', 4));
|
|
|
45 |
$xmlWriter->startDocument('1.0', 'UTF-8');
|
|
|
46 |
$xmlWriter->startElement('phar');
|
|
|
47 |
$xmlWriter->writeAttribute('xmlns', 'https://phar.io/xml/manifest/1.0');
|
|
|
48 |
|
|
|
49 |
$this->xmlWriter = $xmlWriter;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function finishDocument(): string {
|
|
|
53 |
$this->xmlWriter->endElement();
|
|
|
54 |
$this->xmlWriter->endDocument();
|
|
|
55 |
|
|
|
56 |
return $this->xmlWriter->outputMemory();
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
private function addContains(ApplicationName $name, Version $version, Type $type): void {
|
|
|
60 |
$this->xmlWriter->startElement('contains');
|
|
|
61 |
$this->xmlWriter->writeAttribute('name', $name->asString());
|
|
|
62 |
$this->xmlWriter->writeAttribute('version', $version->getVersionString());
|
|
|
63 |
|
|
|
64 |
switch (true) {
|
|
|
65 |
case $type->isApplication(): {
|
|
|
66 |
$this->xmlWriter->writeAttribute('type', 'application');
|
|
|
67 |
|
|
|
68 |
break;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
case $type->isLibrary(): {
|
|
|
72 |
$this->xmlWriter->writeAttribute('type', 'library');
|
|
|
73 |
|
|
|
74 |
break;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
case $type->isExtension(): {
|
|
|
78 |
$this->xmlWriter->writeAttribute('type', 'extension');
|
|
|
79 |
/* @var $type Extension */
|
|
|
80 |
$this->addExtension(
|
|
|
81 |
$type->getApplicationName(),
|
|
|
82 |
$type->getVersionConstraint()
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
break;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
default: {
|
|
|
89 |
$this->xmlWriter->writeAttribute('type', 'custom');
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$this->xmlWriter->endElement();
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private function addCopyright(CopyrightInformation $copyrightInformation): void {
|
|
|
97 |
$this->xmlWriter->startElement('copyright');
|
|
|
98 |
|
|
|
99 |
foreach ($copyrightInformation->getAuthors() as $author) {
|
|
|
100 |
$this->xmlWriter->startElement('author');
|
|
|
101 |
$this->xmlWriter->writeAttribute('name', $author->getName());
|
|
|
102 |
$this->xmlWriter->writeAttribute('email', $author->getEmail()->asString());
|
|
|
103 |
$this->xmlWriter->endElement();
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$license = $copyrightInformation->getLicense();
|
|
|
107 |
|
|
|
108 |
$this->xmlWriter->startElement('license');
|
|
|
109 |
$this->xmlWriter->writeAttribute('type', $license->getName());
|
|
|
110 |
$this->xmlWriter->writeAttribute('url', $license->getUrl()->asString());
|
|
|
111 |
$this->xmlWriter->endElement();
|
|
|
112 |
|
|
|
113 |
$this->xmlWriter->endElement();
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
private function addRequirements(RequirementCollection $requirementCollection): void {
|
|
|
117 |
$phpRequirement = new AnyVersionConstraint();
|
|
|
118 |
$extensions = [];
|
|
|
119 |
|
|
|
120 |
foreach ($requirementCollection as $requirement) {
|
|
|
121 |
if ($requirement instanceof PhpVersionRequirement) {
|
|
|
122 |
$phpRequirement = $requirement->getVersionConstraint();
|
|
|
123 |
|
|
|
124 |
continue;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($requirement instanceof PhpExtensionRequirement) {
|
|
|
128 |
$extensions[] = $requirement->asString();
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
$this->xmlWriter->startElement('requires');
|
|
|
133 |
$this->xmlWriter->startElement('php');
|
|
|
134 |
$this->xmlWriter->writeAttribute('version', $phpRequirement->asString());
|
|
|
135 |
|
|
|
136 |
foreach ($extensions as $extension) {
|
|
|
137 |
$this->xmlWriter->startElement('ext');
|
|
|
138 |
$this->xmlWriter->writeAttribute('name', $extension);
|
|
|
139 |
$this->xmlWriter->endElement();
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$this->xmlWriter->endElement();
|
|
|
143 |
$this->xmlWriter->endElement();
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
private function addBundles(BundledComponentCollection $bundledComponentCollection): void {
|
|
|
147 |
if (\count($bundledComponentCollection) === 0) {
|
|
|
148 |
return;
|
|
|
149 |
}
|
|
|
150 |
$this->xmlWriter->startElement('bundles');
|
|
|
151 |
|
|
|
152 |
foreach ($bundledComponentCollection as $bundledComponent) {
|
|
|
153 |
$this->xmlWriter->startElement('component');
|
|
|
154 |
$this->xmlWriter->writeAttribute('name', $bundledComponent->getName());
|
|
|
155 |
$this->xmlWriter->writeAttribute('version', $bundledComponent->getVersion()->getVersionString());
|
|
|
156 |
$this->xmlWriter->endElement();
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$this->xmlWriter->endElement();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
private function addExtension(ApplicationName $applicationName, VersionConstraint $versionConstraint): void {
|
|
|
163 |
$this->xmlWriter->startElement('extension');
|
|
|
164 |
$this->xmlWriter->writeAttribute('for', $applicationName->asString());
|
|
|
165 |
$this->xmlWriter->writeAttribute('compatible', $versionConstraint->asString());
|
|
|
166 |
$this->xmlWriter->endElement();
|
|
|
167 |
}
|
|
|
168 |
}
|