annotate vendor/phar-io/manifest/src/xml/ManifestDocument.php @ 2:5311817fb629

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