Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/guides-cli/src/Config/XmlFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace phpDocumentor\Guides\Cli\Config;

use DOMAttr;
use DOMElement;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Util\Exception\XmlParsingException;
use Symfony\Component\Config\Util\XmlUtils;
Expand All @@ -22,6 +24,7 @@
use function is_array;
use function is_string;
use function sprintf;
use function trim;

final class XmlFileLoader extends FileLoader
{
Expand All @@ -36,9 +39,45 @@ public function load(mixed $resource, string|null $type = null): array
throw new XmlParsingException(sprintf('The XML file "%s" is not valid.', $resource));
}

// The <project> attributes (title, version, release, copyright) are all
// strings and are read from the DOM directly. XmlUtils::convertDomElementToArray()
// below runs phpize() on every attribute value, which coerces version-like
// strings into numbers ("0.10" would become the float 0.1, "1.0" would
// become 1). Reading them straight from the DOM and detaching <project>
// beforehand keeps the version exactly as written.
$projectConfig = null;
$project = $element->getElementsByTagName('project')->item(0);
if ($project instanceof DOMElement) {
$projectConfig = [];
foreach ($project->attributes as $attribute) {
if (!($attribute instanceof DOMAttr)) {
continue;
}

$value = $attribute->value;

// Backward compatibility: to stop the previous phpize() call from
// turning a version into a number, consumers wrapped it in single
// quotes (e.g. version="'3.0'"). The value is now read straight from
// the DOM so the quotes are no longer needed, but existing guides.xml
// files may still contain them; strip them for these two attributes.
if ($attribute->name === 'version' || $attribute->name === 'release') {
$value = trim($value, "'");
}

$projectConfig[$attribute->name] = $value;
}

$project->parentNode?->removeChild($project);
}

$rootConfig = XmlUtils::convertDomElementToArray($element);
assert(is_array($rootConfig));

if ($projectConfig !== null) {
$rootConfig['project'] = $projectConfig;
}

$configs = [];
if (isset($rootConfig['import'])) {
foreach ((array) $rootConfig['import'] as $import) {
Expand Down
41 changes: 2 additions & 39 deletions packages/guides/src/DependencyInjection/GuidesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@
use function assert;
use function dirname;
use function is_array;
use function is_int;
use function is_string;
use function pathinfo;
use function trim;
use function var_export;

final class GuidesExtension extends Extension implements CompilerPassInterface, ConfigurationInterface, PrependExtensionInterface
{
Expand All @@ -64,42 +61,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('project')
->children()
->scalarNode('title')->end()
->scalarNode('version')
->beforeNormalization()
->always(
// We need to revert the phpize call in XmlUtils. Version is always a string!
static function ($value) {
if (!is_int($value) && !is_string($value)) {
return var_export($value, true);
}

if (is_string($value)) {
return trim($value, "'");
}

return $value;
},
)
->end()
->end()
->scalarNode('release')
->beforeNormalization()
->always(
// We need to revert the phpize call in XmlUtils. Version is always a string!
static function ($value) {
if (!is_int($value) && !is_string($value)) {
return var_export($value, true);
}

if (is_string($value)) {
return trim($value, "'");
}

return $value;
},
)
->end()
->end()
->scalarNode('version')->end()
->scalarNode('release')->end()
->scalarNode('copyright')->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Some Document - Render guides</title>

</head>
<body>
<!-- content start -->
<div class="section" id="some-document">
<h1>Some Document</h1>

<p>Project Render guides in version 3.0, release 3.0.0.</p>

</div>
<!-- content end -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides
xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides vendor/phpdocumentor/guides-cli/resources/schema/guides.xsd"
>
<project
title="Render guides"
version="'3.0'"
release="'3.0.0'"
/>
</guides>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Some Document
=============

Project |project| in version |version|, release |release|.
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Some Document - Render guides</title>

</head>
<body>
<!-- content start -->
<div class="section" id="some-document">
<h1>Some Document</h1>

<p>Project Render guides in version 3.0, release 3.0.0.</p>
<p>Project Render guides in version 0.10, release 3.0.0.</p>

</div>
<!-- content end -->
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>
<project
title="Render guides"
version="'3.0'"
version="0.10"
Comment thread
CybotTM marked this conversation as resolved.
release="3.0.0"
/>
</guides>
Loading