Skip to content
Merged
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
32 changes: 25 additions & 7 deletions src/Render/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract class Templates
public static function renderCoreFile(Config $config, CoreFile $coreFile, array $kwargs): string
{
extract($kwargs);
return require $coreFile->getTemplateFile();
return self::normalize(require $coreFile->getTemplateFile());
}

/**
Expand All @@ -48,7 +48,7 @@ public static function renderCoreFile(Config $config, CoreFile $coreFile, array
*/
public static function renderVersionXHTMLTypeClass(Version $version, Type $type): string
{
return require sprintf('%s/class_xhtml.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
return self::normalize(require sprintf('%s/class_xhtml.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
}

/**
Expand All @@ -59,11 +59,11 @@ public static function renderVersionXHTMLTypeClass(Version $version, Type $type)
public static function renderVersionTypeClass(Version $version, Type $type): string
{
if ($type->getKind()->isResourceContainer($version)) {
return require sprintf('%s/class_resource_container.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
return self::normalize(require sprintf('%s/class_resource_container.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
} else if ($type->isPrimitiveType() && !$type->hasPrimitiveTypeParent()) {
return require sprintf('%s/class_primitive.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
return self::normalize(require sprintf('%s/class_primitive.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
}
return require sprintf('%s/class_default.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR);
return self::normalize(require sprintf('%s/class_default.php', PHPFHIR_TEMPLATE_VERSION_TYPES_DIR));
}

/**
Expand All @@ -73,6 +73,24 @@ public static function renderVersionTypeClass(Version $version, Type $type): str
*/
public static function renderVersionTypeClassTest(Version $version, Type $type): string
{
return require sprintf('%s/class.php', PHPFHIR_TEMPLATE_TESTS_VERSIONS_TYPES_DIR);
return self::normalize(require sprintf('%s/class.php', PHPFHIR_TEMPLATE_TESTS_VERSIONS_TYPES_DIR));
}
}

/**
* Normalize whitespace in rendered template output.
*
* Templates are PHP files rendered via output buffering, so indented inline control
* structures (e.g. " <?php endif; ?>") emit their leading indentation as whitespace-only
* or trailing-whitespace lines. Normalizing here, at the single render chokepoint, keeps
* every generated file free of trailing whitespace and ending in exactly one newline,
* without editing each template.
*
* @param string $rendered
* @return string
*/
private static function normalize(string $rendered): string
{
$rendered = preg_replace('/[ \t]+$/m', '', $rendered);
return rtrim($rendered, "\r\n") . "\n";
}
}