Skip to content

Commit

Permalink
using combination of xml reader and xml writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed May 2, 2024
1 parent 6fb3d74 commit da59ffa
Show file tree
Hide file tree
Showing 26 changed files with 556 additions and 279 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"require-dev": {
"ext-curl": "*",
"ext-dom": "*",
"ext-xmlreader": "*",
"ext-xmlwriter": "*",
"phpunit/phpunit": "^10.5 || ^11.0",
"monolog/monolog": "^3.2.0"
},
Expand Down
3 changes: 2 additions & 1 deletion files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
const PHPFHIR_CLASSNAME_TYPEMAP = 'PHPFHIRTypeMap';
const PHPFHIR_CLASSNAME_DEBUG_CLIENT = 'PHPFHIRDebugClient';
const PHPFHIR_CLASSNAME_DEBUG_CLIENT_RESPONSE = 'PHPFHIRDebugClientResponse';
const PHPFHIR_CLASSNAME_XML_WRITER = 'PHPFHIRXmlWriter';

// Core interface names
const PHPFHIR_INTERFACE_TYPE = 'PHPFHIRTypeInterface';
Expand All @@ -96,7 +97,7 @@
const PHPFHIR_TRAIT_COMMENT_CONTAINER = 'PHPFHIRCommentContainerTrait';
const PHPFHIR_TRAIT_VALIDATION_ASSERTIONS = 'PHPFHIRValidationAssertionsTrait';
const PHPFHIR_TRAIT_CHANGE_TRACKING = 'PHPFHIRChangeTrackingTrait';
const PHPFHIR_TRAIT_XMLNS = 'PHPFHIRXmlNamespaceTrait';
const PHPFHIR_TRAIT_SOURCE_XMLNS = 'PHPFHIRSourceXmlNamespaceTrait';

// Core enums
const PHPFHIR_ENUM_CONFIG_KEY = 'PHPFHIRConfigKeyEnum';
Expand Down
4 changes: 2 additions & 2 deletions src/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public function getDirectlyUsedTraits(): array
$traits,
PHPFHIR_TRAIT_VALIDATION_ASSERTIONS,
PHPFHIR_TRAIT_CHANGE_TRACKING,
PHPFHIR_TRAIT_XMLNS,
PHPFHIR_TRAIT_SOURCE_XMLNS,
);
}
} else if (!$parentType->hasLocalProperties()) {
Expand All @@ -765,7 +765,7 @@ public function getDirectlyUsedTraits(): array
$traits,
PHPFHIR_TRAIT_VALIDATION_ASSERTIONS,
PHPFHIR_TRAIT_CHANGE_TRACKING,
PHPFHIR_TRAIT_XMLNS,
PHPFHIR_TRAIT_SOURCE_XMLNS,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Definition/TypeImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ private function buildImports(): void

$sortedProperties = $this->type->getAllPropertiesIterator();

// always add xml serializable interface and root config to all non-abstract types
// non-abstract types must import config and xml writer
if (!$this->type->isAbstract()) {
$this->addImport(PHPFHIR_CLASSNAME_CONFIG, $configNS);
$this->addImport(PHPFHIR_CLASSNAME_XML_WRITER, $configNS);
}

// if this type is in a nested namespace, there are a few base interfaces, classes, and traits
Expand Down
62 changes: 36 additions & 26 deletions template/core/classes/class_autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
/** @var \DCarbone\PHPFHIR\Definition\Types $types */

$namespace = $config->getFullyQualifiedName(false);
$nsPrefix = "{$namespace}\\";

ob_start();

Expand All @@ -38,62 +37,73 @@
echo "\n\n"; ?>
// if this class is used, assume not using Composer...

// constants
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONSTANTS); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>.php';
}

// config
if (!enum_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_CONFIG_KEY); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>.php';
}
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_CONFIG; ?>.php';
}

// xml writer
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_XML_WRITER); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_XML_WRITER; ?>.php';
}

// interfaces
if (!interface_exists('\<?php echo $nsPrefix . PHPFHIR_INTERFACE_TYPE; ?>', false)) {
if (!interface_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_INTERFACE_TYPE; ?>.php';
}
if (!interface_exists('\<?php echo $nsPrefix . PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?>', false)) {
if (!interface_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_PRIMITIVE_TYPE); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?>.php';
}
if (!interface_exists('\<?php echo $nsPrefix . PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>', false)) {
if (!interface_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_CONTAINED_TYPE); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>.php';
}
if (!interface_exists('\<?php echo $nsPrefix . PHPFHIR_INTERFACE_COMMENT_CONTAINER; ?>', false)) {
if (!interface_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_COMMENT_CONTAINER); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_INTERFACE_COMMENT_CONTAINER; ?>.php';
}

// traits
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_COMMENT_CONTAINER; ?>', false)) {
if (!trait_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_TRAIT_COMMENT_CONTAINER); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_COMMENT_CONTAINER; ?>.php';
}
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_VALIDATION_ASSERTIONS; ?>', false)) {
if (!trait_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_TRAIT_VALIDATION_ASSERTIONS); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_VALIDATION_ASSERTIONS; ?>.php';
}
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_CHANGE_TRACKING; ?>', false)) {
if (!trait_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_TRAIT_CHANGE_TRACKING); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_CHANGE_TRACKING; ?>.php';
}
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_XMLNS; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_XMLNS; ?>.php';
if (!trait_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_TRAIT_SOURCE_XMLNS); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_SOURCE_XMLNS; ?>.php';
}

// enums
if (!enum_exists('\<?php echo $nsPrefix . PHPFHIR_ENUM_CONFIG_KEY; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>.php';
}
if (!enum_exists('\<?php echo $nsPrefix . PHPFHIR_ENUM_TYPE; ?>', false)) {
if (!enum_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_TYPE); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_TYPE; ?>.php';
}
if (!enum_exists('\<?php echo $nsPrefix . PHPFHIR_ENUM_API_FORMAT; ?>', false)) {
if (!enum_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_API_FORMAT); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_API_FORMAT; ?>.php';
}

// classes
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_CONSTANTS; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>.php';
}
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_TYPEMAP; ?>', false)) {
// parser classes
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_TYPEMAP); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_TYPEMAP; ?>.php';
}
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_CONFIG; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_CONFIG; ?>.php';
}
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>', false)) {
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>.php';
}
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_DEBUG_CLIENT_RESPONSE; ?>', false)) {

// debug client
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_DEBUG_CLIENT_RESPONSE); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_DEBUG_CLIENT_RESPONSE; ?>.php';
}
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_DEBUG_CLIENT; ?>', false)) {
if (!class_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_DEBUG_CLIENT); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_CLASSNAME_DEBUG_CLIENT; ?>.php';
}

Expand Down
56 changes: 50 additions & 6 deletions template/core/classes/class_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@
*/
class <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> implements \JsonSerializable
{
public const DEFAULT_LIBXML_OPTS = LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL;

/** @var bool */
private bool $registerAutoloader = false;

/** @var int */
private int $libxmlOpts;

/** @var string */
private string $rootXmlns;

/** @var bool */
private bool $overrideSourceXmlns;

/**
* <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> Constructor
* @param array $config
Expand All @@ -67,7 +71,7 @@ public function __construct(array $config = [])
/**
* Set arbitrary key on this config
*
* @param \<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_ENUM_CONFIG_KEY; ?>|string $key
* @param <?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_CONFIG_KEY); ?>|string $key
* @param mixed $value
* @return static
*/
Expand Down Expand Up @@ -117,7 +121,47 @@ public function setLibxmlOpts(int $libxmlOpts): self
*/
public function getLibxmlOpts(): int
{
return $this->libxmlOpts ?? static::DEFAULT_LIBXML_OPTS;
return $this->libxmlOpts ?? <?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::DEFAULT_LIBXML_OPTS;
}

/**
* Default root xmlns to use.
*
* @param string $rootXmlns
* @return static
*/
public function setRootXmlns(string $rootXmlns): self
{
$this->rootXmlns = $rootXmlns;
return $this;
}

/**
* @return null|string
*/
public function getRootXmlns(): null|string
{
return $this->rootXmlns ?? null;
}

/**
* If true, overrides the xmlns entry found at the root of a source document, if there was one.
*
* @param bool $overrideSourceXmlns
* @return static
*/
public function setOverrideSourceXmlns(bool $overrideSourceXmlns): self
{
$this->overrideSourceXmlns = $overrideSourceXmlns;
return $this;
}

/**
* @return bool
*/
public function getOverrideSourceXmlns(): bool
{
return $this->overrideSourceXmlns ?? false;
}

/**
Expand All @@ -126,8 +170,8 @@ public function getLibxmlOpts(): int
public function jsonSerialize(): \stdClass
{
$out = new \stdClass();
foreach(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>::values() as $k => $_) {
$out->{$k} = $this->{'get'.$k}();
foreach(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>::cases() as $key) {
$out->{$k} = $this->{$key->getter()}();
}
return $out;
}
Expand Down
6 changes: 5 additions & 1 deletion template/core/classes/class_constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<?php endif; ?>

*/
abstract class <?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>
final class <?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>

{
// FHIR source
Expand All @@ -52,6 +52,10 @@ abstract class <?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>
// PHPFHIR
public const CODE_GENERATION_DATE = '<?php echo CopyrightUtils::getStandardDate(); ?>';

// Config Defaults
public const DEFAULT_LIBXML_OPTS = LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL;
public const DEFAULT_XMLNS = 'https://hl7.org/fhir';

// Common
public const JSON_FIELD_RESOURCE_TYPE = 'resourceType';
public const JSON_FIELD_FHIR_COMMENTS = 'fhir_comments';
Expand Down
Loading

0 comments on commit da59ffa

Please sign in to comment.