| 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 DOMDocument;
|
|
|
13 |
use DOMElement;
|
|
|
14 |
|
|
|
15 |
class ManifestDocument {
|
|
|
16 |
public const XMLNS = 'https://phar.io/xml/manifest/1.0';
|
|
|
17 |
|
|
|
18 |
/** @var DOMDocument */
|
|
|
19 |
private $dom;
|
|
|
20 |
|
|
|
21 |
public static function fromFile(string $filename): ManifestDocument {
|
|
|
22 |
if (!\file_exists($filename)) {
|
|
|
23 |
throw new ManifestDocumentException(
|
|
|
24 |
\sprintf('File "%s" not found', $filename)
|
|
|
25 |
);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
return self::fromString(
|
|
|
29 |
\file_get_contents($filename)
|
|
|
30 |
);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
public static function fromString(string $xmlString): ManifestDocument {
|
|
|
34 |
$prev = \libxml_use_internal_errors(true);
|
|
|
35 |
\libxml_clear_errors();
|
|
|
36 |
|
|
|
37 |
$dom = new DOMDocument();
|
|
|
38 |
$dom->loadXML($xmlString);
|
|
|
39 |
|
|
|
40 |
$errors = \libxml_get_errors();
|
|
|
41 |
\libxml_use_internal_errors($prev);
|
|
|
42 |
|
|
|
43 |
if (\count($errors) !== 0) {
|
|
|
44 |
throw new ManifestDocumentLoadingException($errors);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
return new self($dom);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
private function __construct(DOMDocument $dom) {
|
|
|
51 |
$this->ensureCorrectDocumentType($dom);
|
|
|
52 |
|
|
|
53 |
$this->dom = $dom;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function getContainsElement(): ContainsElement {
|
|
|
57 |
return new ContainsElement(
|
|
|
58 |
$this->fetchElementByName('contains')
|
|
|
59 |
);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function getCopyrightElement(): CopyrightElement {
|
|
|
63 |
return new CopyrightElement(
|
|
|
64 |
$this->fetchElementByName('copyright')
|
|
|
65 |
);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function getRequiresElement(): RequiresElement {
|
|
|
69 |
return new RequiresElement(
|
|
|
70 |
$this->fetchElementByName('requires')
|
|
|
71 |
);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function hasBundlesElement(): bool {
|
|
|
75 |
return $this->dom->getElementsByTagNameNS(self::XMLNS, 'bundles')->length === 1;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
public function getBundlesElement(): BundlesElement {
|
|
|
79 |
return new BundlesElement(
|
|
|
80 |
$this->fetchElementByName('bundles')
|
|
|
81 |
);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
private function ensureCorrectDocumentType(DOMDocument $dom): void {
|
|
|
85 |
$root = $dom->documentElement;
|
|
|
86 |
|
|
|
87 |
if ($root->localName !== 'phar' || $root->namespaceURI !== self::XMLNS) {
|
|
|
88 |
throw new ManifestDocumentException('Not a phar.io manifest document');
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function fetchElementByName(string $elementName): DOMElement {
|
|
|
93 |
$element = $this->dom->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
|
|
|
94 |
|
|
|
95 |
if (!$element instanceof DOMElement) {
|
|
|
96 |
throw new ManifestDocumentException(
|
|
|
97 |
\sprintf('Element %s missing', $elementName)
|
|
|
98 |
);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $element;
|
|
|
102 |
}
|
|
|
103 |
}
|