21
21
use DCarbone \PHPFHIR \Enum \TestType ;
22
22
use DCarbone \PHPFHIR \Render \Templates ;
23
23
use DCarbone \PHPFHIR \Utilities \FileUtils ;
24
- use RuntimeException ;
25
24
26
25
/**
27
26
* Class Builder
@@ -54,17 +53,39 @@ public function render(string ...$versionNames): void
54
53
$ versionNames = $ this ->config ->listVersions ();
55
54
}
56
55
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
+ );
58
64
65
+ // write fhir version files
59
66
$ this ->writeFhirVersionFiles (...$ versionNames );
60
67
61
68
if (!$ this ->config ->isSkipTests ()) {
62
- $ this ->writeFhirTestFiles ();
69
+ $ this ->writeFhirVersionTestFiles ();
63
70
}
64
71
}
65
72
66
73
/**
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
+ *
68
89
* @throws \ErrorException
69
90
* @throws \Exception
70
91
*/
@@ -77,13 +98,14 @@ public function writeFhirVersionFiles(string ...$versionNames): void
77
98
78
99
$ log = $ this ->config ->getLogger ();
79
100
80
- foreach ($ this ->config ->getVersionsIterator () as $ version ) {
101
+ foreach ($ this ->config ->getVersionsIterator () as $ version ) {
81
102
if (!in_array ($ version ->getName (), $ versionNames , true )) {
82
103
continue ;
83
104
}
84
105
85
106
$ log ->startBreak (sprintf ('FHIR Version %s Class Generation ' , $ version ->getName ()));
86
107
108
+ // write version fhir type files
87
109
$ definition = $ version ->getDefinition ();
88
110
89
111
if (!$ definition ->isDefined ()) {
@@ -94,15 +116,27 @@ public function writeFhirVersionFiles(string ...$versionNames): void
94
116
95
117
$ types = $ definition ->getTypes ();
96
118
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
+
97
131
foreach ($ types ->getIterator () as $ type ) {
98
132
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */
99
133
$ log ->debug ("Generating class for type {$ type }... " );
100
134
101
135
// TODO(@dcarbone): revisit with template system refactor
102
136
if (PHPFHIR_XHTML_TYPE_NAME === $ type ->getFHIRName ()) {
103
- $ classDefinition = Templates::renderXhtmlTypeClass ($ version , $ types , $ type );
137
+ $ classDefinition = Templates::renderVersionXhtmlTypeClass ($ version , $ types , $ type );
104
138
} else {
105
- $ classDefinition = Templates::renderFhirTypeClass ($ version , $ types , $ type );
139
+ $ classDefinition = Templates::renderVersionTypeClass ($ version , $ types , $ type );
106
140
}
107
141
$ filepath = FileUtils::buildTypeFilePath ($ version , $ type );
108
142
if (!file_put_contents ($ filepath , $ classDefinition )) {
@@ -123,12 +157,14 @@ public function writeFhirVersionFiles(string ...$versionNames): void
123
157
124
158
/**
125
159
* Generate Test classes only. Tests will not pass if FHIR classes have not been built.
160
+ *
161
+ * @throws \Exception
126
162
*/
127
- public function writeFhirTestFiles (string ...$ versionNames ): void
163
+ public function writeFhirVersionTestFiles (string ...$ versionNames ): void
128
164
{
129
165
$ log = $ this ->config ->getLogger ();
130
166
131
- foreach ($ this ->config ->getVersionsIterator () as $ version ) {
167
+ foreach ($ this ->config ->getVersionsIterator () as $ version ) {
132
168
if (!in_array ($ version ->getName (), $ versionNames , true )) {
133
169
continue ;
134
170
}
@@ -164,10 +200,10 @@ public function writeFhirTestFiles(string ...$versionNames): void
164
200
}
165
201
166
202
$ 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 );
169
205
if (false === file_put_contents ($ filepath , $ classDefinition )) {
170
- throw new RuntimeException (
206
+ throw new \ RuntimeException (
171
207
sprintf (
172
208
'Unable to write Type %s class definition to file %s ' ,
173
209
$ filepath ,
@@ -185,7 +221,7 @@ public function writeFhirTestFiles(string ...$versionNames): void
185
221
/**
186
222
* @return \RecursiveIteratorIterator
187
223
*/
188
- protected function getCoreTypeFileIterator (): \RecursiveIteratorIterator
224
+ protected function getCoreTemplateFileIterator (): \RecursiveIteratorIterator
189
225
{
190
226
return new \RecursiveIteratorIterator (
191
227
new \RecursiveDirectoryIterator (
@@ -198,34 +234,45 @@ protected function getCoreTypeFileIterator(): \RecursiveIteratorIterator
198
234
/**
199
235
* Renders core PHP FHIR type classes, interfaces, traits, and enums.
200
236
*
237
+ * @param \RecursiveIteratorIterator $dirIterator
238
+ * @param string $baseOutputDir
239
+ * @param string $baseNS
240
+ * @param string $testNS
241
+ * @param array $templateArgs
201
242
* @return void
202
243
*/
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
204
251
{
205
252
$ this ->log ->startBreak ('Core Files ' );
206
253
207
254
// render each core file
208
- foreach ( $ this -> getCoreTypeFileIterator () as $ fpath => $ fi ) {
255
+ foreach ( $ dirIterator as $ fpath => $ fi ) {
209
256
/** @var $fi \SplFileInfo */
210
257
211
258
// get filename
212
259
$ fname = basename ($ fpath );
213
260
// store "type"
214
261
$ ftype = substr ($ fname , 0 , strpos ($ fname , '_ ' ));
215
262
// trim "type" and ".php"
216
- $ fname = strstr (substr ($ fname , strpos ($ fname ,'_ ' ) + 1 ), '. ' , true );
263
+ $ fname = strstr (substr ($ fname , strpos ($ fname , '_ ' ) + 1 ), '. ' , true );
217
264
// classname suffix
218
265
$ suffix = ucfirst ($ ftype );
219
266
220
267
// define "default" namespace
221
- $ ns = $ this -> config -> getFullyQualifiedName ( true ) ;
268
+ $ ns = $ baseNS ;
222
269
223
270
if ('class ' === $ ftype ) {
224
271
// 'class' types do have suffix
225
272
$ suffix = '' ;
226
273
} else if ('test ' === $ ftype ) {
227
274
// test classes have different namespace
228
- $ ns = $ this -> config -> getFullyQualifiedName ( true , TestType:: BASE -> value ) ;
275
+ $ ns = $ testNS ;
229
276
// trim subtype
230
277
$ fname = substr ($ fname , strpos ($ fname , '_ ' ) + 1 );
231
278
}
@@ -240,11 +287,11 @@ protected function writeCoreTypeFiles(): void
240
287
// write file to disk
241
288
$ this ->writeFile (
242
289
FileUtils::buildCoreFilePath (
243
- $ this -> config ,
290
+ $ baseOutputDir ,
244
291
$ ns ,
245
292
$ cname ,
246
293
),
247
- Templates::renderCoreType ($ fpath , $ this -> config )
294
+ Templates::renderCoreTemplate ($ fpath , $ templateArgs )
248
295
);
249
296
}
250
297
@@ -260,7 +307,7 @@ private function writeFile(string $filePath, string $fileContents): void
260
307
$ this ->log ->info (sprintf ('Writing %s... ' , $ filePath ));
261
308
$ b = file_put_contents ($ filePath , $ fileContents );
262
309
if (false === $ b ) {
263
- throw new RuntimeException (
310
+ throw new \ RuntimeException (
264
311
sprintf (
265
312
'Unable to write "%s" ' ,
266
313
$ filePath
0 commit comments