1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 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: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 
<?php
/**
 * MvcCore
 *
 * This source file is subject to the BSD 3 License
 * For the full copyright and license information, please view
 * the LICENSE.md file that are distributed with this source code.
 *
 * @copyright   Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
 * @license     https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
 */
namespace MvcCore\Ext;
/**
 * Responsibility - create authentication module by loaded (existing) classes.
 */
class Auth extends \MvcCore\Ext\Auths\Basic {
    /**
     * MvcCore Extension - Auth - version:
     * Comparison by PHP function version_compare();
     * @see http://php.net/manual/en/function.version-compare.php
     */
    const VERSION = '5.0.0';
    /**
     * Full authentication module type with all features.
     */
    const AUTH_CLASS_FULL = '\\MvcCore\\Ext\\Auths\\Full';
    /**
     * Basic authentication module type with signin/signout form, system config user or database user only.
     */
    const AUTH_CLASS_BASIC = '\\MvcCore\\Ext\\Auths\\Basic';
    /**
     * Authentication module type. Possible values: `NULL | "full" | "basic"`.
     * @var string|NULL
     */
    protected static $authType = NULL;
    /**
     * Detected or configured authentication class name.
     * @var string|NULL
     */
    protected static $authClass = NULL;
    /**
     * Return authentication module full class name.
     * @return string|NULL
     */
    public static function GetAuthClass () {
        if (self::$authClass === NULL) {
            if (class_exists(self::AUTH_CLASS_FULL)) {
                self::$authClass = self::AUTH_CLASS_FULL;
            } else {
                self::$authClass = self::AUTH_CLASS_BASIC;
            }
        }
        return self::$authClass;
    }
    /**
     * Set authentication module full class name implementing `\MvcCore\Ext\Auths\IBasic`.
     * @return string|NULL
     */
    public static function SetAuthClass ($authClass) {
        $toolClass = \MvcCore\Application::GetInstance()->GetToolClass();
        if ($toolClass::CheckClassInterface($authClass, 'MvcCore\\Ext\\Auths\\IBasic', TRUE, TRUE)) 
            self::$authClass = $authClass;
    }
    /**
     * Return singleton instance. If instance exists, return existing instance,
     * if not, create new authentication module instance by existing classes,
     * store it and return it. Try to create authentication modules in this order:
     * - `\MvcCore\Ext\Auths\Full` - if class exists
     * - `\MvcCore\Ext\Auths\Basic` - always loaded
     * @param array $configuration Optional configuration passed into method
     *                           `\MvcCore\Ext\Auths\Basic::__construct($configuration)`.
     * @return \MvcCore\Ext\Auths\Full|\MvcCore\Ext\Auths\Basic
     */
    public static function GetInstance ($configuration = []) {
        if (self::$instance === NULL) {
            $authClass = self::GetAuthClass();
            self::$instance = $authClass::GetInstance($configuration);
        }
        return self::$instance;
    }
}