Skip to content

Commit 69f6bf0

Browse files
committed
more thoughts
1 parent c4c240a commit 69f6bf0

File tree

22 files changed

+68
-110
lines changed

22 files changed

+68
-110
lines changed

files/funcs.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ function require_with(string $requiredFile, array $vars): mixed
3838
)
3939
);
4040
}
41-
if (!isset($config) || !($config instanceof Config)) {
42-
throw new \LogicException(sprintf(
43-
'Refusing to require "%s" as you didn\'t provide \'config\' => $config(%s)',
44-
$requiredFile,
45-
Config::class,
46-
));
47-
}
4841
// unset vars defined by this func
4942
unset($vars, $num);
5043
return require $requiredFile;

src/Builder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ public function writeFhirVersionFiles(string ...$versionNames): void
100100

101101
// TODO(@dcarbone): revisit with template system refactor
102102
if (PHPFHIR_XHTML_TYPE_NAME === $type->getFHIRName()) {
103-
$classDefinition = Templates::renderXhtmlTypeClass($this->config, $version, $types, $type);
103+
$classDefinition = Templates::renderXhtmlTypeClass($version, $types, $type);
104104
} else {
105-
$classDefinition = Templates::renderFhirTypeClass($this->config, $version, $types, $type);
105+
$classDefinition = Templates::renderFhirTypeClass($version, $types, $type);
106106
}
107-
$filepath = FileUtils::buildTypeFilePath($this->config, $type);
107+
$filepath = FileUtils::buildTypeFilePath($version, $type);
108108
if (!file_put_contents($filepath, $classDefinition)) {
109109
throw new \RuntimeException(
110110
sprintf(
@@ -164,7 +164,7 @@ public function writeFhirTestFiles(string ...$versionNames): void
164164
}
165165

166166
$log->debug("Generated {$testType->value} test class for type {$type}...");
167-
$classDefinition = Templates::renderFhirTypeClassTest($this->config, $version, $types, $type, $testType);
167+
$classDefinition = Templates::renderFhirTypeClassTest($version, $types, $type, $testType);
168168
$filepath = FileUtils::buildTypeTestFilePath($this->config, $type, $testType);
169169
if (false === file_put_contents($filepath, $classDefinition)) {
170170
throw new RuntimeException(
@@ -239,7 +239,7 @@ protected function writeCoreTypeFiles(): void
239239

240240
// write file to disk
241241
$this->writeFile(
242-
FileUtils::buildGenericFilePath(
242+
FileUtils::buildCoreFilePath(
243243
$this->config,
244244
$ns,
245245
$cname,

src/Render/Templates.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,35 @@ public static function renderCoreType(string $coreFilename, Config $config): str
4141
}
4242

4343
/**
44-
* @param \DCarbone\PHPFHIR\Config $config
4544
* @param \DCarbone\PHPFHIR\Version $version
4645
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
4746
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
4847
* @return string
4948
*/
50-
public static function renderXhtmlTypeClass(Config $config, Version $version, Types $types, Type $type): string
49+
public static function renderXhtmlTypeClass(Version $version, Types $types, Type $type): string
5150
{
5251
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_xhtml.php';
5352
}
5453

5554
/**
56-
* @param \DCarbone\PHPFHIR\Config $config
5755
* @param \DCarbone\PHPFHIR\Version $version
5856
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
5957
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
6058
* @return string
6159
*/
62-
public static function renderFhirTypeClass(Config $config, Version $version, Types $types, Type $type): string
60+
public static function renderFhirTypeClass(Version $version, Types $types, Type $type): string
6361
{
6462
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_default.php';
6563
}
6664

6765
/**
68-
* @param \DCarbone\PHPFHIR\Config $config
6966
* @param \DCarbone\PHPFHIR\Version $version
7067
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
7168
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
7269
* @param \DCarbone\PHPFHIR\Enum\TestType $testType
7370
* @return string
7471
*/
75-
public static function renderFhirTypeClassTest(Config $config, Version $version, Types $types, Type $type, TestType $testType): string
72+
public static function renderFhirTypeClassTest(Version $version, Types $types, Type $type, TestType $testType): string
7673
{
7774
return require PHPFHIR_TEMPLATE_VERSION_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType->value . DIRECTORY_SEPARATOR .'class.php';
7875
}

src/Utilities/FileUtils.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
use DCarbone\PHPFHIR\Config;
22+
use DCarbone\PHPFHIR\Version;
2223
use DCarbone\PHPFHIR\Version\Definition\Type;
2324
use DCarbone\PHPFHIR\Enum\TestType;
2425
use RuntimeException;
@@ -34,28 +35,20 @@ abstract class FileUtils
3435
public const REGEX_SLASH_REPLACE = '/';
3536

3637
/**
37-
* @param \DCarbone\PHPFHIR\Config $config
38-
* @param string $pathSuffix
38+
* @param string ...$bits
3939
* @return string
4040
*/
41-
public static function mkdirRecurse(Config $config, string $pathSuffix): string
41+
public static function mkdirRecurse(string ...$bits): string
4242
{
43-
$path = $config->getClassesPath();
44-
foreach (explode('/', str_replace('\\', '/', $pathSuffix)) as $dir) {
45-
$dir = trim($dir, "/");
43+
$path = '';
44+
foreach (preg_split('{[/\\\]}', implode('/', $bits)) as $dir) {
45+
$dir = trim($dir, '/');
4646
if ('' === $dir) {
4747
continue;
4848
}
4949
$path .= DIRECTORY_SEPARATOR .$dir;
50-
if (is_dir($path)) {
51-
$config->getLogger()->debug(sprintf('Directory at path "%s" already exists.', $path));
52-
} else {
53-
$config->getLogger()->info(sprintf('Attempting to create directory at path "%s"...', $path));
54-
if (!mkdir($path)) {
55-
$msg = 'Unable to create directory at path "' . $path . '"';
56-
$config->getLogger()->critical($msg);
57-
throw new RuntimeException($msg);
58-
}
50+
if (!is_dir($path) && !mkdir($path)) {
51+
throw new RuntimeException(sprintf('Unable to create directory at path "%s"', $path));
5952
}
6053
}
6154
return $path;
@@ -67,36 +60,36 @@ public static function mkdirRecurse(Config $config, string $pathSuffix): string
6760
* @param string $filename
6861
* @return string
6962
*/
70-
public static function buildGenericFilePath(Config $config, string $namespace, string $filename): string
63+
public static function buildCoreFilePath(Config $config, string $namespace, string $filename): string
7164
{
72-
return self::mkdirRecurse($config, self::cleanupPath($namespace)) . DIRECTORY_SEPARATOR . "{$filename}.php";
65+
return self::mkdirRecurse($config->getClassesPath(), self::cleanupPath($namespace)) . DIRECTORY_SEPARATOR . "{$filename}.php";
7366
}
7467

7568
/**
76-
* @param \DCarbone\PHPFHIR\Config $config
69+
* @param \DCarbone\PHPFHIR\Version $version
7770
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
7871
* @return string
7972
*/
80-
public static function buildTypeFilePath(Config $config, Type $type): string
73+
public static function buildTypeFilePath(Version $version, Type $type): string
8174
{
8275
return static::mkdirRecurse(
83-
$config,
76+
$version->getClassesPath(),
8477
self::cleanupPath($type->getFullyQualifiedNamespace(false))
85-
) . "/{$type->getClassName()}.php";
78+
) . DIRECTORY_SEPARATOR . "{$type->getClassName()}.php";
8679
}
8780

8881
/**
89-
* @param \DCarbone\PHPFHIR\Config $config
82+
* @param \DCarbone\PHPFHIR\Version $version
9083
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
9184
* @param \DCarbone\PHPFHIR\Enum\TestType $testType
9285
* @return string
9386
*/
94-
public static function buildTypeTestFilePath(Config $config, Type $type, TestType $testType): string
87+
public static function buildTypeTestFilePath(Version $version, Type $type, TestType $testType): string
9588
{
9689
return static::mkdirRecurse(
97-
$config,
90+
$version->getClassesPath(),
9891
self::cleanupPath($type->getFullyQualifiedTestNamespace($testType, false))
99-
) . "/{$type->getTestClassName()}.php";
92+
) . DIRECTORY_SEPARATOR . "{$type->getTestClassName()}.php";
10093
}
10194

10295
/**

src/Version.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ public function __construct(Config $config, string $name, array $params = [])
104104
$this->copyright = new SourceMetadata($config, $this);
105105
}
106106

107+
/**
108+
* @return \DCarbone\PHPFHIR\Config
109+
*/
110+
public function getConfig(): Config
111+
{
112+
return $this->config;
113+
}
114+
107115
/**
108116
* @return \DCarbone\PHPFHIR\Version\SourceMetadata
109117
*/
@@ -148,6 +156,16 @@ public function getSchemaPath(): string
148156
return $this->config->getSchemaPath() . DIRECTORY_SEPARATOR . $this->name;
149157
}
150158

159+
/**
160+
* Returns the specific class output path for this version's generated code
161+
*
162+
* @return string
163+
*/
164+
public function getClassesPath(): string
165+
{
166+
return $this->config->getClassesPath() . DIRECTORY_SEPARATOR . $this->name;
167+
}
168+
151169
/**
152170
* @return string
153171
*/

template/versions/types/class_default.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
use DCarbone\PHPFHIR\Enum\TypeKind;
2020

21-
/** @var \DCarbone\PHPFHIR\Config $config */
2221
/** @var \DCarbone\PHPFHIR\Version $version */
2322
/** @var \DCarbone\PHPFHIR\Version\Definition\Types $types */
2423
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */
@@ -37,7 +36,6 @@
3736
echo require_with(
3837
PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'header_type.php',
3938
[
40-
'config' => $config,
4139
'version' => $version,
4240
'fqns' => $fqns,
4341
'skipImports' => false,
@@ -61,7 +59,6 @@
6159
echo require_with(
6260
PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'definition.php',
6361
[
64-
'config' => $config,
6562
'version' => $version,
6663
'type' => $type,
6764
'parentType' => $parentType
@@ -81,7 +78,6 @@
8178
echo require_with(
8279
PHPFHIR_TEMPLATE_VERSION_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'constants.php',
8380
[
84-
'config' => $config,
8581
'version' => $version,
8682
'property' => $property,
8783
]
@@ -94,7 +90,6 @@
9490
echo require_with(
9591
PHPFHIR_TEMPLATE_VERSION_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'declaration.php',
9692
[
97-
'config' => $config,
9893
'version' => $version,
9994
'property' => $property,
10095
]
@@ -106,7 +101,6 @@
106101
echo require_with(
107102
PHPFHIR_TEMPLATE_VERSION_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'field_map.php',
108103
[
109-
'config' => $config,
110104
'version' => $version,
111105
'type' => $type,
112106
]
@@ -117,7 +111,6 @@
117111
echo require_with(
118112
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'primitive_xml_location_map.php',
119113
[
120-
'config' => $config,
121114
'version' => $version,
122115
'type' => $type,
123116
]
@@ -128,7 +121,6 @@
128121
echo require_with(
129122
PHPFHIR_TEMPLATE_VERSION_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'constructor.php',
130123
[
131-
'config' => $config,
132124
'version' => $version,
133125
'type' => $type,
134126
'properties' => $localProperties,
@@ -140,7 +132,6 @@
140132
echo require_with(
141133
PHPFHIR_TEMPLATE_VERSION_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'common.php',
142134
[
143-
'config' => $config,
144135
'version' => $version,
145136
'type' => $type,
146137
'parentType' => $type->getParentType(),
@@ -153,7 +144,6 @@
153144
echo require_with(
154145
PHPFHIR_TEMPLATE_VERSION_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'contained_type.php',
155146
[
156-
'config' => $config,
157147
'version' => $version,
158148
'type' => $type,
159149
]
@@ -164,7 +154,6 @@
164154
echo require_with(
165155
PHPFHIR_TEMPLATE_VERSION_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'methods.php',
166156
[
167-
'config' => $config,
168157
'version' => $version,
169158
'type' => $type,
170159
'properties' => $localProperties,
@@ -176,7 +165,6 @@
176165
echo require_with(
177166
PHPFHIR_TEMPLATE_VERSION_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods.php',
178167
[
179-
'config' => $config,
180168
'version' => $version,
181169
'type' => $type,
182170
]
@@ -187,7 +175,6 @@
187175
echo require_with(
188176
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml.php',
189177
[
190-
'config' => $config,
191178
'version' => $version,
192179
'type' => $type,
193180
'typeKind' => $typeKind,
@@ -201,7 +188,6 @@
201188
echo require_with(
202189
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json.php',
203190
[
204-
'config' => $config,
205191
'version' => $version,
206192
'type' => $type,
207193
]

template/versions/types/class_xhtml.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
use DCarbone\PHPFHIR\Utilities\NameUtils;
2020

21-
/** @var \DCarbone\PHPFHIR\Config $config */
2221
/** @var \DCarbone\PHPFHIR\Version $version */
2322
/** @var \DCarbone\PHPFHIR\Version\Definition\Types $types */
2423
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */
@@ -34,7 +33,6 @@
3433
echo require_with(
3534
PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'header_type.php',
3635
[
37-
'config' => $config,
3836
'version' => $version,
3937
'fqns' => $fqns,
4038
'skipImports' => false,
@@ -126,7 +124,7 @@ public function setXhtml(null|string|\DOMNode|\SimpleXMLElement $xhtml): self
126124
}
127125

128126
/**
129-
* @param null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
127+
* @param null|<?php echo $version->getConfig()->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
130128
* @return null|\SimpleXMLElement
131129
* @throws \Exception
132130
*/
@@ -143,7 +141,7 @@ public function getSimpleXMLElement(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?>
143141
}
144142

145143
/**
146-
* @param null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
144+
* @param null|<?php echo $version->getConfig()->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
147145
* @return null|\DOMDocument
148146
*/
149147
public function getDOMDocument(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config = null): null|\DOMDocument
@@ -163,7 +161,7 @@ public function getDOMDocument(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $con
163161
/**
164162
* Returns open \XMLReader instance with content read
165163
*
166-
* @param null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
164+
* @param null|<?php echo $version->getConfig()->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
167165
* @return null|\XMLReader
168166
*/
169167
public function getXMLReader(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config = null): null|\XMLReader
@@ -185,7 +183,6 @@ public function getXMLReader(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $confi
185183
echo require_with(
186184
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'unserialize' . DIRECTORY_SEPARATOR . 'header.php',
187185
[
188-
'config' => $config,
189186
'version' => $version,
190187
'type' => $type,
191188
'typeKind' => $type->getKind(),
@@ -199,9 +196,9 @@ public function getXMLReader(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $confi
199196
}
200197

201198
/**
202-
* @param null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_XML_WRITER); ?> $xw
199+
* @param null|<?php echo $version->getConfig()->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_XML_WRITER); ?> $xw
203200
* @param null|int|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_CLASSNAME_CONFIG; ?> $config XML serialization config. Supports an integer value interpreted as libxml opts for backwards compatibility.
204-
* @return <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_XML_WRITER); ?>
201+
* @return <?php echo $version->getConfig()->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_XML_WRITER); ?>
205202

206203
*/
207204
public function xmlSerialize(null|<?php echo PHPFHIR_CLASSNAME_XML_WRITER; ?> $xw = null, null|int|<?php echo PHPFHIR_CLASSNAME_CONFIG ?> $config = null): <?php echo PHPFHIR_CLASSNAME_XML_WRITER; ?>

0 commit comments

Comments
 (0)