Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Loader.php #5

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,17 @@ public function __construct(

/** The specified vendor directory doesn't exist or isn't readable. */
if (!is_dir($VendorPath) || !is_readable($VendorPath)) {
if (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_NAME'])) {
/** Safeguard for symlinked installations. */
$VendorPath = $this->buildPath(dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME']) . DIRECTORY_SEPARATOR . 'vendor', false);

/** Eep.. Still not working. Generate exception. */
if ($VendorPath === '' || !is_dir($VendorPath) || !is_readable($VendorPath)) {
throw new \Exception('Vendor directory is undefined or unreadable.');
}
} else {
if (!isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_NAME'])) {
/** Further safeguards not possible. Generate exception. */
throw new \Exception('Vendor directory is undefined or unreadable.');
}


/** Safeguard for symlinked installations. */
$VendorPath = $this->buildPath(dirname($_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME']) . DIRECTORY_SEPARATOR . 'vendor', false);

/** Eep.. Still not working. Generate exception. */
if ($VendorPath === '' || !is_dir($VendorPath) || !is_readable($VendorPath)) {
throw new \Exception('Vendor directory is undefined or unreadable.');
}
}
Expand Down Expand Up @@ -288,23 +289,8 @@ public function __construct(
$this->Configuration = parse_ini_file($this->ConfigurationPath, true);

/** Multiline support. */
if (is_array($this->Configuration)) {
foreach ($this->Configuration as $CatKey => &$CatVal) {
if (is_array($CatVal)) {
foreach ($CatVal as $DirKey => &$DirVal) {
if (!is_string($DirVal)) {
continue;
}
$DirVal = str_replace(
["\\\\", '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\e'],
["\\", "\0", "\7", "\8", "\t", "\n", "\x0B", "\x0C", "\r", "\x1B"],
$DirVal
);
}
}
}
unset($DirVal, $DirKey, $CatVal, $CatKey);
}
$this->decodeConfigurations();

} elseif (preg_match('~\.ya?ml$~i', $this->ConfigurationPath)) {
if ($Configuration = $this->readFile($this->ConfigurationPath)) {
$this->YAML->process($Configuration, $this->Configuration);
Expand All @@ -329,7 +315,7 @@ public function __construct(

/** Calculate and build various paths. */
foreach (['CachePath', 'QuarantinePath', 'SignaturesPath'] as $Path) {
if (!$$Path) {
if (!${$Path}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether we need that change. Still executes fine either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more readable i think

if (!$VendorPath) {
continue;
}
Expand Down Expand Up @@ -446,6 +432,30 @@ public function __construct(
});
}

/**
* Method to decode the configurations
* @return void
*/
private function decodeConfigurations() : void
{
if (is_array($this->Configuration)) {
foreach ($this->Configuration as $CatKey => &$CatVal) {
if (is_array($CatVal)) {
foreach ($CatVal as $DirKey => &$DirVal) {
if (!is_string($DirVal)) {
continue;
}
$DirVal = str_replace(
["\\\\", '\0', '\a', '\b', '\t', '\n', '\v', '\f', '\r', '\e'],
["\\", "\0", "\7", "\8", "\t", "\n", "\x0B", "\x0C", "\r", "\x1B"],
$DirVal
);
}
}
}
}
}

/**
* Destruct the loader.
*
Expand Down