ManifestDocument.php 2.9 KB

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