Skip to content

Commit

Permalink
Merge tag 'v2.0.10' into v2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Dec 28, 2024
2 parents 4258824 + acc853e commit 5be1e90
Show file tree
Hide file tree
Showing 139 changed files with 2,223 additions and 1,175 deletions.
57 changes: 36 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,46 @@ Tools for creating PHP classes from the HL7 FHIR Specification
If you're looking to use the classes generated by this library, you may want the
[php-fhir-generated](https://github.com/dcarbone/php-fhir-generated) repo instead.

# Install as Standalone Generator
If you wish to use this package as a standalone generator:

1. Check out the desired branch or tag
2. Execute `composer install` from root of project directory
3. Execute `./bin/generate.sh`
4. Answer all prompts
* If no custom configuration file is defined, definitions will be downloaded to `./input` and
classes will be generated under `./output`
* You can execute `./bin/generate.sh --help` for details on how to utilize this script
* You can configure various aspects of this script by altering the values in [./bin/config.php](./bin/config.php)

This script will download configured major versions of FHIR into the `input` folder and
# Quick start

If you're looking to use to use the classes generated by this library, you may want the [php-fhir-generated](https://github.com/dcarbone/php-fhir-generated) repo instead.

A convenient download and generation script is included in this repository.
The script will download current major versions of FHIR into the `input` folder and
generate classes for every version in the `output` folder.

# Install as Library
If you wish to use the generator as part of a project, you can include it as a composer
dependency:
* Run `composer install`
* Run `php ./bin/generate.php`

Answer all prompts. If no custom configuration file is defined, definitions will be downloaded to `./input` and
classes will be generated under `./output`

Use `./bin/generate --help` to see a list of available options when running the generator script

# Install as Library using Composer

`composer require dcarbone/php-fhir`
This library is registered as a [composer](https://getcomposer.org/) library in
[Packagist](https://packagist.org/packages/dcarbone/php-fhir). You may use it as part of your composer-enabled project
through either of the below means:

From there, you can reference the [Example](#example) block for a quick example on how to
configure and execute the generator.
## Copmoser Require Command

```shell
composer require dcarbone/php-fhir:^v2.0
```

## Composer `require` Map Entry

Require entry:
```json
{
"require": {
"dcarbone/php-fhir": "^v2.0"
}
}
```

# Basic Workflow
## Basic Workflow

The first step is to determine the version of the FHIR spec your implementation supports. Once done, download
the appropriate class definition XSDs from [http://hl7.org/fhir/directory.html](http://hl7.org/fhir/directory.html).
Expand Down Expand Up @@ -138,7 +153,7 @@ require 'path to PHPFHIRResponseParser.php';
// build config
$config = new \YourConfiguredNamespace\PHPFHIRResponseParserConfig([
'registerAutoloader' => true, // use if you are not using Composer
'sxeArgs' => LIBXML_COMPACT | LIBXML_NSCLEAN // choose different SimpleXML arguments if you want, ymmv.
'sxeArgs' => LIBXML_COMPACT | LIBXML_NSCLEAN // choose different libxml arguments if you want, ymmv.
]);

// build parser
Expand Down
4 changes: 4 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!config.php
!generate.php
!generate.sh
5 changes: 3 additions & 2 deletions bin/config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

/**
* Generator default configuration file
*
* Copyright 2017 Pim Koeman (pim@dataground.com)
* Copyright 2017-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2017-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +37,7 @@
// Map of versions and configurations to generate
// Each entry in this map will grab the latest revision of that particular version. If you wish to use a specific
// version, please see http://www.hl7.org/fhir/directory.cfml
'versions' => [
'versions' => [
'DSTU1' => [
// Source URL
'url' => 'http://hl7.org/fhir/DSTU1/fhir-all-xsd.zip',
Expand Down
14 changes: 10 additions & 4 deletions bin/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Download and generation script for all major FHIR versions
*
* Copyright 2017 Pim Koeman (pim@dataground.com)
* Copyright 2017-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2017-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -123,7 +123,7 @@ function exit_with_help($err = false)
PHP-FHIR: Tools for creating PHP classes from the HL7 FHIR Specification
Copyright 2016-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
Copyright 2016-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
- Links:
Source: https://github.com/dcarbone/php-fhir
Expand Down Expand Up @@ -441,9 +441,15 @@ function is_dir_empty($dir)
} else {
echo "ext-zip not found, trying \"unzip\" directly...\n";
$cmd = "unzip -o -qq {$schema_dir}.zip -d {$schema_dir}";
$output = [];
$code = 0;
echo "executing: {$cmd}\n";
if (null !== ($res = shell_exec($cmd))) {
echo "unable to unzip: \"{$res}\". exiting.\n";
exec($cmd, $output, $code);
if (0 !== $code) {
echo "unzip failed with code {$code}\noutput:\n";
foreach($output as $line) {
echo "-----> {$line}\n";
}
exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

/usr/bin/env php ${DIR}/generate.php "$@"
/usr/bin/env php "${DIR}"/generate.php "$@"
12 changes: 10 additions & 2 deletions files/constants.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright 2016-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2016-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,10 @@

// conf defaults
define('PHPFHIR_ROOT_DIR', realpath(dirname(__DIR__)));
define('PHPFHIR_DEFAULT_OUTPUT_DIR', realpath(PHPFHIR_ROOT_DIR . '/output'));
define('PHPFHIR_BIN_DIR', realpath(__DIR__ . '/../bin'));
define('PHPFHIR_DEFAULT_OUTPUT_DIR', realpath(PHPFHIR_ROOT_DIR . DIRECTORY_SEPARATOR . 'output'));
define('PHPFHIR_OUTPUT_TMP_DIR', PHPFHIR_DEFAULT_OUTPUT_DIR . DIRECTORY_SEPARATOR . 'tmp');
define('PHPFHIR_FHIR_VALIDATION_JAR', PHPFHIR_BIN_DIR . DIRECTORY_SEPARATOR . 'org.hl7.fhir.validator.jar');

// format regex
define('PHPFHIR_VARIABLE_NAME_REGEX', '{^[a-zA-Z_][a-zA-Z0-9_]*$}S');
Expand All @@ -33,6 +36,10 @@
// html property
define('PHPFHIR_XHTML_DIV', 'xhtml:div');

// raw type
define('PHPFHIR_RAW_TYPE_NAME', 'raw');
define('PHPFHIR_RAW_TYPE_DESCRIPTION', 'Raw type used in special cases');

// FHIR XML NS
define('PHPFHIR_FHIR_XMLNS', 'http://hl7.org/fhir');

Expand Down Expand Up @@ -80,6 +87,7 @@
// traits
define('PHPFHIR_TRAIT_COMMENT_CONTAINER', 'PHPFHIRCommentContainerTrait');
define('PHPFHIR_TRAIT_VALIDATION_ASSERTIONS', 'PHPFHIRValidationAssertionsTrait');
define('PHPFHIR_TRAIT_CHANGE_TRACKING', 'PHPFHIRChangeTrackingTrait');

// validation constants
define('PHPFHIR_VALIDATION_ENUM', 0x1);
Expand Down
31 changes: 28 additions & 3 deletions files/funcs.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright 2016-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2016-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,14 +16,17 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum;
use DCarbone\PHPFHIR\Config\VersionConfig;
use DCarbone\PHPFHIR\Definition\Type;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;

/**
* @param string $_requiredFile
* @param array $vars
* @return mixed
*/
function require_with($_requiredFile, array $vars) {
function require_with($_requiredFile, array $vars)
{
$num = extract($vars, EXTR_OVERWRITE);
if ($num !== count($vars)) {
throw new \RuntimeException(sprintf(
Expand All @@ -36,3 +39,25 @@ function require_with($_requiredFile, array $vars) {
unset($vars, $num);
return require $_requiredFile;
}

/**
* @param \DCarbone\PHPFHIR\Config\VersionConfig $config
* @return \DCarbone\PHPFHIR\Definition\Type
*/
function build_raw_type(VersionConfig $config)
{
$rt = new Type($config, PHPFHIR_RAW_TYPE_NAME);
$rt->setKind(new TypeKindEnum(TypeKindEnum::RAW));
$rt->addDocumentationFragment(PHPFHIR_RAW_TYPE_DESCRIPTION);
return $rt;
}

/**
* @param \DCarbone\PHPFHIR\Definition\Type $type
*/
function type_debug(Type $type)
{
echo "\n\n\n";
var_dump($type->getConfig()->getVersion()->getName(), $type->getFHIRName());
echo "\n\n\n";
}
21 changes: 9 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.0/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
stopOnFailure="true"
>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true" stopOnFailure="true">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</include>
</coverage>
<php>
<ini name="memory_limit" value="2048M"/>
</php>
Expand Down
59 changes: 37 additions & 22 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR;

/*
* Copyright 2016-2019 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2016-2020 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
use DCarbone\PHPFHIR\Generator\TemplateBuilder;
use DCarbone\PHPFHIR\Utilities\CopyrightUtils;
use DCarbone\PHPFHIR\Utilities\FileUtils;
use RuntimeException;

/**
* Class Builder
Expand Down Expand Up @@ -90,21 +91,22 @@ public function buildFHIRClasses()
$log->startBreak('FHIR Class Generation');
foreach ($types->getIterator() as $type) {
$log->debug("Generating class for type {$type}...");
$classDefinition = TemplateBuilder::generateTypeClass($this->config, $types, $type);
if (null !== $classDefinition) {
$filepath = FileUtils::buildTypeFilePath($this->config, $type);
if (!(bool)file_put_contents($filepath, $classDefinition)) {
throw new \RuntimeException(sprintf(

// TODO: eventually merge "raw" into typical workflow?
if (PHPFHIR_RAW_TYPE_NAME === $type->getFHIRName()) {
$classDefinition = TemplateBuilder::generateRawTypeClass($this->config, $types, $type);
} else {
$classDefinition = TemplateBuilder::generateTypeClass($this->config, $types, $type);
}
$filepath = FileUtils::buildTypeFilePath($this->config, $type);
if (!(bool)file_put_contents($filepath, $classDefinition)) {
throw new RuntimeException(
sprintf(
'Unable to write Type %s class definition to file %s',
$filepath,
$type
));
}
} else {
$log->warning(sprintf(
'Received NULL from generateTypeClass call for type "%s"...',
$type
));
)
);
}
}
$log->endBreak('FHIR Class Generation');
Expand Down Expand Up @@ -148,11 +150,13 @@ public function buildTestClasses()
$classDefinition = TemplateBuilder::generateTypeTestClass($this->config, $types, $type);
$filepath = FileUtils::buildTypeTestFilePath($this->config, $type);
if (!(bool)file_put_contents($filepath, $classDefinition)) {
throw new \RuntimeException(sprintf(
'Unable to write Type %s class definition to file %s',
$filepath,
$type
));
throw new RuntimeException(
sprintf(
'Unable to write Type %s class definition to file %s',
$filepath,
$type
)
);
}
}

Expand Down Expand Up @@ -199,10 +203,12 @@ private function writeClassFile($filePath, $fileContents)
$this->log->info(sprintf('Writing %s...', $filePath));
$b = file_put_contents($filePath, $fileContents);
if (false === $b) {
throw new \RuntimeException(sprintf(
'Unable to write "%s"',
$filePath
));
throw new RuntimeException(
sprintf(
'Unable to write "%s"',
$filePath
)
);
}
$this->log->debug(sprintf('%d bytes written to file %s', $b, $filePath));
}
Expand Down Expand Up @@ -286,6 +292,15 @@ protected function staticClassGeneration()
TemplateBuilder::generatePHPFHIRValidationAssertionsTrait($this->config, $types)
);

$this->writeClassFile(
FileUtils::buildGenericFilePath(
$this->config,
$this->config->getNamespace(true),
PHPFHIR_TRAIT_CHANGE_TRACKING
),
TemplateBuilder::generatePHPFHIRChangeTrackingTrait($this->config, $types)
);

$this->writeClassFile(
FileUtils::buildGenericFilePath(
$this->config,
Expand Down
Loading

0 comments on commit 5be1e90

Please sign in to comment.