Skip to content

Commit

Permalink
Introduce breaking reader/writer PHP 8.4 changes and new stream funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
veewee committed Sep 18, 2024
1 parent cd83c01 commit 2c1dec3
Show file tree
Hide file tree
Showing 15 changed files with 691 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/Xml/Reader/Loader/xml_file_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
/**
* @return Closure(): XMLReader
*/
function xml_file_loader(string $file): Closure
function xml_file_loader(string $file, ?string $encoding = null, int $flags = 0): Closure
{
return static fn (): XMLReader => disallow_issues(
static function () use ($file): XMLReader {
static function () use ($file, $encoding, $flags): XMLReader {
Assert::fileExists($file);
return disallow_libxml_false_returns(
XMLReader::open($file),
XMLReader::fromUri($file, $encoding, $flags),
'Could not open the provided XML file!'
);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Xml/Reader/Loader/xml_stream_loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace VeeWee\Xml\Reader\Loader;

use Closure;
use Webmozart\Assert\Assert;
use XMLReader;
use function VeeWee\Xml\ErrorHandling\disallow_issues;
use function VeeWee\Xml\ErrorHandling\disallow_libxml_false_returns;

/**
* @param resource $stream
* @return Closure(): XMLReader
*/
function xml_stream_loader(mixed $stream, ?string $encoding = null, int $flags = 0, ?string $documentUri = null): Closure
{
return static fn (): XMLReader => disallow_issues(
static function () use ($stream, $encoding, $flags, $documentUri): XMLReader {
return disallow_libxml_false_returns(
XMLReader::fromStream($stream, $encoding, $flags, $documentUri),
'Could not read the provided XML stream!'
);
}
);
}
6 changes: 3 additions & 3 deletions src/Xml/Reader/Loader/xml_string_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
/**
* @return Closure(): XMLReader
*/
function xml_string_loader(string $xml): Closure
function xml_string_loader(string $xml, ?string $encoding = null, int $flags = 0): Closure
{
return static fn (): XMLReader => disallow_issues(
static function () use ($xml): XMLReader {
static function () use ($xml, $encoding, $flags): XMLReader {
Assert::notEmpty($xml, 'The provided XML can not be empty!');

return disallow_libxml_false_returns(
XMLReader::XML($xml),
XMLReader::fromString($xml, $encoding, $flags),
'Could not read the provided XML!'
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Xml/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function VeeWee\Xml\ErrorHandling\stop_on_first_issue;
use function VeeWee\Xml\Internal\configure;
use function VeeWee\Xml\Reader\Loader\xml_file_loader;
use function VeeWee\Xml\Reader\Loader\xml_stream_loader;
use function VeeWee\Xml\Reader\Loader\xml_string_loader;

final class Reader
Expand Down Expand Up @@ -57,6 +58,15 @@ public static function fromXmlString(string $xml, callable ... $configurators):
return self::configure(xml_string_loader($xml), ...$configurators);
}

/**
* @param resource $stream
* @param list<callable(XMLReader): XMLReader> $configurators
*/
public static function fromXmlStream(mixed $stream, callable ... $configurators): self
{
return self::configure(xml_stream_loader($stream), ...$configurators);
}

/**
* @param callable(NodeSequence): bool $matcher
*
Expand Down
11 changes: 11 additions & 0 deletions src/Xml/Writer/Applicative/Applicative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace VeeWee\Xml\Writer\Applicative;

interface Applicative
{
/**
* @return mixed
*/
public function __invoke(\XMLWriter $writer): mixed;
}
18 changes: 18 additions & 0 deletions src/Xml/Writer/Applicative/flush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace VeeWee\Xml\Writer\Applicative;

use XMLWriter;

/**
* @param bool $empty - Whether to empty the buffer or not.
*
* @return \Closure(XMLWriter): mixed
*/
function flush(bool $empty = true): \Closure {
return function (XMLWriter $writer) use ($empty): void {
$writer->flush($empty);
};
}
28 changes: 0 additions & 28 deletions src/Xml/Writer/Configurator/open.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Xml/Writer/Opener/Opener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface Opener
{
public function __invoke(XMLWriter $writer): bool;
public function __invoke(): XMLWriter;
}
6 changes: 2 additions & 4 deletions src/Xml/Writer/Opener/memory_opener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
use XMLWriter;

/**
* @return Closure(XMLWriter): bool XMLWriter
* @return Closure(): XMLWriter
*/
function memory_opener(): Closure
{
return static function (XMLWriter $writer): bool {
return $writer->openMemory();
};
return static fn () => XMLWriter::toMemory();
}
6 changes: 3 additions & 3 deletions src/Xml/Writer/Opener/xml_file_opener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
/**
* @param non-empty-string $file
*
* @return Closure(XMLWriter): bool XMLWriter
* @return Closure(): XMLWriter
*/
function xml_file_opener(string $file): Closure
{
return static function (XMLWriter $writer) use ($file) : bool {
return static function () use ($file) : XMLWriter {
// Try to create the file first.
// If the file exists, it will truncated. (Default behaviour of XMLWriter as well)
// If it cannot be created, it will throw exceptions.
write($file, '', WriteMode::Truncate);

return $writer->openUri($file);
return XMLWriter::toUri($file);
};
}
22 changes: 22 additions & 0 deletions src/Xml/Writer/Opener/xml_stream_opener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace VeeWee\Xml\Writer\Opener;

use Closure;
use Psl\File\WriteMode;
use XMLWriter;
use function Psl\File\write;

/**
* @param resource $stream
*
* @return Closure(): XMLWriter
*/
function xml_stream_opener(mixed $stream): Closure
{
return static function () use ($stream) : XMLWriter {
return XMLWriter::toStream($stream);
};
}
26 changes: 20 additions & 6 deletions src/Xml/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function VeeWee\Xml\Writer\Configurator\open;
use function VeeWee\Xml\Writer\Opener\memory_opener;
use function VeeWee\Xml\Writer\Opener\xml_file_opener;
use function VeeWee\Xml\Writer\Opener\xml_stream_opener;

final class Writer
{
Expand All @@ -24,11 +25,12 @@ private function __construct(XMLWriter $writer)
}

/**
* @param callable(): XMLWriter $opener
* @param list<(callable(XMLWriter): XMLWriter)> $configurators
*/
public static function configure(callable ... $configurators): self
public static function configure(callable $opener, callable ... $configurators): self
{
return self::fromUnsafeWriter(new XMLWriter(), ...$configurators);
return self::fromUnsafeWriter($opener(), ...$configurators);
}

/**
Expand All @@ -45,8 +47,20 @@ public static function fromUnsafeWriter(XMLWriter $writer, callable ... $configu
*/
public static function forFile(string $file, callable ... $configurators): self

Check failure on line 48 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:48:80: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forFile (see https://psalm.dev/047)

Check failure on line 48 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:48:80: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forFile (see https://psalm.dev/047)

Check failure on line 48 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:48:80: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forFile (see https://psalm.dev/047)
{
return self::configure(
open(xml_file_opener($file)),
return self::open(

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:50:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:50:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:50:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:50:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:50:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 50 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:50:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
xml_file_opener($file),
...$configurators
);
}

/**
* @param resource $stream
* @param list<(callable(XMLWriter): XMLWriter)> $configurators
*/
public static function forStream(mixed $stream, callable ... $configurators): self

Check failure on line 60 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:60:83: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forStream (see https://psalm.dev/047)

Check failure on line 60 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:60:83: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forStream (see https://psalm.dev/047)

Check failure on line 60 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:60:83: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::forStream (see https://psalm.dev/047)
{
return self::open(

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:62:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:62:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:62:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:62:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:62:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 62 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:62:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
xml_stream_opener($stream),
...$configurators
);
}
Expand All @@ -56,8 +70,8 @@ public static function forFile(string $file, callable ... $configurators): self
*/
public static function inMemory(callable ... $configurators): self

Check failure on line 71 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:71:67: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::inMemory (see https://psalm.dev/047)

Check failure on line 71 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:71:67: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::inMemory (see https://psalm.dev/047)

Check failure on line 71 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedInferredReturnType

src/Xml/Writer/Writer.php:71:67: MixedInferredReturnType: Could not verify return type 'VeeWee\Xml\Writer\Writer' for VeeWee\Xml\Writer\Writer::inMemory (see https://psalm.dev/047)
{
return self::configure(
open(memory_opener()),
return self::open(

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:73:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:73:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:73:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:73:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

UndefinedMethod

src/Xml/Writer/Writer.php:73:16: UndefinedMethod: Method VeeWee\Xml\Writer\Writer::open does not exist (see https://psalm.dev/022)

Check failure on line 73 in src/Xml/Writer/Writer.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 @ ubuntu-latest

MixedReturnStatement

src/Xml/Writer/Writer.php:73:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
memory_opener(),
...$configurators
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
'Xml\Reader\Configurator\substitute_entities' => __DIR__.'/Xml/Reader/Configurator/substitute_entities.php',
'Xml\Reader\Configurator\xsd_schema' => __DIR__.'/Xml/Reader/Configurator/xsd_schema.php',
'Xml\Reader\Loader\xml_file_loader' => __DIR__.'/Xml/Reader/Loader/xml_file_loader.php',
'Xml\Reader\Loader\xml_stream_loader' => __DIR__.'/Xml/Reader/Loader/xml_stream_loader.php',
'Xml\Reader\Loader\xml_string_loader' => __DIR__.'/Xml/Reader/Loader/xml_string_loader.php',
'Xml\Reader\Matcher\all' => __DIR__.'/Xml/Reader/Matcher/all.php',
'Xml\Reader\Matcher\any' => __DIR__.'/Xml/Reader/Matcher/any.php',
Expand All @@ -142,6 +143,7 @@
'Xml\Reader\Matcher\nested' => __DIR__.'/Xml/Reader/Matcher/nested.php',
'Xml\Reader\Matcher\not' => __DIR__.'/Xml/Reader/Matcher/not.php',
'Xml\Reader\Matcher\sequence' => __DIR__.'/Xml/Reader/Matcher/sequence.php',
'Xml\Writer\Applicative\flush' => __DIR__.'/Xml/Writer/Applicative/flush.php',
'Xml\Writer\Builder\attribute' => __DIR__.'/Xml/Writer/Builder/attribute.php',
'Xml\Writer\Builder\attributes' => __DIR__.'/Xml/Writer/Builder/attributes.php',
'Xml\Writer\Builder\cdata' => __DIR__.'/Xml/Writer/Builder/cdata.php',
Expand All @@ -159,10 +161,10 @@
'Xml\Writer\Builder\raw' => __DIR__.'/Xml/Writer/Builder/raw.php',
'Xml\Writer\Builder\value' => __DIR__.'/Xml/Writer/Builder/value.php',
'Xml\Writer\Configurator\indentation' => __DIR__.'/Xml/Writer/Configurator/indentation.php',
'Xml\Writer\Configurator\open' => __DIR__.'/Xml/Writer/Configurator/open.php',
'Xml\Writer\Mapper\memory_output' => __DIR__.'/Xml/Writer/Mapper/memory_output.php',
'Xml\Writer\Opener\memory_opener' => __DIR__.'/Xml/Writer/Opener/memory_opener.php',
'Xml\Writer\Opener\xml_file_opener' => __DIR__.'/Xml/Writer/Opener/xml_file_opener.php',
'Xml\Writer\Opener\xml_stream_opener' => __DIR__.'/Xml/Writer/Opener/xml_stream_opener.php',
'Xml\Xsd\Schema\Manipulator\base_path' => __DIR__.'/Xml/Xsd/Schema/Manipulator/base_path.php',
'Xml\Xsd\Schema\Manipulator\overwrite_with_local_files' => __DIR__.'/Xml/Xsd/Schema/Manipulator/overwrite_with_local_files.php',
'Xml\Xslt\Configurator\all_functions' => __DIR__.'/Xml/Xslt/Configurator/all_functions.php',
Expand Down
Loading

0 comments on commit 2c1dec3

Please sign in to comment.