Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Module.php 9.80 KiB
<?php
namespace SimpleSAML;

/**
 * Helper class for accessing information about modules.
 *
 * @author Olav Morken <olav.morken@uninett.no>, UNINETT AS.
 * @author Boy Baukema, SURFnet.
 * @author Jaime Perez <jaime.perez@uninett.no>, UNINETT AS.
 * @package SimpleSAMLphp
 */
class Module
{

    /**
     * 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);