| description | Generate PHP model classes from FHIR Structure Definitions and ValueSets. |
|---|---|
| icon | gears |
The Code Generation component loads FHIR packages from the registry and generates strongly-typed PHP
classes from their definitions. A StructureDefinition is FHIR's machine-readable description of a
resource or data type, and a ValueSet is a named set of permitted codes. The generator turns them
into Resources, DataTypes, Primitives, and Enums. It is the engine behind the
fhir:generate and fhir:generate-ig console
commands.
Namespace: Ardenexal\FHIRTools\Component\CodeGeneration\.
There are two distinct workflows:
| Workflow | Page |
|---|---|
| Generate the base FHIR models (R4 / R4B / R5) | Base FHIR Models |
| Generate typed extension & profile classes for an Implementation Guide | Implementation Guides |
Both commands share the same pipeline, implemented with Nette PhpGenerator:
- Package loading —
PackageLoaderdownloads and caches FHIR packages (.tgz) from the FHIR registry (packages.fhir.org), verifying integrity and resolving versions. Cached packages can be reused with--offline. - Definition parsing —
loadPackageStructureDefinitions()readsStructureDefinitionandValueSetJSON resources out of each package into a per-versionBuilderContext. - Class generation — each
StructureDefinitionis routed by itskindto a generator that emits a PHP class or enum:kind: resource→ Resources (PatientResource,ObservationResource, …)kind: complex-type→ DataTypes (HumanName,Address, …)kind: primitive-type→ Primitives (StringPrimitive,BooleanPrimitive, …)- referenced
ValueSetbindings → Enums (AdministrativeGender, …) plus a…Typecode wrapper class inDataType/
fhir:generate runs in three global phases — load all requested packages, clear only the
output directories for the affected FHIR versions, then generate — so generating one version
never clobbers another. It skips logical models and derivation: constraint definitions (profiles
and named extensions); those are the domain of fhir:generate-ig.
| Class | Location under …\CodeGeneration\ |
Responsibility |
|---|---|---|
PackageLoader |
Package\PackageLoader |
Download, cache, extract, and parse FHIR packages |
BuilderContext |
Context\BuilderContext |
Hold definitions, generated types, enums, and namespace registrations during a run |
FHIRModelGenerator |
Generator\FHIRModelGenerator |
Generate a PHP class from a base StructureDefinition |
FHIRValueSetGenerator |
Generator\FHIRValueSetGenerator |
Generate a PHP enum from a ValueSet |
FHIRExtensionGenerator |
Generator\FHIRExtensionGenerator |
Generate typed extension classes (type: Extension) |
FHIRProfileGenerator |
Generator\FHIRProfileGenerator |
Generate profile subclasses (derivation: constraint) |
ErrorCollector |
Generator\ErrorCollector |
Collect non-fatal errors/warnings for end-of-run reporting |
See Base FHIR Models for the generator APIs in detail and Implementation Guides for the extension/profile generators.
{% hint style="warning" %}
Generated model files under src/Component/Models/src/ are never hand-edited — always
regenerate. See Generated Output Structure.
{% endhint %}