Newer
Older
* This file implements a autoloader for SimpleSAMLphp modules.
Jaime Perez Crespo
committed
* @author Jaime Perez <jaime.perez@uninett.no>, UNINETT
Jaime Perez Crespo
committed
* Autoload function for SimpleSAMLphp modules following PSR-0.
*
* @param string $className Name of the class.
*/
Jaime Perez Crespo
committed
function SimpleSAML_autoload_psr0($className)
{
$modulePrefixLength = strlen('sspmod_');
$classPrefix = substr($className, 0, $modulePrefixLength);
if ($classPrefix !== 'sspmod_') {
return;
}
$modNameEnd = strpos($className, '_', $modulePrefixLength);
$module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
$moduleClass = substr($className, $modNameEnd + 1);
if (!SimpleSAML_Module::isModuleEnabled($module)) {
return;
}
Jaime Perez Crespo
committed
$file = SimpleSAML_Module::getModuleDir($module).'/lib/'.str_replace('_', '/', $moduleClass).'.php';
if (file_exists($file)) {
require_once($file);
}
}
Jaime Perez Crespo
committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Autoload function for SimpleSAMLphp modules following PSR-4.
*
* @param string $className Name of the class.
*/
function SimpleSAML_autoload_psr4($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('SimpleSAML_autoload_psr0');
spl_autoload_register('SimpleSAML_autoload_psr4');