Use class map for autoloading

Close #7
This commit is contained in:
Joshua Ramon Enslin 2021-03-06 12:45:51 +01:00
parent de0d963048
commit 2ec0aa47ef
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
3 changed files with 115 additions and 4 deletions

View File

@ -27,15 +27,20 @@ require_once __DIR__ . '/../vendor/autoload.php';
*/ */
function mdCsvxmlAutoloader(string $className):void { function mdCsvxmlAutoloader(string $className):void {
$classDirs = AUTOLOAD_DIRS; // Try using class map as defined through /scripts/buildClassMap.php
foreach ($classDirs as $classDir) { if (isset(AUTOLOAD_CLASS_MAP[$className])) {
include AUTOLOAD_CLASS_MAP[$className];
return;
}
// Fallback: Load classes by autoload directories
foreach (AUTOLOAD_DIRS as $classDir) {
if (\file_exists("$classDir/$className.php")) { if (\file_exists("$classDir/$className.php")) {
include "$classDir/$className.php"; include "$classDir/$className.php";
return; return;
} }
} }
} }
@ -309,7 +314,8 @@ function rrmdir(string $dir):void {
$objects = scandir($dir); $objects = scandir($dir);
foreach ($objects as $object) { foreach ($objects as $object) {
if ($object != "." && $object != "..") { if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); if (filetype($dir . "/" . $object) == "dir") rrmdir($dir . "/" . $object);
else unlink($dir . "/" . $object);
} }
} }
reset($objects); reset($objects);

View File

@ -30,3 +30,69 @@ const AUTOLOAD_DIRS = [
__DIR__ . "/../classes/MDErrorReporter/exceptions/updates", __DIR__ . "/../classes/MDErrorReporter/exceptions/updates",
__DIR__ . "/../classes/MDTlLoader/exceptions", __DIR__ . "/../classes/MDTlLoader/exceptions",
]; ];
const AUTOLOAD_CLASS_MAP = [
'CsvxmlAvailableFields' => __DIR__ . '/../classes/CsvxmlAvailableFields.php',
'MDTlLoader' => __DIR__ . '/../classes/MDTlLoader/src/MDTlLoader.php',
'MD_JAIL' => __DIR__ . '/../classes/MD_STD/MD_JAIL.php',
'MD_STD' => __DIR__ . '/../classes/MD_STD/MD_STD.php',
'MD_STD_CACHE' => __DIR__ . '/../classes/MD_STD/MD_STD_CACHE.php',
'MD_STD_IN' => __DIR__ . '/../classes/MD_STD/MD_STD_IN.php',
'MD_STD_SEC' => __DIR__ . '/../classes/MD_STD/MD_STD_SEC.php',
'MDAppointmentRecordingSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDAppointmentRecordingSet.php',
'MDConservationReportTypeSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDConservationReportTypeSet.php',
'MDCurrenciesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDCurrenciesSet.php',
'MDEntryTypesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDEntryTypesSet.php',
'MDEventsSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDEventsSet.php',
'MDLanguagesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDLanguagesSet.php',
'MDLicensesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDLicensesSet.php',
'MDMarkingTypesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDMarkingTypesSet.php',
'MDObjectPositionsSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDObjectPositionsSet.php',
'MDObjectPublicationBackgrounds' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDObjectPublicationBackgrounds.php',
'MDPlacetypesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDPlacetypesSet.php',
'MDPodcastContributorsSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDPodcastContributorsSet.php',
'MDSourceTypeSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDSourceTypeSet.php',
'MDTitleTypesSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDTitleTypesSet.php',
'MDUnitsSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDUnitsSet.php',
'MDValueSet' => __DIR__ . '/../classes/MDAllowedValueSets/src/MDValueSet.php',
'MDErrorReporter' => __DIR__ . '/../classes/MDErrorReporter/MDErrorReporter.php',
'MDMailFormat' => __DIR__ . '/../classes/MDMailer/src/MDMailFormat.php',
'MDMailerHelper' => __DIR__ . '/../classes/MDMailer/src/MDMailerHelper.php',
'MD_CONF_EMAIL' => __DIR__ . '/../conf/MD_CONF_EMAIL.php',
'MDAccessDeniedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDAccessDeniedException.php',
'MDConfigCannotBeLoadedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDConfigCannotBeLoadedException.php',
'MDExpectedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDExpectedException.php',
'MDFileDoesNotExist' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDFileDoesNotExist.php',
'MDFileIsNotReadable' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDFileIsNotReadable.php',
'MDInaccessiblePropertyException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDInaccessiblePropertyException.php',
'MDInvalidColorCode' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDInvalidColorCode.php',
'MDInvalidEmail' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDInvalidEmail.php',
'MDInvalidUrl' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDInvalidUrl.php',
'MDOutputBufferNotStarted' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDOutputBufferNotStarted.php',
'MDRequiredConfigNotSet' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDRequiredConfigNotSet.php',
'MDWrongCsrfTokenException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDWrongCsrfTokenException.php',
'MDgenericInvalidInputsException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDgenericInvalidInputsException.php',
'MDhttpFailedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDhttpFailedException.php',
'MDmainEntityNotExistentException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDmainEntityNotExistentException.php',
'MDpageParameterNotFromListException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/generic/MDpageParameterNotFromListException.php',
'MDPageNotInAggregatedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/page/MDPageNotInAggregatedException.php',
'MDmainEntityNotPublicException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/page/MDmainEntityNotPublicException.php',
'MDpageParameterMissingException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/page/MDpageParameterMissingException.php',
'MDpageParameterNotNumericException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/page/MDpageParameterNotNumericException.php',
'MDDuplicateLinkException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDDuplicateLinkException.php',
'MDFileUploadNotAcceptedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDFileUploadNotAcceptedException.php',
'MDInvalidCurrency' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidCurrency.php',
'MDInvalidInputDate' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidInputDate.php',
'MDInvalidLengthUnit' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidLengthUnit.php',
'MDInvalidLicense' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidLicense.php',
'MDInvalidObjectPublicationBackground' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidObjectPublicationBackground.php',
'MDInvalidWeightUnit' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDInvalidWeightUnit.php',
'MDNoUpdateVarSetException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDNoUpdateVarSetException.php',
'MDTooManyFilesUploadException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDTooManyFilesUploadException.php',
'MDWrongFileType' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDWrongFileType.php',
'MDcouldNotSaveException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDcouldNotSaveException.php',
'MDcouldNotSetPublic' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDcouldNotSetPublic.php',
'MDwriteAccessDeniedException' => __DIR__ . '/../classes/MDErrorReporter/exceptions/updates/MDwriteAccessDeniedException.php',
'MDMissingFileException' => __DIR__ . '/../classes/MDTlLoader/exceptions/MDMissingFileException.php',
'MDMissingTranslationException' => __DIR__ . '/../classes/MDTlLoader/exceptions/MDMissingTranslationException.php',
];

39
scripts/buildClassMap.php Normal file
View File

@ -0,0 +1,39 @@
<?PHP
/**
* Generates a class map, based on a constant AUTOLOAD_DIRS.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
require_once __DIR__ . '/../inc/constants.php';
$classesToAutoload = [];
// Load list of classes
foreach (AUTOLOAD_DIRS as $dir) {
$dir = str_replace(realpath(__DIR__ . "/../") . "/inc/../", "", $dir);
foreach (glob($dir . "/*.php") as $file) {
$classesToAutoload[pathinfo(__DIR__ . '/../' . $file, PATHINFO_FILENAME)] = $file;
}
}
// Output
$output = [];
foreach ($classesToAutoload as $key => $value) {
$output[] = ' \'' . $key . '\' => __DIR__ . \'/../' . $value . '\',';
}
$toWrite = '<?PHP
const AUTOLOAD_CLASS_MAP = [
' . implode(PHP_EOL, $output) . '
];';
file_put_contents("tmp-class-map.php", $toWrite);