Skip to content
Snippets Groups Projects
Commit 8894ddd1 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Add a PSR-4 autoloader for modules. Now modules can declare their classes...

Add a PSR-4 autoloader for modules. Now modules can declare their classes under the SimpleSAML\Module namespace.
parent d555bb53
No related branches found
No related tags found
No related merge requests found
......@@ -4,15 +4,16 @@
* This file implements a autoloader for SimpleSAMLphp modules.
*
* @author Boy Baukema, SURFnet
* @author Jaime Perez <jaime.perez@uninett.no>, UNINETT
* @package SimpleSAMLphp
*/
/**
* Autoload function for SimpleSAMLphp modules.
* Autoload function for SimpleSAMLphp modules following PSR-0.
*
* @param string $className Name of the class.
*/
function SimpleSAML_autoload($className)
function SimpleSAML_autoload_psr0($className)
{
$modulePrefixLength = strlen('sspmod_');
$classPrefix = substr($className, 0, $modulePrefixLength);
......@@ -28,11 +29,47 @@ function SimpleSAML_autoload($className)
return;
}
$file = SimpleSAML_Module::getModuleDir($module) . '/lib/' . str_replace('_', '/', $moduleClass) . '.php';
$file = SimpleSAML_Module::getModuleDir($module).'/lib/'.str_replace('_', '/', $moduleClass).'.php';
if (file_exists($file)) {
require_once($file);
}
}
spl_autoload_register('SimpleSAML_autoload');
/**
* 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');
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