Skip to content

Commit

Permalink
refs matomo-org#4074 in case we know it is a plugins or a core class,…
Browse files Browse the repository at this point in the history
… try to load it directly from there and do not look in other folders first. Unfortunately we still need the other logic for backwards compatibility
  • Loading branch information
tsteur committed Nov 10, 2013
1 parent da22f52 commit bc758f2
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions core/Loader.php
Expand Up @@ -60,6 +60,16 @@ protected static function removePrefix($class, $vendorPrefixToRemove)
return $class;
}

private static function isPluginClass($class)
{
return 0 === strpos($class, 'Piwik\Plugins') || 0 === strpos($class, '\Piwik\Plugins');
}

private static function usesPiwikNamespace($class)
{
return 0 === strpos($class, 'Piwik\\') || 0 === strpos($class, '\Piwik\\');
}

/**
* Load class by name
*
Expand All @@ -69,19 +79,27 @@ protected static function removePrefix($class, $vendorPrefixToRemove)
public static function loadClass($class)
{
$classPath = self::getClassFileName($class);

$loaded = false;
if (static::isPluginClass($class)) {
$loaded = static::tryToLoadClass($class, '/plugins/', $classPath);
} else if (static::usesPiwikNamespace($class)) {
$loaded = static::tryToLoadClass($class, '/core/', $classPath);
}

if ($loaded) {
return;
}

if (strpos($class, '\Piwik') === 0
|| strpos($class, 'Piwik') === 0
) {
// Piwik classes are in core/ or plugins/
// only used for files that declare multiple classes (ie Piwik/Live.php declares Piwik\Live\VisitorLog)
do {
// auto-discover class location
foreach (self::$dirs as $dir) {
$path = PIWIK_INCLUDE_PATH . $dir . $classPath . '.php';
if (file_exists($path)) {
require_once $path; // prefixed by PIWIK_INCLUDE_PATH
if (class_exists($class, false) || interface_exists($class, false)) {
return;
}
if (static::tryToLoadClass($class, $dir, $classPath)) {
return;
}
}

Expand All @@ -91,16 +109,23 @@ public static function loadClass($class)
} while (!empty($classPath));
} else {
// non-Piwik classes (e.g., Zend Framework) are in libs/
$path = PIWIK_INCLUDE_PATH . '/libs/' . $classPath . '.php';
if (file_exists($path)) {
require_once $path; // prefixed by PIWIK_INCLUDE_PATH
if (class_exists($class, false) || interface_exists($class, false)) {
return;
}
}
static::tryToLoadClass($class, '/libs/', $classPath);
}
}

private static function tryToLoadClass($class, $dir, $classPath)
{
$path = PIWIK_INCLUDE_PATH . $dir . $classPath . '.php';

if (file_exists($path)) {
require_once $path; // prefixed by PIWIK_INCLUDE_PATH

return class_exists($class, false) || interface_exists($class, false);
}

return false;
}

/**
* Autoloader
*
Expand Down

0 comments on commit bc758f2

Please sign in to comment.