Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ManifestDocument.php

class ManifestDocument

Hierarchy

  • class \PharIo\Manifest\ManifestDocument

Expanded class hierarchy of ManifestDocument

File

vendor/phar-io/manifest/src/xml/ManifestDocument.php, line 24

Namespace

PharIo\Manifest
View source
class ManifestDocument {
    public const XMLNS = 'https://phar.io/xml/manifest/1.0';
    
    /** @var DOMDocument */
    private $dom;
    public static function fromFile(string $filename) : ManifestDocument {
        if (!is_file($filename)) {
            throw new ManifestDocumentException(sprintf('File "%s" not found', $filename));
        }
        return self::fromString(file_get_contents($filename));
    }
    public static function fromString(string $xmlString) : ManifestDocument {
        $prev = libxml_use_internal_errors(true);
        libxml_clear_errors();
        try {
            $dom = new DOMDocument();
            $dom->loadXML($xmlString);
            $errors = libxml_get_errors();
            libxml_use_internal_errors($prev);
        } catch (Throwable $t) {
            throw new ManifestDocumentException($t->getMessage(), 0, $t);
        }
        if (count($errors) !== 0) {
            throw new ManifestDocumentLoadingException($errors);
        }
        return new self($dom);
    }
    private function __construct(DOMDocument $dom) {
        $this->ensureCorrectDocumentType($dom);
        $this->dom = $dom;
    }
    public function getContainsElement() : ContainsElement {
        return new ContainsElement($this->fetchElementByName('contains'));
    }
    public function getCopyrightElement() : CopyrightElement {
        return new CopyrightElement($this->fetchElementByName('copyright'));
    }
    public function getRequiresElement() : RequiresElement {
        return new RequiresElement($this->fetchElementByName('requires'));
    }
    public function hasBundlesElement() : bool {
        return $this->dom
            ->getElementsByTagNameNS(self::XMLNS, 'bundles')->length === 1;
    }
    public function getBundlesElement() : BundlesElement {
        return new BundlesElement($this->fetchElementByName('bundles'));
    }
    private function ensureCorrectDocumentType(DOMDocument $dom) : void {
        $root = $dom->documentElement;
        if ($root->localName !== 'phar' || $root->namespaceURI !== self::XMLNS) {
            throw new ManifestDocumentException('Not a phar.io manifest document');
        }
    }
    private function fetchElementByName(string $elementName) : DOMElement {
        $element = $this->dom
            ->getElementsByTagNameNS(self::XMLNS, $elementName)
            ->item(0);
        if (!$element instanceof DOMElement) {
            throw new ManifestDocumentException(sprintf('Element %s missing', $elementName));
        }
        return $element;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ManifestDocument::$dom private property @var DOMDocument
ManifestDocument::ensureCorrectDocumentType private function
ManifestDocument::fetchElementByName private function
ManifestDocument::fromFile public static function
ManifestDocument::fromString public static function
ManifestDocument::getBundlesElement public function
ManifestDocument::getContainsElement public function
ManifestDocument::getCopyrightElement public function
ManifestDocument::getRequiresElement public function
ManifestDocument::hasBundlesElement public function
ManifestDocument::XMLNS public constant
ManifestDocument::__construct private function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal