From 1c4f7ab3b838b23afb2ee4dab14acbf75956e952 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 22 Mar 2022 12:24:31 +0300 Subject: * add phpunit as a dev dependency * add some basic tests for UrlHelper::rewrite_relative() * fix UrlHelper::rewrite_relative() to work better on non-absolute relative URL paths --- vendor/phar-io/manifest/src/xml/AuthorElement.php | 20 ++++ .../manifest/src/xml/AuthorElementCollection.php | 18 ++++ vendor/phar-io/manifest/src/xml/BundlesElement.php | 18 ++++ .../phar-io/manifest/src/xml/ComponentElement.php | 20 ++++ .../src/xml/ComponentElementCollection.php | 18 ++++ .../phar-io/manifest/src/xml/ContainsElement.php | 30 ++++++ .../phar-io/manifest/src/xml/CopyrightElement.php | 24 +++++ .../phar-io/manifest/src/xml/ElementCollection.php | 61 ++++++++++++ vendor/phar-io/manifest/src/xml/ExtElement.php | 16 ++++ .../manifest/src/xml/ExtElementCollection.php | 18 ++++ .../phar-io/manifest/src/xml/ExtensionElement.php | 20 ++++ vendor/phar-io/manifest/src/xml/LicenseElement.php | 20 ++++ .../phar-io/manifest/src/xml/ManifestDocument.php | 103 +++++++++++++++++++++ .../phar-io/manifest/src/xml/ManifestElement.php | 66 +++++++++++++ vendor/phar-io/manifest/src/xml/PhpElement.php | 26 ++++++ .../phar-io/manifest/src/xml/RequiresElement.php | 18 ++++ 16 files changed, 496 insertions(+) create mode 100644 vendor/phar-io/manifest/src/xml/AuthorElement.php create mode 100644 vendor/phar-io/manifest/src/xml/AuthorElementCollection.php create mode 100644 vendor/phar-io/manifest/src/xml/BundlesElement.php create mode 100644 vendor/phar-io/manifest/src/xml/ComponentElement.php create mode 100644 vendor/phar-io/manifest/src/xml/ComponentElementCollection.php create mode 100644 vendor/phar-io/manifest/src/xml/ContainsElement.php create mode 100644 vendor/phar-io/manifest/src/xml/CopyrightElement.php create mode 100644 vendor/phar-io/manifest/src/xml/ElementCollection.php create mode 100644 vendor/phar-io/manifest/src/xml/ExtElement.php create mode 100644 vendor/phar-io/manifest/src/xml/ExtElementCollection.php create mode 100644 vendor/phar-io/manifest/src/xml/ExtensionElement.php create mode 100644 vendor/phar-io/manifest/src/xml/LicenseElement.php create mode 100644 vendor/phar-io/manifest/src/xml/ManifestDocument.php create mode 100644 vendor/phar-io/manifest/src/xml/ManifestElement.php create mode 100644 vendor/phar-io/manifest/src/xml/PhpElement.php create mode 100644 vendor/phar-io/manifest/src/xml/RequiresElement.php (limited to 'vendor/phar-io/manifest/src/xml') diff --git a/vendor/phar-io/manifest/src/xml/AuthorElement.php b/vendor/phar-io/manifest/src/xml/AuthorElement.php new file mode 100644 index 000000000..c454b271a --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/AuthorElement.php @@ -0,0 +1,20 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class AuthorElement extends ManifestElement { + public function getName(): string { + return $this->getAttributeValue('name'); + } + + public function getEmail(): string { + return $this->getAttributeValue('email'); + } +} diff --git a/vendor/phar-io/manifest/src/xml/AuthorElementCollection.php b/vendor/phar-io/manifest/src/xml/AuthorElementCollection.php new file mode 100644 index 000000000..a54147eb4 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/AuthorElementCollection.php @@ -0,0 +1,18 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class AuthorElementCollection extends ElementCollection { + public function current(): AuthorElement { + return new AuthorElement( + $this->getCurrentElement() + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/BundlesElement.php b/vendor/phar-io/manifest/src/xml/BundlesElement.php new file mode 100644 index 000000000..eb2105acd --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/BundlesElement.php @@ -0,0 +1,18 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class BundlesElement extends ManifestElement { + public function getComponentElements(): ComponentElementCollection { + return new ComponentElementCollection( + $this->getChildrenByName('component') + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ComponentElement.php b/vendor/phar-io/manifest/src/xml/ComponentElement.php new file mode 100644 index 000000000..7f6a5ec9a --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ComponentElement.php @@ -0,0 +1,20 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ComponentElement extends ManifestElement { + public function getName(): string { + return $this->getAttributeValue('name'); + } + + public function getVersion(): string { + return $this->getAttributeValue('version'); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ComponentElementCollection.php b/vendor/phar-io/manifest/src/xml/ComponentElementCollection.php new file mode 100644 index 000000000..23bcbd2f1 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ComponentElementCollection.php @@ -0,0 +1,18 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ComponentElementCollection extends ElementCollection { + public function current(): ComponentElement { + return new ComponentElement( + $this->getCurrentElement() + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ContainsElement.php b/vendor/phar-io/manifest/src/xml/ContainsElement.php new file mode 100644 index 000000000..ebef49d9b --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ContainsElement.php @@ -0,0 +1,30 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ContainsElement extends ManifestElement { + public function getName(): string { + return $this->getAttributeValue('name'); + } + + public function getVersion(): string { + return $this->getAttributeValue('version'); + } + + public function getType(): string { + return $this->getAttributeValue('type'); + } + + public function getExtensionElement(): ExtensionElement { + return new ExtensionElement( + $this->getChildByName('extension') + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/CopyrightElement.php b/vendor/phar-io/manifest/src/xml/CopyrightElement.php new file mode 100644 index 000000000..3debe7dcd --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/CopyrightElement.php @@ -0,0 +1,24 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class CopyrightElement extends ManifestElement { + public function getAuthorElements(): AuthorElementCollection { + return new AuthorElementCollection( + $this->getChildrenByName('author') + ); + } + + public function getLicenseElement(): LicenseElement { + return new LicenseElement( + $this->getChildByName('license') + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ElementCollection.php b/vendor/phar-io/manifest/src/xml/ElementCollection.php new file mode 100644 index 000000000..26d9250f3 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ElementCollection.php @@ -0,0 +1,61 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +use DOMElement; +use DOMNodeList; + +abstract class ElementCollection implements \Iterator { + /** @var DOMElement[] */ + private $nodes = []; + + /** @var int */ + private $position; + + public function __construct(DOMNodeList $nodeList) { + $this->position = 0; + $this->importNodes($nodeList); + } + + #[\ReturnTypeWillChange] + abstract public function current(); + + public function next(): void { + $this->position++; + } + + public function key(): int { + return $this->position; + } + + public function valid(): bool { + return $this->position < \count($this->nodes); + } + + public function rewind(): void { + $this->position = 0; + } + + protected function getCurrentElement(): DOMElement { + return $this->nodes[$this->position]; + } + + private function importNodes(DOMNodeList $nodeList): void { + foreach ($nodeList as $node) { + if (!$node instanceof DOMElement) { + throw new ElementCollectionException( + \sprintf('\DOMElement expected, got \%s', \get_class($node)) + ); + } + + $this->nodes[] = $node; + } + } +} diff --git a/vendor/phar-io/manifest/src/xml/ExtElement.php b/vendor/phar-io/manifest/src/xml/ExtElement.php new file mode 100644 index 000000000..257853c43 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ExtElement.php @@ -0,0 +1,16 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ExtElement extends ManifestElement { + public function getName(): string { + return $this->getAttributeValue('name'); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ExtElementCollection.php b/vendor/phar-io/manifest/src/xml/ExtElementCollection.php new file mode 100644 index 000000000..059773490 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ExtElementCollection.php @@ -0,0 +1,18 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ExtElementCollection extends ElementCollection { + public function current(): ExtElement { + return new ExtElement( + $this->getCurrentElement() + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ExtensionElement.php b/vendor/phar-io/manifest/src/xml/ExtensionElement.php new file mode 100644 index 000000000..db067f996 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ExtensionElement.php @@ -0,0 +1,20 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class ExtensionElement extends ManifestElement { + public function getFor(): string { + return $this->getAttributeValue('for'); + } + + public function getCompatible(): string { + return $this->getAttributeValue('compatible'); + } +} diff --git a/vendor/phar-io/manifest/src/xml/LicenseElement.php b/vendor/phar-io/manifest/src/xml/LicenseElement.php new file mode 100644 index 000000000..658c3d1c2 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/LicenseElement.php @@ -0,0 +1,20 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class LicenseElement extends ManifestElement { + public function getType(): string { + return $this->getAttributeValue('type'); + } + + public function getUrl(): string { + return $this->getAttributeValue('url'); + } +} diff --git a/vendor/phar-io/manifest/src/xml/ManifestDocument.php b/vendor/phar-io/manifest/src/xml/ManifestDocument.php new file mode 100644 index 000000000..f88b28293 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ManifestDocument.php @@ -0,0 +1,103 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +use DOMDocument; +use DOMElement; + +class ManifestDocument { + public const XMLNS = 'https://phar.io/xml/manifest/1.0'; + + /** @var DOMDocument */ + private $dom; + + public static function fromFile(string $filename): ManifestDocument { + if (!\file_exists($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(); + + $dom = new DOMDocument(); + $dom->loadXML($xmlString); + + $errors = \libxml_get_errors(); + \libxml_use_internal_errors($prev); + + 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; + } +} diff --git a/vendor/phar-io/manifest/src/xml/ManifestElement.php b/vendor/phar-io/manifest/src/xml/ManifestElement.php new file mode 100644 index 000000000..1f57f547d --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/ManifestElement.php @@ -0,0 +1,66 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +use DOMElement; +use DOMNodeList; + +class ManifestElement { + public const XMLNS = 'https://phar.io/xml/manifest/1.0'; + + /** @var DOMElement */ + private $element; + + public function __construct(DOMElement $element) { + $this->element = $element; + } + + protected function getAttributeValue(string $name): string { + if (!$this->element->hasAttribute($name)) { + throw new ManifestElementException( + \sprintf( + 'Attribute %s not set on element %s', + $name, + $this->element->localName + ) + ); + } + + return $this->element->getAttribute($name); + } + + protected function getChildByName(string $elementName): DOMElement { + $element = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0); + + if (!$element instanceof DOMElement) { + throw new ManifestElementException( + \sprintf('Element %s missing', $elementName) + ); + } + + return $element; + } + + protected function getChildrenByName(string $elementName): DOMNodeList { + $elementList = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName); + + if ($elementList->length === 0) { + throw new ManifestElementException( + \sprintf('Element(s) %s missing', $elementName) + ); + } + + return $elementList; + } + + protected function hasChild(string $elementName): bool { + return $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->length !== 0; + } +} diff --git a/vendor/phar-io/manifest/src/xml/PhpElement.php b/vendor/phar-io/manifest/src/xml/PhpElement.php new file mode 100644 index 000000000..c5c906c96 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/PhpElement.php @@ -0,0 +1,26 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class PhpElement extends ManifestElement { + public function getVersion(): string { + return $this->getAttributeValue('version'); + } + + public function hasExtElements(): bool { + return $this->hasChild('ext'); + } + + public function getExtElements(): ExtElementCollection { + return new ExtElementCollection( + $this->getChildrenByName('ext') + ); + } +} diff --git a/vendor/phar-io/manifest/src/xml/RequiresElement.php b/vendor/phar-io/manifest/src/xml/RequiresElement.php new file mode 100644 index 000000000..b7cd41ef7 --- /dev/null +++ b/vendor/phar-io/manifest/src/xml/RequiresElement.php @@ -0,0 +1,18 @@ +, Sebastian Heuer , Sebastian Bergmann + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace PharIo\Manifest; + +class RequiresElement extends ManifestElement { + public function getPHPElement(): PhpElement { + return new PhpElement( + $this->getChildByName('php') + ); + } +} -- cgit v1.2.3