SimpleSAML_Store::getInstance PHP Method

getInstance() public static method

Retrieve our singleton instance.
public static getInstance ( ) : SimpleSAML_Store | false
return SimpleSAML_Store | false The data store, or false if it isn't enabled.
    public static function getInstance()
    {
        if (self::$instance !== null) {
            return self::$instance;
        }
        $config = SimpleSAML_Configuration::getInstance();
        $storeType = $config->getString('store.type', null);
        if ($storeType === null) {
            $storeType = $config->getString('session.handler', 'phpsession');
        }
        switch ($storeType) {
            case 'phpsession':
                // we cannot support advanced features with the PHP session store
                self::$instance = false;
                break;
            case 'memcache':
                self::$instance = new SimpleSAML_Store_Memcache();
                break;
            case 'sql':
                self::$instance = new SimpleSAML_Store_SQL();
                break;
            default:
                // datastore from module
                try {
                    $className = SimpleSAML\Module::resolveClass($storeType, 'Store', 'SimpleSAML_Store');
                } catch (Exception $e) {
                    $c = $config->toArray();
                    $c['store.type'] = 'phpsession';
                    throw new SimpleSAML\Error\CriticalConfigurationError("Invalid 'store.type' configuration option. Cannot find store '{$storeType}'.", null, $c);
                }
                self::$instance = new $className();
        }
        return self::$instance;
    }

Usage Example

function oauth2_hook_cron(&$croninfo)
{
    assert('is_array($croninfo)');
    assert('array_key_exists("summary", $croninfo)');
    assert('array_key_exists("tag", $croninfo)');
    $oauth2config = SimpleSAML_Configuration::getOptionalConfig('module_oauth2.php');
    if (is_null($oauth2config->getValue('cron_tag', 'hourly'))) {
        return;
    }
    if ($oauth2config->getValue('cron_tag', NULL) !== $croninfo['tag']) {
        return;
    }
    try {
        $store = SimpleSAML_Store::getInstance();
        if (!$store instanceof \SimpleSAML\Modules\DBAL\Store\DBAL) {
            throw new \SimpleSAML_Error_Exception('OAuth2 module: Only DBAL Store is supported');
        }
        $accessTokenRepository = new AccessTokenRepository();
        $accessTokenRepository->removeExpiredAccessTokens();
        $authTokenRepository = new AuthCodeRepository();
        $authTokenRepository->removeExpiredAuthCodes();
        $refreshTokenRepository = new RefreshTokenRepository();
        $refreshTokenRepository->removeExpiredRefreshTokens();
        $croninfo['summary'][] = 'OAuth2 clean up. Removed expired entries from OAuth2 storage.';
    } catch (Exception $e) {
        $message = 'OAuth2 clean up cron script failed: ' . $e->getMessage();
        SimpleSAML\Logger::warning($message);
        $croninfo['summary'][] = $message;
    }
}
All Usage Examples Of SimpleSAML_Store::getInstance