Created
October 30, 2022 11:24
-
-
Save eypsilon/120d9376f42157d894e9045e939df74b to your computer and use it in GitHub Desktop.
Implements Delight\Auth\Auth Library using Decorator pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php declare(strict_types=1); | |
| // use Many\Authentication; | |
| use Delight\Auth\Auth; | |
| use PDO; | |
| use Exception; | |
| use BadMethodCallException; | |
| use InvalidArgumentException; | |
| use function call_user_func_array; | |
| use function func_get_args; | |
| use function method_exists; | |
| /** | |
| * Implements Delight\Auth\Auth Library using Decorator pattern | |
| * | |
| * @link https://github.com/delight-im/PHP-Auth PHP-Auth | |
| * | |
| * @author Engin Ypsilon <engin.ypsilon@gmail.com> | |
| * @license http://www.opensource.org/licenses/mit-license.html MIT License | |
| */ | |
| class AuthDecorator | |
| { | |
| /** | |
| * @var null|Auth Instance | |
| */ | |
| protected static $authInstance; | |
| /** | |
| * Check if Auth is instantiated | |
| * | |
| * @return bool | |
| */ | |
| public static function checkInstance(): bool | |
| { | |
| return self::$authInstance instanceof Auth; | |
| } | |
| /** | |
| * Get Instance, try to instantiate if it doesn't exist, expects PDO as first arg | |
| * | |
| * @return null|Auth | |
| */ | |
| public static function getInstance(): ?Auth | |
| { | |
| if (!static::checkInstance() AND $a = func_get_args()) { | |
| static::setInstance(...$a); | |
| } | |
| return self::$authInstance; | |
| } | |
| /** | |
| * Instantiate Auth-Lib, expects PDO as first arg | |
| * | |
| * @return void | |
| * @throws InvalidArgumentException | |
| */ | |
| public static function setInstance(): void | |
| { | |
| if (!$args = func_get_args() OR (!$args[0] instanceof PDO)) { | |
| throw new InvalidArgumentException('Auth expects PDO connection'); | |
| } | |
| self::$authInstance = new Auth(...$args); | |
| } | |
| /** | |
| * Check if Auth is instantiated or throw Exception | |
| * | |
| * @return void | |
| * @throws Exception | |
| */ | |
| public static function checkInstanceIsAuth(): void | |
| { | |
| if (!static::checkInstance()) { | |
| throw new Exception('Auth is not instantiated'); | |
| } | |
| } | |
| /** | |
| * Use AuthDecorator to __call methods that are defined in Auth-Lib | |
| * | |
| * @param string $name | |
| * @param mixed $args arguments | |
| * @return mixed | |
| * @throws BadMethodCallException | |
| */ | |
| public function __call($name, $args): mixed | |
| { | |
| static::checkInstanceIsAuth(); | |
| if (method_exists(self::$authInstance, $name)) { | |
| return call_user_func_array([self::$authInstance, $name], $args); | |
| } | |
| throw new BadMethodCallException('Method not found'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Set and get instanceof Auth | |
| * | |
| * (default setter) AuthDecorator::setInstance($db) | |
| * | |
| * @var null|Auth | |
| */ | |
| try | |
| { | |
| if ($auth = AuthDecorator::getInstance($db)) | |
| { | |
| print_r([ | |
| $auth->getUserId() => $auth->getEmail(), | |
| ]); | |
| } | |
| } catch(Exception $e) { | |
| print $e->getMessage(); | |
| } | |
| /** | |
| * If a called method doesn't exist in AuthDecorator, tries to call it with Auth | |
| * | |
| * @var AuthDecorator | |
| */ | |
| try | |
| { | |
| if ($deco = new AuthDecorator) | |
| { | |
| print_r([ | |
| $deco->getUserId() => $deco->getEmail(), | |
| ]); | |
| } | |
| } catch(Exception $e) { | |
| print $e->getMessage(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment