ardenexal/fhir-code-generation
Installation Add the package via Composer:
composer require ardenexal/fhir-code-generation
Ensure php-fhir-tools (parent package) is also installed if dependencies are shared.
First Use Case
Generate a FHIR resource class (e.g., Patient) from a FHIR profile or schema:
use Ardenexal\FhirCodeGeneration\Generator;
$generator = new Generator();
$resourceClass = $generator->generateFromProfile('Patient', 'path/to/profile.json');
Save the output to a file:
file_put_contents('app/FHIR/Patient.php', $resourceClass);
Key Files
src/Generator.php for core logic.examples/ (if available) for sample profiles/schemas.Profile Input Use FHIR profiles (JSON/XML) to define custom resources:
$profile = json_decode(file_get_contents('profiles/CustomObservation.json'), true);
$generator->generateFromProfile('CustomObservation', $profile);
Schema-Based Generation
For base FHIR schemas (e.g., Patient, Encounter):
$generator->generateFromSchema('Encounter', 'FHIR/R4');
Integration with Laravel
app/FHIR/ and autoload via composer.json:
"autoload": {
"psr-4": {
"App\\FHIR\\": "app/FHIR/"
}
}
$this->app->singleton(Generator::class, function ($app) {
return new Generator(config('fhir.generator'));
});
Batch Processing Generate multiple resources in a loop:
$profiles = ['Patient', 'Observation', 'Medication'];
foreach ($profiles as $resource) {
$generator->generateFromProfile($resource, "profiles/{$resource}.json");
}
Dependency Conflicts
php-fhir-tools (parent package) is installed and compatible. Check for version mismatches in composer.json.Profile Validation
Namespace Collisions
FHIR\\ or use a dedicated namespace in composer.json:
"autoload-dev": {
"psr-4": {
"FHIR\\": "app/FHIR/"
}
}
Caching Generated Classes
storage/fhir-generated/ and skip regeneration if files exist.\Log::debug('Profile data:', ['profile' => $profile]);
phpstan.Custom Templates Override default class templates by extending the generator:
class CustomGenerator extends Generator {
protected function getClassTemplate(): string {
return file_get_contents('custom/FHIRClassTemplate.twig');
}
}
Post-Processing Hook into the generation pipeline to modify classes (e.g., add Laravel traits):
$generator->setPostProcessor(function (string $classCode) {
return str_replace('class Patient', 'class Patient extends \Illuminate\Database\Eloquent\Model', $classCode);
});
FHIR Version Support
Explicitly specify FHIR versions (e.g., R4, STU3) to avoid ambiguity:
$generator->generateFromSchema('Patient', 'FHIR/R4');
How can I help you explore Laravel packages today?