Skip to content
Snippets Groups Projects
Unverified Commit 1408e1f9 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Move the PSR* autoload functions outside of SimpleSAML\Module.

Doing so allows us to mock the class. Otherwise, the _autoload_module.php is always called first, and when it tries to register the functions from that class, it automatically autoloads it, making it impossible to mock it afterwards.
parent d9379234
No related branches found
No related tags found
No related merge requests found
......@@ -27,89 +27,6 @@ class Module
public static $module_info = array();
/**
* Autoload function for SimpleSAMLphp modules following PSR-0.
*
* @param string $className Name of the class.
*
* @deprecated This method will be removed in SSP 2.0.
*
* TODO: this autoloader should be removed once everything has been migrated to namespaces.
*/
public static function autoloadPSR0($className)
{
$modulePrefixLength = strlen('sspmod_');
$classPrefix = substr($className, 0, $modulePrefixLength);
if ($classPrefix !== 'sspmod_') {
return;
}
$modNameEnd = strpos($className, '_', $modulePrefixLength);
$module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
$path = explode('_', substr($className, $modNameEnd + 1));
if (!self::isModuleEnabled($module)) {
return;
}
$file = self::getModuleDir($module).'/lib/'.join('/', $path).'.php';
if (!file_exists($file)) {
return;
}
require_once($file);
if (!class_exists($className, false) && !interface_exists($className, false)) {
// the file exists, but the class is not defined. Is it using namespaces?
$nspath = join('\\', $path);
if (class_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath) ||
interface_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath)
) {
// the class has been migrated, create an alias and warn about it
\SimpleSAML\Logger::warning(
"The class or interface '$className' is now using namespaces, please use 'SimpleSAML\\Module\\".
$module."\\".$nspath."' instead."
);
class_alias("SimpleSAML\\Module\\$module\\$nspath", $className);
}
}
}
/**
* Autoload function for SimpleSAMLphp modules following PSR-4.
*
* @param string $className Name of the class.
*/
public static function autoloadPSR4($className)
{
$elements = explode('\\', $className);
if ($elements[0] === '') { // class name starting with /, ignore
array_shift($elements);
}
if (count($elements) < 4) {
return; // it can't be a module
}
if (array_shift($elements) !== 'SimpleSAML') {
return; // the first element is not "SimpleSAML"
}
if (array_shift($elements) !== 'Module') {
return; // the second element is not "module"
}
// this is a SimpleSAMLphp module following PSR-4
$module = array_shift($elements);
if (!self::isModuleEnabled($module)) {
return; // module not enabled, avoid giving out any information at all
}
$file = self::getModuleDir($module).'/lib/'.implode('/', $elements).'.php';
if (file_exists($file)) {
require_once($file);
}
}
/**
* Retrieve the base directory for a module.
*
......
......@@ -69,6 +69,89 @@ function temporaryLoader($class)
}
}
/**
* Autoload function for SimpleSAMLphp modules following PSR-0.
*
* @param string $className Name of the class.
*
* @deprecated This method will be removed in SSP 2.0.
*
* TODO: this autoloader should be removed once everything has been migrated to namespaces.
*/
function sspmodAutoloadPSR0($className)
{
$modulePrefixLength = strlen('sspmod_');
$classPrefix = substr($className, 0, $modulePrefixLength);
if ($classPrefix !== 'sspmod_') {
return;
}
$modNameEnd = strpos($className, '_', $modulePrefixLength);
$module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
$path = explode('_', substr($className, $modNameEnd + 1));
if (!\SimpleSAML\Module::isModuleEnabled($module)) {
return;
}
$file = \SimpleSAML\Module::getModuleDir($module).'/lib/'.join('/', $path).'.php';
if (!file_exists($file)) {
return;
}
require_once($file);
if (!class_exists($className, false) && !interface_exists($className, false)) {
// the file exists, but the class is not defined. Is it using namespaces?
$nspath = join('\\', $path);
if (class_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath) ||
interface_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath)
) {
// the class has been migrated, create an alias and warn about it
\SimpleSAML\Logger::warning(
"The class or interface '$className' is now using namespaces, please use 'SimpleSAML\\Module\\".
$module."\\".$nspath."' instead."
);
class_alias("SimpleSAML\\Module\\$module\\$nspath", $className);
}
}
}
/**
* Autoload function for SimpleSAMLphp modules following PSR-4.
*
* @param string $className Name of the class.
*/
function sspmodAutoloadPSR4($className)
{
$elements = explode('\\', $className);
if ($elements[0] === '') { // class name starting with /, ignore
array_shift($elements);
}
if (count($elements) < 4) {
return; // it can't be a module
}
if (array_shift($elements) !== 'SimpleSAML') {
return; // the first element is not "SimpleSAML"
}
if (array_shift($elements) !== 'Module') {
return; // the second element is not "module"
}
// this is a SimpleSAMLphp module following PSR-4
$module = array_shift($elements);
if (!\SimpleSAML\Module::isModuleEnabled($module)) {
return; // module not enabled, avoid giving out any information at all
}
$file = \SimpleSAML\Module::getModuleDir($module).'/lib/'.implode('/', $elements).'.php';
if (file_exists($file)) {
require_once($file);
}
}
spl_autoload_register("temporaryLoader");
spl_autoload_register(array('SimpleSAML\Module', 'autoloadPSR0'));
spl_autoload_register(array('SimpleSAML\Module', 'autoloadPSR4'));
spl_autoload_register('sspmodAutoloadPSR0');
spl_autoload_register('sspmodAutoloadPSR4');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment