| 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\Exception as VersionException;
|
|
|
13 |
use PharIo\Version\Version;
|
|
|
14 |
use PharIo\Version\VersionConstraintParser;
|
|
|
15 |
|
|
|
16 |
class ManifestDocumentMapper {
|
|
|
17 |
public function map(ManifestDocument $document): Manifest {
|
|
|
18 |
try {
|
|
|
19 |
$contains = $document->getContainsElement();
|
|
|
20 |
$type = $this->mapType($contains);
|
|
|
21 |
$copyright = $this->mapCopyright($document->getCopyrightElement());
|
|
|
22 |
$requirements = $this->mapRequirements($document->getRequiresElement());
|
|
|
23 |
$bundledComponents = $this->mapBundledComponents($document);
|
|
|
24 |
|
|
|
25 |
return new Manifest(
|
|
|
26 |
new ApplicationName($contains->getName()),
|
|
|
27 |
new Version($contains->getVersion()),
|
|
|
28 |
$type,
|
|
|
29 |
$copyright,
|
|
|
30 |
$requirements,
|
|
|
31 |
$bundledComponents
|
|
|
32 |
);
|
|
|
33 |
} catch (VersionException $e) {
|
|
|
34 |
throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
|
|
|
35 |
} catch (Exception $e) {
|
|
|
36 |
throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
private function mapType(ContainsElement $contains): Type {
|
|
|
41 |
switch ($contains->getType()) {
|
|
|
42 |
case 'application':
|
|
|
43 |
return Type::application();
|
|
|
44 |
case 'library':
|
|
|
45 |
return Type::library();
|
|
|
46 |
case 'extension':
|
|
|
47 |
return $this->mapExtension($contains->getExtensionElement());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
throw new ManifestDocumentMapperException(
|
|
|
51 |
\sprintf('Unsupported type %s', $contains->getType())
|
|
|
52 |
);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private function mapCopyright(CopyrightElement $copyright): CopyrightInformation {
|
|
|
56 |
$authors = new AuthorCollection();
|
|
|
57 |
|
|
|
58 |
foreach ($copyright->getAuthorElements() as $authorElement) {
|
|
|
59 |
$authors->add(
|
|
|
60 |
new Author(
|
|
|
61 |
$authorElement->getName(),
|
|
|
62 |
new Email($authorElement->getEmail())
|
|
|
63 |
)
|
|
|
64 |
);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$licenseElement = $copyright->getLicenseElement();
|
|
|
68 |
$license = new License(
|
|
|
69 |
$licenseElement->getType(),
|
|
|
70 |
new Url($licenseElement->getUrl())
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
return new CopyrightInformation(
|
|
|
74 |
$authors,
|
|
|
75 |
$license
|
|
|
76 |
);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function mapRequirements(RequiresElement $requires): RequirementCollection {
|
|
|
80 |
$collection = new RequirementCollection();
|
|
|
81 |
$phpElement = $requires->getPHPElement();
|
|
|
82 |
$parser = new VersionConstraintParser;
|
|
|
83 |
|
|
|
84 |
try {
|
|
|
85 |
$versionConstraint = $parser->parse($phpElement->getVersion());
|
|
|
86 |
} catch (VersionException $e) {
|
|
|
87 |
throw new ManifestDocumentMapperException(
|
|
|
88 |
\sprintf('Unsupported version constraint - %s', $e->getMessage()),
|
|
|
89 |
(int)$e->getCode(),
|
|
|
90 |
$e
|
|
|
91 |
);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$collection->add(
|
|
|
95 |
new PhpVersionRequirement(
|
|
|
96 |
$versionConstraint
|
|
|
97 |
)
|
|
|
98 |
);
|
|
|
99 |
|
|
|
100 |
if (!$phpElement->hasExtElements()) {
|
|
|
101 |
return $collection;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
foreach ($phpElement->getExtElements() as $extElement) {
|
|
|
105 |
$collection->add(
|
|
|
106 |
new PhpExtensionRequirement($extElement->getName())
|
|
|
107 |
);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
return $collection;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private function mapBundledComponents(ManifestDocument $document): BundledComponentCollection {
|
|
|
114 |
$collection = new BundledComponentCollection();
|
|
|
115 |
|
|
|
116 |
if (!$document->hasBundlesElement()) {
|
|
|
117 |
return $collection;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
foreach ($document->getBundlesElement()->getComponentElements() as $componentElement) {
|
|
|
121 |
$collection->add(
|
|
|
122 |
new BundledComponent(
|
|
|
123 |
$componentElement->getName(),
|
|
|
124 |
new Version(
|
|
|
125 |
$componentElement->getVersion()
|
|
|
126 |
)
|
|
|
127 |
)
|
|
|
128 |
);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
return $collection;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
private function mapExtension(ExtensionElement $extension): Extension {
|
|
|
135 |
try {
|
|
|
136 |
$versionConstraint = (new VersionConstraintParser)->parse($extension->getCompatible());
|
|
|
137 |
|
|
|
138 |
return Type::extension(
|
|
|
139 |
new ApplicationName($extension->getFor()),
|
|
|
140 |
$versionConstraint
|
|
|
141 |
);
|
|
|
142 |
} catch (VersionException $e) {
|
|
|
143 |
throw new ManifestDocumentMapperException(
|
|
|
144 |
\sprintf('Unsupported version constraint - %s', $e->getMessage()),
|
|
|
145 |
(int)$e->getCode(),
|
|
|
146 |
$e
|
|
|
147 |
);
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|