Skip to content

Commit

Permalink
Merge pull request #904 from oat-sa/fix/TR-1765/autoload-root-level-e…
Browse files Browse the repository at this point in the history
…xtensions-classes

Fix TR-1765: autoload root-level extensions' classes
  • Loading branch information
wazelin authored Aug 5, 2021
2 parents eebe9a1 + 335e8c9 commit 4727bdd
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions common/legacy/class.LegacyAutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
*/

use oat\oatbox\extension\Manifest;

/**
* the generis autoloader
*
Expand All @@ -42,16 +44,22 @@ private static function singleton()
return self::$singleton;
}

/** @var string[] */
private $legacyPrefixes = [];

/** @var string */
private $generisPath;

/** @var string */
private $rootPath;

/**
* protect the cunstructer, singleton pattern
*/
private function __construct()
{
$this->generisPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR;
$this->rootPath = $this->generisPath . '..' . DIRECTORY_SEPARATOR;
}

/**
Expand Down Expand Up @@ -82,15 +90,15 @@ public static function supportLegacyPrefix($prefix, $namespace)
* @param string pClassName
* @return void
*/
public function autoload($pClassName)
public function autoload($pClassName): void
{
if (strpos($pClassName, '_') === false) {
return;
}

$tokens = explode("_", $pClassName);
$size = count($tokens);
$path = '';
$size = count($tokens);
$path = '';
for ($i = 0; $i < $size - 1; $i++) {
$path .= $tokens[$i] . '/';
}
Expand All @@ -116,13 +124,13 @@ public function autoload($pClassName)
return;
}

if (file_exists($this->generisPath . '..' . DIRECTORY_SEPARATOR . $filePath)) {
require_once $this->generisPath . '..' . DIRECTORY_SEPARATOR . $filePath;
if (file_exists($this->rootPath . $filePath)) {
require_once $this->rootPath . $filePath;
return;
}

if (file_exists($this->generisPath . '..' . DIRECTORY_SEPARATOR . $filePathInterface)) {
require_once $this->generisPath . '..' . DIRECTORY_SEPARATOR . $filePathInterface;
if (file_exists($this->rootPath . $filePathInterface)) {
require_once $this->rootPath . $filePathInterface;
return;
}

Expand All @@ -133,6 +141,36 @@ public function autoload($pClassName)
return;
}
}

$this->loadFromRootExtension($tokens);
}

private function loadFromRootExtension(array $classNameTokens): void
{
$manifestPath = $this->rootPath . 'manifest.php';

if (!file_exists($manifestPath)) {
return;
}

$manifest = new Manifest($manifestPath);
if ($manifest->getName() !== reset($classNameTokens)) {
return;
}

$path = implode(DIRECTORY_SEPARATOR, array_slice($classNameTokens, 1, -1)) . DIRECTORY_SEPARATOR;

$classFilePath = $this->rootPath . $path . 'class.' . end($classNameTokens) . '.php';
if (file_exists($classFilePath)) {
require_once $classFilePath;

return;
}

$interfaceFilePath = $this->rootPath . $path . 'interface.' . end($classNameTokens) . '.php';
if (file_exists($interfaceFilePath)) {
require_once $interfaceFilePath;
}
}

private function wrapClass($legacyClass, $realClass)
Expand Down

0 comments on commit 4727bdd

Please sign in to comment.