forked from dcarbone/php-fhir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_autoloader.php
More file actions
112 lines (95 loc) · 4.04 KB
/
Copy pathclass_autoloader.php
File metadata and controls
112 lines (95 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php declare(strict_types=1);
/*
* Copyright 2018-2025 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.
* 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.
*/
use DCarbone\PHPFHIR\Utilities\FileUtils;
/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */
$coreFiles = $config->getCoreFiles();
ob_start();
echo '<?php ';?>declare(strict_types=1);
namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;
<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>
abstract class <?php echo $coreFile; ?>
{
private const _ROOT_NAMESPACE = '<?php echo $config->getFullyQualifiedName(false); ?>\\';
private const _VERSION_AUTOLOADER_MAP = [
<?php foreach($config->getVersionsIterator() as $i => $version): ?>
<?php echo $i; ?> => [
'<?php echo $version->getFullyQualifiedName(false); ?>\\',
'<?php echo $version->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_AUTOLOADER); ?>',
<?php echo FileUtils::buildAutoloaderRelativeFilepath(
$config->getFullyQualifiedName(false),
$version->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_AUTOLOADER),
); ?>,
],
<?php endforeach; ?>
];
private const _CORE_CLASS_MAP = [
// core types
<?php foreach($coreFiles->getIterator() as $cf): if ($cf->isAutoloader() || $cf->isTest()) { continue; } ?>
'<?php echo $cf->getFullyQualifiedName(false); ?>' => <?php echo FileUtils::buildAutoloaderRelativeFilepath(
$config->getFullyQualifiedName(false),
$cf->getFullyQualifiedName(false),
); ?>,
<?php endforeach; ?> ];
private static bool $_registered = false;
private static array $_versionRegistered = [
<?php foreach($config->getVersionsIterator() as $i => $version) : ?>
<?php echo $i; ?> => false,
<?php endforeach; ?>
];
public static function register(): bool
{
if (!self::$_registered) {
self::$_registered = spl_autoload_register(self::class . '::loadClass');
}
return self::$_registered;
}
public static function unregister(): bool
{
if (self::$_registered) {
if (spl_autoload_unregister(self::class . '::loadClass')) {
self::$_registered = false;
return true;
}
}
return false;
}
public static function loadClass(string $class): null|bool
{
if (isset(self::_CORE_CLASS_MAP[$class])) {
return (bool)require self::_CORE_CLASS_MAP[$class];
} else if (!str_starts_with($class, self::_ROOT_NAMESPACE)) {
return null;
}<?php foreach($config->getVersionsIterator() as $i => $version): ?> else if (str_starts_with($class, self::_VERSION_AUTOLOADER_MAP[<?php echo $i; ?>][0])) {
if (self::$_versionRegistered[<?php echo $i; ?>]) {
return null;
}
require self::_VERSION_AUTOLOADER_MAP[<?php echo $i; ?>][2];
<?php echo $version->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_AUTOLOADER); ?>::register();
self::$_versionRegistered[<?php echo $i; ?>] = true;
if ($class !== self::_VERSION_AUTOLOADER_MAP[<?php echo $i; ?>][1]) {
return <?php echo $version->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_AUTOLOADER); ?>::loadClass($class);
} else {
return true;
}
}<?php endforeach; ?> else {
return null;
}
}
}
<?php echo PHPFHIR_CLASSNAME_AUTOLOADER; ?>::register();
<?php return ob_get_clean();