Skip to content

Commit

Permalink
further improvements to import management
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 5, 2025
1 parent 0d98ebd commit 04c2fb3
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 124 deletions.
21 changes: 19 additions & 2 deletions src/Builder/Imports.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/

use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Version;
use DCarbone\PHPFHIR\Version\Definition\Type;

class Imports implements \Countable
Expand Down Expand Up @@ -93,7 +93,7 @@ function (Import $a, Import $b) {
return $this;
}

public function addCoreFileImports(string ...$entityNames): self
public function addCoreFileImportsByName(string ...$entityNames): self
{
foreach ($entityNames as $en) {
$coreFile = $this->_config->getCoreFiles()->getCoreFileByEntityName($en);
Expand All @@ -102,6 +102,23 @@ public function addCoreFileImports(string ...$entityNames): self
return $this;
}

public function addVersionCoreFileImportsByName(Version $version, string ...$entityNames): self
{
foreach ($entityNames as $en) {
$coreFile = $version->getCoreFiles()->getCoreFileByEntityName($en);
$this->addImport($coreFile->getNamespace(), $coreFile->getEntityName());
}
return $this;
}

public function addVersionTypeImports(Type ...$types): self
{
foreach($types as $type) {
$this->addImport($type->getFullyQualifiedNamespace(false), $type->getClassName());
}
return $this;
}

/**
* @return \DCarbone\PHPFHIR\Builder\Import[]
*/
Expand Down
26 changes: 13 additions & 13 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public static function buildVersionTypeImports(Type $type): void
$allProperties = $type->getAllPropertiesIndexedIterator();

if (!$type->isAbstract()) {
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_XML_WRITER);
$imports->addCoreFileImports(PHPFHIR_ENUM_XML_LOCATION);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_XML_WRITER);
$imports->addCoreFileImportsByName(PHPFHIR_ENUM_XML_LOCATION);
}

$imports->addVersionCoreFileImport(PHPFHIR_CLASSNAME_VERSION);
$imports->addVersionCoreFileImport(PHPFHIR_CLASSNAME_VERSION_CONSTANTS);
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG);
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_SERIALIZE_CONFIG);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION_CONSTANTS);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_SERIALIZE_CONFIG);

$imports->addCoreFileImports(PHPFHIR_INTERFACE_TYPE);
$imports->addCoreFileImportsByName(PHPFHIR_INTERFACE_TYPE);

foreach ($type->getDirectlyImplementedInterfaces() as $interface => $namespace) {
$imports->addImport($namespace, $interface);
Expand All @@ -74,7 +74,7 @@ public static function buildVersionTypeImports(Type $type): void
if (($type->isCommentContainer() && !$type->hasCommentContainerParent()) ||
$type->hasLocalPropertiesWithValidations() ||
($typeKind->isOneOf(TypeKindEnum::PRIMITIVE) && !$type->hasPrimitiveParent())) {
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_CONSTANTS);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
}

if ($parentType = $type->getParentType()) {
Expand All @@ -83,7 +83,7 @@ public static function buildVersionTypeImports(Type $type): void
}

if ($type->hasLocalPropertiesWithValidations()) {
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_VALIDATOR);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_VALIDATOR);
}

if ($restrictionBaseType = $type->getRestrictionBaseFHIRType()) {
Expand All @@ -105,10 +105,10 @@ public static function buildVersionTypeImports(Type $type): void

if ($ptk->isOneOf(TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE) &&
$typeNS !== $configNS) {
$imports->addCoreFileImports(PHPFHIR_CLASSNAME_CONSTANTS);
$imports->addVersionCoreFileImport(PHPFHIR_INTERFACE_VERSION_CONTAINED_TYPE);
$imports->addVersionCoreFileImport(PHPFHIR_CLASSNAME_VERSION_TYPE_MAP);
$imports->addVersionCoreFileImport(PHPFHIR_CLASSNAME_VERSION);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_INTERFACE_VERSION_CONTAINED_TYPE);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION_TYPE_MAP);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION);
} else {
if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$primType = $propertyType->getLocalProperties()->getProperty('value')->getValueFHIRType();
Expand Down
14 changes: 7 additions & 7 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Version\VersionImports;
use DCarbone\PHPFHIR\Builder\Imports;
use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum;
use DCarbone\PHPFHIR\Enum\TestTypeEnum;
Expand Down Expand Up @@ -99,8 +99,8 @@ class Type
private bool $valueContainer = false;
/** @var bool */
private bool $commentContainer = false;
/** @var \DCarbone\PHPFHIR\Version\VersionImports */
private VersionImports $imports;
/** @var \DCarbone\PHPFHIR\Builder\Imports */
private Imports $imports;

/**
* Type constructor.
Expand Down Expand Up @@ -128,8 +128,8 @@ public function __construct(
$this->sourceFilename = $sourceFilename;
$this->localProperties = new Properties($config, $version, $this);
$this->enumeration = new Enumeration();
$this->imports = new VersionImports(
$version,
$this->imports = new Imports(
$config,
$this->getFullyQualifiedNamespace(false),
$this->getClassName(),
);
Expand Down Expand Up @@ -170,9 +170,9 @@ public function getFHIRName(): string
}

/**
* @return \DCarbone\PHPFHIR\Version\VersionImports
* @return \DCarbone\PHPFHIR\Builder\Imports
*/
public function getImports(): VersionImports
public function getImports(): Imports
{
return $this->imports;
}
Expand Down
49 changes: 0 additions & 49 deletions src/Version/VersionImports.php

This file was deleted.

2 changes: 1 addition & 1 deletion template/core/class_factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_FACTORY_CONFIG,
PHPFHIR_INTERFACE_VERSION,
);
Expand Down
2 changes: 1 addition & 1 deletion template/core/class_version_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$imports = $coreFile->getImports();
$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG,
);
Expand Down
2 changes: 1 addition & 1 deletion template/core/client/class_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$coreFiles = $config->getCoreFiles();
$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_CLIENT_CONFIG,
PHPFHIR_CLASSNAME_CLIENT_REQUEST,
PHPFHIR_CLASSNAME_CLIENT_RESPONSE,
Expand Down
2 changes: 1 addition & 1 deletion template/core/interface_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG,
PHPFHIR_CLASSNAME_XML_WRITER,
Expand Down
12 changes: 10 additions & 2 deletions template/core/interface_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$imports = $coreFile->getImports();

$imports->addCoreFileImportsByName(
PHPFHIR_INTERFACE_VERSION_CONFIG,
PHPFHIR_INTERFACE_VERSION_TYPE_MAP,
);

$coreFiles = $config->getCoreFiles();

$versionConfigInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_INTERFACE_VERSION_CONFIG);
Expand All @@ -32,8 +41,7 @@
<?php echo $config->getBasePHPFHIRCopyrightComment(false); ?>


use <?php echo $versionConfigInterface->getFullyQualifiedName(false); ?>;
use <?php echo $versionTypeMapInterface->getFullyQualifiedName(false); ?>;
<?php echo ImportUtils::compileImportStatements($imports); ?>

interface <?php echo PHPFHIR_INTERFACE_VERSION; ?>

Expand Down
2 changes: 1 addition & 1 deletion template/core/interface_version_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use DCarbone\PHPFHIR\Utilities\ImportUtils;

$imports = $coreFile->getImports();
$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG,
);
Expand Down
2 changes: 1 addition & 1 deletion template/core/interface_version_type_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function isContainableResource(string|array|\SimpleXMLElement|<?ph
public static function getContainedTypeClassNameFromXML(\SimpleXMLElement $node): string;

/**
* @param array|null $data
* @param array $data
* @return string Fully qualified class name of contained resource type
*/
public static function getContainedTypeClassNameFromArray(array $data): string;
Expand Down
8 changes: 2 additions & 6 deletions template/core/trait_comment_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,19 @@ public function _getFHIRComments(): array
/**
* Set internal fhir_comments list, overwriting any previous value(s)
* @param array $fhirComments
* @return self
*/
public function _setFHIRComments(array $fhirComments): self
public function _setFHIRComments(array $fhirComments): void
{
$this->_fhirComments = $fhirComments;
return $this;
}

/**
* Append comment string to internal fhir_comments list
* @param string $fhirComment
* @return self
*/
public function _addFHIRComment(string $fhirComment): self
public function _addFHIRComment(string $fhirComment): void
{
$this->_fhirComments[] = $fhirComment;
return $this;
}
}
<?php
Expand Down
2 changes: 1 addition & 1 deletion template/core/validation/class_validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$coreFiles = $config->getCoreFiles();
$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$imports->addCoreFileImportsByName(
PHPFHIR_CLASSNAME_CONSTANTS,
PHPFHIR_INTERFACE_TYPE,
PHPFHIR_INTERFACE_PRIMITIVE_TYPE,
Expand Down
Loading

0 comments on commit 04c2fb3

Please sign in to comment.