Skip to content

Commit

Permalink
Refactor XmlFile to support being created from content only
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Jun 12, 2020
1 parent 545ea00 commit ea8ea5d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
11 changes: 1 addition & 10 deletions src/services/resolver/PharIoAliasResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,10 @@ public function resolve(RequestedPhar $requestedPhar): SourceRepository {
new JsonData($file->getContent())
);
case 'phar.io':
$filename = new Filename(\tempnam(\sys_get_temp_dir(), 'repo_'));
$file->saveAs($filename);

$repo = new PharIoRepository(
new XmlFile(
$filename,
'https://phar.io/repository',
'repository'
)
XmlFile::fromFile($file)
);

$filename->delete();

return $repo;
}

Expand Down
43 changes: 36 additions & 7 deletions src/shared/XmlFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace PharIo\Phive;

use PharIo\FileSystem\Directory;
use PharIo\FileSystem\File;
use PharIo\FileSystem\Filename;

class XmlFile {
Expand All @@ -24,7 +25,20 @@ public function __construct(Filename $filename, string $namespace, string $root)
$this->filename = $filename;
$this->namespace = $namespace;
$this->rootElementName = $root;
$this->init();
}

public static function fromFile(File $file): self {
$dom = self::createDomDocument();
$dom->loadXML($file->getContent());

$xmlFile = new self(
$file->getFilename(),
$dom->documentElement->namespaceURI,
$dom->documentElement->localName
);
$xmlFile->dom = $dom;

return $xmlFile;
}

public function createElement(string $name, string $text = ''): \DOMElement {
Expand Down Expand Up @@ -53,29 +67,44 @@ public function getDirectory(): Directory {
}

public function getDom(): \DOMDocument {
$this->initDom();

return $this->dom;
}

private function getXPath(): \DOMXPath {
$this->initXPath();

return $this->xPath;
}

/** @psalm-assert \DomDocument $this->dom */
private function init(): void {
private function initDom(): void {
if ($this->dom instanceof \DOMDocument) {
return;
}

$this->dom = new \DOMDocument('1.0', 'UTF-8');
$this->dom->preserveWhiteSpace = false;
$this->dom->formatOutput = true;
$this->dom = self::createDomDocument();

if ($this->filename->exists()) {
$this->dom->load($this->filename->asString());
} else {
$this->dom->appendChild($this->dom->createElementNS($this->namespace, $this->rootElementName));
}
}

private function initXPath(): void {
$this->initDom();

$this->xPath = new \DOMXPath($this->dom);
$this->xPath->registerNamespace('phive', $this->namespace);
}

private function getXPath(): \DOMXPath {
return $this->xPath;
private static function createDomDocument(): \DOMDocument {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

return $dom;
}
}
5 changes: 1 addition & 4 deletions tests/unit/services/phar/PharIoAliasResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ public function testReturnsRepository(): void {
$sourcesListFileLoader = $this->createMock(RemoteSourcesListFileLoader::class);
$sourcesListFileLoader->expects($this->once())->method('load')->willReturn($sourcesList);

$filename = $this->createMock(Filename::class);
$filename->method('delete')->willReturn(true);

$file = $this->createMock(File::class);
$file->method('getFilename')->willReturn($filename);
$file->expects($this->once())->method('saveAs');
$file->method('getContent')->willReturn('<?xml version="1.0"?><root xmlns="a:b" />');

$fileDownloader = $this->createMock(FileDownloader::class);
$fileDownloader->method('download')->with($url)->willReturn($file);
Expand Down

0 comments on commit ea8ea5d

Please sign in to comment.