Skip to content

Commit 1e964f9

Browse files
committed
closer.
1 parent 21e0f0e commit 1e964f9

File tree

15 files changed

+108
-78
lines changed

15 files changed

+108
-78
lines changed

files/constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
// Core class names
7676
const PHPFHIR_CLASSNAME_AUTOLOADER = 'Autoloader';
7777
const PHPFHIR_CLASSNAME_CONFIG = 'Config';
78-
const PHPFHIR_CLASSNAME_TYPEMAP = 'TypeMap';
7978
const PHPFHIR_CLASSNAME_RESPONSE_PARSER = 'ResponseParser';
8079
const PHPFHIR_CLASSNAME_CONSTANTS = 'Constants';
8180
const PHPFHIR_CLASSNAME_API_CLIENT = 'ApiClient';
@@ -105,6 +104,7 @@
105104
const PHPFHIR_ENUM_API_SORT = 'ApiSortEnum';
106105

107106
// Version Constants
107+
const PHPFHIR_CLASSNAME_VERSION = 'Version';
108108
const PHPFHIR_CLASSNAME_VERSION_TYPEMAP = 'VersionTypeMap';
109109
const PHPFHIR_CLASSNAME_VERSION_CONSTANTS = 'VersionConstants';
110110

src/Builder.php

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use DCarbone\PHPFHIR\Enum\TestType;
2222
use DCarbone\PHPFHIR\Render\Templates;
2323
use DCarbone\PHPFHIR\Utilities\FileUtils;
24-
use RuntimeException;
2524

2625
/**
2726
* Class Builder
@@ -54,17 +53,39 @@ public function render(string ...$versionNames): void
5453
$versionNames = $this->config->listVersions();
5554
}
5655

57-
$this->writeCoreTypeFiles();
56+
// write php-fhir core files
57+
$this->writeCoreFiles(
58+
$this->getCoreTemplateFileIterator(),
59+
$this->config->getClassesPath(),
60+
$this->config->getFullyQualifiedName(true),
61+
$this->config->getFullyQualifiedTestsName(TestType::BASE, true),
62+
['config' => $this->config]
63+
);
5864

65+
// write fhir version files
5966
$this->writeFhirVersionFiles(...$versionNames);
6067

6168
if (!$this->config->isSkipTests()) {
62-
$this->writeFhirTestFiles();
69+
$this->writeFhirVersionTestFiles();
6370
}
6471
}
6572

6673
/**
67-
* Generate FHIR classes only.
74+
* @return \RecursiveIteratorIterator
75+
*/
76+
protected function getVersionCoreTemplateFileIterator(): \RecursiveIteratorIterator
77+
{
78+
return new \RecursiveIteratorIterator(
79+
new \RecursiveDirectoryIterator(
80+
PHPFHIR_TEMPLATE_VERSIONS_CORE_DIR,
81+
\FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS
82+
)
83+
);
84+
}
85+
86+
/**
87+
* Generate FHIR version files.
88+
*
6889
* @throws \ErrorException
6990
* @throws \Exception
7091
*/
@@ -77,13 +98,14 @@ public function writeFhirVersionFiles(string ...$versionNames): void
7798

7899
$log = $this->config->getLogger();
79100

80-
foreach($this->config->getVersionsIterator() as $version) {
101+
foreach ($this->config->getVersionsIterator() as $version) {
81102
if (!in_array($version->getName(), $versionNames, true)) {
82103
continue;
83104
}
84105

85106
$log->startBreak(sprintf('FHIR Version %s Class Generation', $version->getName()));
86107

108+
// write version fhir type files
87109
$definition = $version->getDefinition();
88110

89111
if (!$definition->isDefined()) {
@@ -94,15 +116,27 @@ public function writeFhirVersionFiles(string ...$versionNames): void
94116

95117
$types = $definition->getTypes();
96118

119+
// write version core files
120+
$this->writeCoreFiles(
121+
$this->getVersionCoreTemplateFileIterator(),
122+
$version->getClassesPath(),
123+
$version->getFullyQualifiedName(true),
124+
$version->getFullyQualifiedTestsName(TestType::BASE, true),
125+
[
126+
'version' => $version,
127+
'types' => $definition->getTypes(),
128+
]
129+
);
130+
97131
foreach ($types->getIterator() as $type) {
98132
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */
99133
$log->debug("Generating class for type {$type}...");
100134

101135
// TODO(@dcarbone): revisit with template system refactor
102136
if (PHPFHIR_XHTML_TYPE_NAME === $type->getFHIRName()) {
103-
$classDefinition = Templates::renderXhtmlTypeClass($version, $types, $type);
137+
$classDefinition = Templates::renderVersionXhtmlTypeClass($version, $types, $type);
104138
} else {
105-
$classDefinition = Templates::renderFhirTypeClass($version, $types, $type);
139+
$classDefinition = Templates::renderVersionTypeClass($version, $types, $type);
106140
}
107141
$filepath = FileUtils::buildTypeFilePath($version, $type);
108142
if (!file_put_contents($filepath, $classDefinition)) {
@@ -123,12 +157,14 @@ public function writeFhirVersionFiles(string ...$versionNames): void
123157

124158
/**
125159
* Generate Test classes only. Tests will not pass if FHIR classes have not been built.
160+
*
161+
* @throws \Exception
126162
*/
127-
public function writeFhirTestFiles(string ...$versionNames): void
163+
public function writeFhirVersionTestFiles(string ...$versionNames): void
128164
{
129165
$log = $this->config->getLogger();
130166

131-
foreach($this->config->getVersionsIterator() as $version) {
167+
foreach ($this->config->getVersionsIterator() as $version) {
132168
if (!in_array($version->getName(), $versionNames, true)) {
133169
continue;
134170
}
@@ -164,10 +200,10 @@ public function writeFhirTestFiles(string ...$versionNames): void
164200
}
165201

166202
$log->debug("Generated {$testType->value} test class for type {$type}...");
167-
$classDefinition = Templates::renderFhirTypeClassTest($version, $types, $type, $testType);
168-
$filepath = FileUtils::buildTypeTestFilePath($this->config, $type, $testType);
203+
$classDefinition = Templates::renderVersionTypeClassTest($version, $types, $type, $testType);
204+
$filepath = FileUtils::buildTypeTestFilePath($version, $type, $testType);
169205
if (false === file_put_contents($filepath, $classDefinition)) {
170-
throw new RuntimeException(
206+
throw new \RuntimeException(
171207
sprintf(
172208
'Unable to write Type %s class definition to file %s',
173209
$filepath,
@@ -185,7 +221,7 @@ public function writeFhirTestFiles(string ...$versionNames): void
185221
/**
186222
* @return \RecursiveIteratorIterator
187223
*/
188-
protected function getCoreTypeFileIterator(): \RecursiveIteratorIterator
224+
protected function getCoreTemplateFileIterator(): \RecursiveIteratorIterator
189225
{
190226
return new \RecursiveIteratorIterator(
191227
new \RecursiveDirectoryIterator(
@@ -198,34 +234,45 @@ protected function getCoreTypeFileIterator(): \RecursiveIteratorIterator
198234
/**
199235
* Renders core PHP FHIR type classes, interfaces, traits, and enums.
200236
*
237+
* @param \RecursiveIteratorIterator $dirIterator
238+
* @param string $baseOutputDir
239+
* @param string $baseNS
240+
* @param string $testNS
241+
* @param array $templateArgs
201242
* @return void
202243
*/
203-
protected function writeCoreTypeFiles(): void
244+
protected function writeCoreFiles(
245+
\RecursiveIteratorIterator $dirIterator,
246+
string $baseOutputDir,
247+
string $baseNS,
248+
string $testNS,
249+
array $templateArgs,
250+
): void
204251
{
205252
$this->log->startBreak('Core Files');
206253

207254
// render each core file
208-
foreach($this->getCoreTypeFileIterator() as $fpath => $fi) {
255+
foreach ($dirIterator as $fpath => $fi) {
209256
/** @var $fi \SplFileInfo */
210257

211258
// get filename
212259
$fname = basename($fpath);
213260
// store "type"
214261
$ftype = substr($fname, 0, strpos($fname, '_'));
215262
// trim "type" and ".php"
216-
$fname = strstr(substr($fname, strpos($fname,'_') + 1), '.', true);
263+
$fname = strstr(substr($fname, strpos($fname, '_') + 1), '.', true);
217264
// classname suffix
218265
$suffix = ucfirst($ftype);
219266

220267
// define "default" namespace
221-
$ns = $this->config->getFullyQualifiedName(true);
268+
$ns = $baseNS;
222269

223270
if ('class' === $ftype) {
224271
// 'class' types do have suffix
225272
$suffix = '';
226273
} else if ('test' === $ftype) {
227274
// test classes have different namespace
228-
$ns = $this->config->getFullyQualifiedName(true, TestType::BASE->value);
275+
$ns = $testNS;
229276
// trim subtype
230277
$fname = substr($fname, strpos($fname, '_') + 1);
231278
}
@@ -240,11 +287,11 @@ protected function writeCoreTypeFiles(): void
240287
// write file to disk
241288
$this->writeFile(
242289
FileUtils::buildCoreFilePath(
243-
$this->config,
290+
$baseOutputDir,
244291
$ns,
245292
$cname,
246293
),
247-
Templates::renderCoreType($fpath, $this->config)
294+
Templates::renderCoreTemplate($fpath, $templateArgs)
248295
);
249296
}
250297

@@ -260,7 +307,7 @@ private function writeFile(string $filePath, string $fileContents): void
260307
$this->log->info(sprintf('Writing %s...', $filePath));
261308
$b = file_put_contents($filePath, $fileContents);
262309
if (false === $b) {
263-
throw new RuntimeException(
310+
throw new \RuntimeException(
264311
sprintf(
265312
'Unable to write "%s"',
266313
$filePath

src/Render/Templates.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ abstract class Templates
3232
{
3333
/**
3434
* @param string $coreFilename
35-
* @param \DCarbone\PHPFHIR\Config $config
35+
* @param array $templateArgs
3636
* @return string
3737
*/
38-
public static function renderCoreType(string $coreFilename, Config $config): string
38+
public static function renderCoreTemplate(string $coreFilename, array $templateArgs): string
3939
{
40+
extract($templateArgs);
4041
return require $coreFilename;
4142
}
4243

@@ -46,7 +47,7 @@ public static function renderCoreType(string $coreFilename, Config $config): str
4647
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
4748
* @return string
4849
*/
49-
public static function renderXhtmlTypeClass(Version $version, Types $types, Type $type): string
50+
public static function renderVersionXhtmlTypeClass(Version $version, Types $types, Type $type): string
5051
{
5152
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_xhtml.php';
5253
}
@@ -57,7 +58,7 @@ public static function renderXhtmlTypeClass(Version $version, Types $types, Type
5758
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
5859
* @return string
5960
*/
60-
public static function renderFhirTypeClass(Version $version, Types $types, Type $type): string
61+
public static function renderVersionTypeClass(Version $version, Types $types, Type $type): string
6162
{
6263
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_default.php';
6364
}
@@ -69,7 +70,7 @@ public static function renderFhirTypeClass(Version $version, Types $types, Type
6970
* @param \DCarbone\PHPFHIR\Enum\TestType $testType
7071
* @return string
7172
*/
72-
public static function renderFhirTypeClassTest(Version $version, Types $types, Type $type, TestType $testType): string
73+
public static function renderVersionTypeClassTest(Version $version, Types $types, Type $type, TestType $testType): string
7374
{
7475
return require PHPFHIR_TEMPLATE_VERSION_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType->value . DIRECTORY_SEPARATOR .'class.php';
7576
}

src/Utilities/FileUtils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public static function mkdirRecurse(string ...$bits): string
5656
}
5757

5858
/**
59-
* @param \DCarbone\PHPFHIR\Config $config
59+
* @param string $baseDir
6060
* @param string $namespace
6161
* @param string $filename
6262
* @return string
6363
*/
64-
public static function buildCoreFilePath(Config $config, string $namespace, string $filename): string
64+
public static function buildCoreFilePath(string $baseDir, string $namespace, string $filename): string
6565
{
66-
return self::mkdirRecurse($config->getClassesPath(), self::cleanupPath($namespace)) . DIRECTORY_SEPARATOR . "{$filename}.php";
66+
return self::mkdirRecurse($baseDir, self::cleanupPath($namespace)) . DIRECTORY_SEPARATOR . "{$filename}.php";
6767
}
6868

6969
/**

src/Version.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,6 @@ public function getFullyQualifiedTestsName(TestType $testType, bool $leadingSlas
223223
return $this->getFullyQualifiedName($leadingSlash, $testType->namespaceSlug(), ...$bits);
224224
}
225225

226-
/**
227-
* @return string
228-
*/
229-
public function getVersionClassname(): string
230-
{
231-
return ucfirst($this->name);
232-
}
233-
234226
/**
235227
* @return \DCarbone\PHPFHIR\Version\Definition
236228
*/

src/Version/Definition/TypeImports.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ private function buildImports(): void
222222

223223
$typeNS = $this->type->getFullyQualifiedNamespace(false);
224224
$configNS = $this->type->getConfig()->getFullyQualifiedName(false);
225+
$versionNS = $this->type->getVersion()->getFullyQualifiedName(false);
225226

226227
$sortedProperties = $this->type->getAllPropertiesIterator();
227228

@@ -239,7 +240,7 @@ private function buildImports(): void
239240
// always add the base interface type as its used by the xml serialization func
240241
$this->addImport(PHPFHIR_INTERFACE_TYPE, $configNS);
241242
// always add the constants class as its used everywhere.
242-
$this->addImport(PHPFHIR_CLASSNAME_CONSTANTS, $configNS);
243+
$this->addImport(PHPFHIR_CLASSNAME_VERSION_CONSTANTS, $versionNS);
243244
// add directly implemented interfaces
244245
foreach ($this->type->getDirectlyImplementedInterfaces() as $interface) {
245246
$this->addImport($interface, $configNS);
@@ -273,8 +274,8 @@ private function buildImports(): void
273274

274275
if ($ptk->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE) &&
275276
$typeNS !== $configNS) {
276-
$this->addImport(PHPFHIR_INTERFACE_CONTAINED_TYPE, $configNS);
277-
$this->addImport(PHPFHIR_CLASSNAME_VERSION_TYPEMAP, $configNS);
277+
$this->addImport(PHPFHIR_INTERFACE_CONTAINED_TYPE, $versionNS);
278+
$this->addImport(PHPFHIR_CLASSNAME_VERSION_TYPEMAP, $versionNS);
278279
} else {
279280

280281
if ($ptk === TypeKind::PRIMITIVE_CONTAINER) {

template/core/classes/class_autoloader.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@
7979
}
8080

8181
// enums
82-
if (!enum_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_TYPE); ?>', false)) {
83-
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_TYPE; ?>.php';
84-
}
8582
if (!enum_exists('<?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_API_FORMAT); ?>', false)) {
8683
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_ENUM_API_FORMAT; ?>.php';
8784
}

0 commit comments

Comments
 (0)