Skip to content

Commit

Permalink
autoloader improvements, small testing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Dec 22, 2024
1 parent 0535136 commit c255e74
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 98 deletions.
25 changes: 19 additions & 6 deletions bin/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@
// --- autoload setup
const AUTOLOAD_CLASS_FILEPATH = __DIR__ . '/../vendor/autoload.php';

$autoloadClasspath = realpath(AUTOLOAD_CLASS_FILEPATH);

// ensure composer autoload class exists.
if (!file_exists(AUTOLOAD_CLASS_FILEPATH)) {
if (!$autoloadClasspath) {
echo sprintf("Unable to locate composer autoload file expected at path: %s\n\n", AUTOLOAD_CLASS_FILEPATH);
echo "Please run \"composer install\" from the root of the project directory\n\n";
exit(1);
}

require AUTOLOAD_CLASS_FILEPATH;
require $autoloadClasspath;

// --- use statements
// ----- use statements

use DCarbone\PHPFHIR\Builder;
use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Version\Definition;
use JetBrains\PhpStorm\NoReturn;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
Expand All @@ -59,6 +60,7 @@
const FLAG_ONLY_LIBRARY = '--onlyLibrary';
const FLAG_ONLY_TESTS = '--onlyTests';
const FLAG_VERSIONS = '--versions';
const FLAG_USERAGENT = '--userAgent';
const FLAG_LOG_LEVEL = '--logLevel';

// ----- cli and config opts
Expand All @@ -74,6 +76,7 @@
$versions_to_generate = null;
$use_existing = false;
$log_level = LogLevel::WARNING;
$user_agent = 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0';

// ----- functions

Expand Down Expand Up @@ -127,6 +130,7 @@ function missing_config_text(bool $return): string
#[NoReturn] function exit_with_help(bool $err = false): void
{
global $config_location_def;

$env_var = ENV_GENERATE_CONFIG_FILE;
$out = <<<STRING
Expand Down Expand Up @@ -154,6 +158,8 @@ function missing_config_text(bool $return): string
ex: ./bin/generate.sh --config path/to/file
--versions: Comma-separated list of specific versions to parse from config
ex: ./bin/generate.sh --versions STU3,R4
--userAgent: User-Agent string to use when downloading FHIR schema files
ex: ./bin/generate.sh --userAgent "Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0"
--logLevel: Level of verbosity during generation
ex: ./bin/generate.sh --logLevel warning
Expand Down Expand Up @@ -285,6 +291,13 @@ function is_dir_empty(string $dir): bool
$only_tests = true;
break;

case FLAG_USERAGENT:
$user_agent = trim($next);
if (!$found_equal) {
$i++;
}
break;

default:
echo "Unknown argument \"{$arg}\" passed at position {$i}\n";
exit_with_help(true);
Expand Down Expand Up @@ -420,7 +433,7 @@ function is_dir_empty(string $dir): bool
curl_setopt_array(
$ch,
[
CURLOPT_USERAGENT => 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0',
CURLOPT_USERAGENT => $user_agent,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => 0,
CURLOPT_FILE => $fh,
Expand All @@ -436,7 +449,7 @@ function is_dir_empty(string $dir): bool
exit(1);
}
if ($code !== 200) {
echo sprintf('Error downlodaing from %s: %d (%s)%s', $version_name, $source_url, $code, $resp, PHP_EOL);
echo sprintf('Error downlodaing from %s: %d (%s)%s%s', $version_name, $source_url, $code, $resp, PHP_EOL);
exit(1);
}
}
Expand Down
8 changes: 0 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@
"DCarbone\\PHPFHIR\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"DCarbone\\PHPFHIRGenerated\\": "output/DCarbone/PHPFHIRGenerated"
}
},
"config": {
"platform-check": false
},
"suggest": {
"ext-zip": "To enable unzipping of downloaded source files"
}
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpunit/Core.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="../vendor/autoload.php"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
Expand Down
2 changes: 1 addition & 1 deletion phpunit/DSTU1.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="../vendor/autoload.php"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
Expand Down
34 changes: 19 additions & 15 deletions phpunit/DSTU2.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="../vendor/autoload.php" colors="true" stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://hapi.fhir.org/baseDstu2" force="true" />
</php>
<testsuites>
<testsuite name="DSTU2">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/DSTU2/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://hapi.fhir.org/baseDstu2" force="true"/>
</php>
<testsuites>
<testsuite name="DSTU2">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/DSTU2/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
</phpunit>
34 changes: 19 additions & 15 deletions phpunit/R4.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="../vendor/autoload.php" colors="true" stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r4" force="true" />
</php>
<testsuites>
<testsuite name="R4">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/R4/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r4" force="true"/>
</php>
<testsuites>
<testsuite name="R4">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/R4/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
</phpunit>
34 changes: 19 additions & 15 deletions phpunit/R5.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="../vendor/autoload.php" colors="true" stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r5" force="true" />
</php>
<testsuites>
<testsuite name="R5">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/R5/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r5" force="true"/>
</php>
<testsuites>
<testsuite name="R5">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/R5/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
</phpunit>
34 changes: 19 additions & 15 deletions phpunit/STU3.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="../vendor/autoload.php" colors="true" stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r3" force="true" />
</php>
<testsuites>
<testsuite name="STU3">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/STU3/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="bootstrap.php"
colors="true"
stopOnFailure="false">
<php>
<ini name="memory_limit" value="2048M"/>
<env name="PHPFHIR_TEST_INTEGRATION_ENDPOINT" value="https://server.fire.ly/r3" force="true"/>
</php>
<testsuites>
<testsuite name="STU3">
<directory>../output/DCarbone/PHPFHIRGenerated/Versions/STU3/Tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../src</directory>
</include>
</source>
</phpunit>
37 changes: 37 additions & 0 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/**
* PHPUnit bootstrap script
*
* Copyright 2024 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const COMPOSER_AUTOLOADER_PATH = __DIR__ . '/../vendor/autoload.php';
const GENERATED_AUTOLOADER_PATH = __DIR__ . '/../output/DCarbone/PHPFHIRGenerated/Autoloader.php';

$composerAutoloader = realpath(COMPOSER_AUTOLOADER_PATH);
$generatedAutoloader = realpath(GENERATED_AUTOLOADER_PATH);

if (!$composerAutoloader) {
throw new \RuntimeException(sprintf('Copmoser autoloader class file not found at expected path: %s', COMPOSER_AUTOLOADER_PATH));
}
if (!$generatedAutoloader) {
throw new \RuntimeException(sprintf('Generated autoloader class file not found at expected path: %s', GENERATED_AUTOLOADER_PATH));
}

echo "Requiring composer autoloader: {$composerAutoloader}\n";
require $composerAutoloader;
echo "Requiring generated autoloader: {$generatedAutoloader}\n";
require $generatedAutoloader;
2 changes: 1 addition & 1 deletion src/Utilities/FileUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function buildAutoloaderRelativeFilepath(string $baseNS, string $c
if (str_starts_with($classFQN, $baseNS)) {
$classFQN = ltrim(substr($classFQN, strlen($baseNS)), '\\');
}
return sprintf("'%s.php'", str_replace('\\', "' . DIRECTORY_SEPARATOR . '", $classFQN));
return sprintf("__DIR__ . '/%s.php'", str_replace('\\', "/", $classFQN));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions template/core/classes/class_autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// version autoloaders
<?php foreach($config->getVersionsIterator() as $version): ?>
if (!class_exists('<?php echo $version->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_AUTOLOADER); ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . <?php echo FileUtils::buildAutoloaderRelativeFilepath(
require <?php echo FileUtils::buildAutoloaderRelativeFilepath(
$config->getFullyQualifiedName(false),
$version->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_AUTOLOADER),
); ?>;
Expand Down Expand Up @@ -90,7 +90,7 @@ public static function unregister(): bool
public static function loadClass(string $class): null|bool
{
if (isset(self::_CLASS_MAP[$class])) {
return (bool)require __DIR__ . DIRECTORY_SEPARATOR . self::_CLASS_MAP[$class];
return (bool)require self::_CLASS_MAP[$class];
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions template/core/classes/class_response_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function parseArray(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $vers
return null;
}
if (isset($input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE])) {
$className = $version->getTypeMap()::getTypeClass($input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
$className = $version->getTypeMap()::getTypeClassName($input[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
if (null === $className) {
throw new \UnexpectedValueException(sprintf(
'Provided input has "%s" value of "%s", but it does not map to any known type. Other keys: ["%s"]',
Expand Down Expand Up @@ -110,7 +110,7 @@ public static function parseSimpleXMLElement(<?php echo PHPFHIR_INTERFACE_VERSIO
{
$elementName = $input->getName();
/** @var <?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> $fhirType */
$fhirType = $version->getTypeMap()::getTypeClass($elementName);
$fhirType = $version->getTypeMap()::getTypeClassName($elementName);
if (null === $fhirType) {
throw new \UnexpectedValueException(sprintf(
'Unable to locate FHIR type for root XML element "%s". Input seen: %s',
Expand Down
Loading

0 comments on commit c255e74

Please sign in to comment.