annotate vendor/phar-io/manifest/src/xml/ManifestDocument.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@14 1 <?php
Chris@14 2 /*
Chris@14 3 * This file is part of PharIo\Manifest.
Chris@14 4 *
Chris@14 5 * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
Chris@14 6 *
Chris@14 7 * For the full copyright and license information, please view the LICENSE
Chris@14 8 * file that was distributed with this source code.
Chris@14 9 */
Chris@14 10
Chris@14 11 namespace PharIo\Manifest;
Chris@14 12
Chris@14 13 use DOMDocument;
Chris@14 14 use DOMElement;
Chris@14 15
Chris@14 16 class ManifestDocument {
Chris@14 17 const XMLNS = 'https://phar.io/xml/manifest/1.0';
Chris@14 18
Chris@14 19 /**
Chris@14 20 * @var DOMDocument
Chris@14 21 */
Chris@14 22 private $dom;
Chris@14 23
Chris@14 24 /**
Chris@14 25 * ManifestDocument constructor.
Chris@14 26 *
Chris@14 27 * @param DOMDocument $dom
Chris@14 28 */
Chris@14 29 private function __construct(DOMDocument $dom) {
Chris@14 30 $this->ensureCorrectDocumentType($dom);
Chris@14 31
Chris@14 32 $this->dom = $dom;
Chris@14 33 }
Chris@14 34
Chris@14 35 public static function fromFile($filename) {
Chris@14 36 if (!file_exists($filename)) {
Chris@14 37 throw new ManifestDocumentException(
Chris@14 38 sprintf('File "%s" not found', $filename)
Chris@14 39 );
Chris@14 40 }
Chris@14 41
Chris@14 42 return self::fromString(
Chris@14 43 file_get_contents($filename)
Chris@14 44 );
Chris@14 45 }
Chris@14 46
Chris@14 47 public static function fromString($xmlString) {
Chris@14 48 $prev = libxml_use_internal_errors(true);
Chris@14 49 libxml_clear_errors();
Chris@14 50
Chris@14 51 $dom = new DOMDocument();
Chris@14 52 $dom->loadXML($xmlString);
Chris@14 53
Chris@14 54 $errors = libxml_get_errors();
Chris@14 55 libxml_use_internal_errors($prev);
Chris@14 56
Chris@14 57 if (count($errors) !== 0) {
Chris@14 58 throw new ManifestDocumentLoadingException($errors);
Chris@14 59 }
Chris@14 60
Chris@14 61 return new self($dom);
Chris@14 62 }
Chris@14 63
Chris@14 64 public function getContainsElement() {
Chris@14 65 return new ContainsElement(
Chris@14 66 $this->fetchElementByName('contains')
Chris@14 67 );
Chris@14 68 }
Chris@14 69
Chris@14 70 public function getCopyrightElement() {
Chris@14 71 return new CopyrightElement(
Chris@14 72 $this->fetchElementByName('copyright')
Chris@14 73 );
Chris@14 74 }
Chris@14 75
Chris@14 76 public function getRequiresElement() {
Chris@14 77 return new RequiresElement(
Chris@14 78 $this->fetchElementByName('requires')
Chris@14 79 );
Chris@14 80 }
Chris@14 81
Chris@14 82 public function hasBundlesElement() {
Chris@14 83 return $this->dom->getElementsByTagNameNS(self::XMLNS, 'bundles')->length === 1;
Chris@14 84 }
Chris@14 85
Chris@14 86 public function getBundlesElement() {
Chris@14 87 return new BundlesElement(
Chris@14 88 $this->fetchElementByName('bundles')
Chris@14 89 );
Chris@14 90 }
Chris@14 91
Chris@14 92 private function ensureCorrectDocumentType(DOMDocument $dom) {
Chris@14 93 $root = $dom->documentElement;
Chris@14 94
Chris@14 95 if ($root->localName !== 'phar' || $root->namespaceURI !== self::XMLNS) {
Chris@14 96 throw new ManifestDocumentException('Not a phar.io manifest document');
Chris@14 97 }
Chris@14 98 }
Chris@14 99
Chris@14 100 /**
Chris@14 101 * @param $elementName
Chris@14 102 *
Chris@14 103 * @return DOMElement
Chris@14 104 *
Chris@14 105 * @throws ManifestDocumentException
Chris@14 106 */
Chris@14 107 private function fetchElementByName($elementName) {
Chris@14 108 $element = $this->dom->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
Chris@14 109
Chris@14 110 if (!$element instanceof DOMElement) {
Chris@14 111 throw new ManifestDocumentException(
Chris@14 112 sprintf('Element %s missing', $elementName)
Chris@14 113 );
Chris@14 114 }
Chris@14 115
Chris@14 116 return $element;
Chris@14 117 }
Chris@14 118 }