friendship ended with social-app. php is my new best friend
1<?php 2/** 3 * Class Utilities 4 * 5 * @created 10.04.2024 6 * @author smiley <smiley@chillerlan.net> 7 * @copyright 2024 smiley 8 * @license MIT 9 * 10 * @filesource 11 */ 12declare(strict_types=1); 13 14namespace chillerlan\OAuth\Core; 15 16use chillerlan\Utilities\File; 17use DirectoryIterator; 18use ReflectionClass; 19use function hash; 20use function substr; 21use function trim; 22 23/** 24 * Common utilities for use with the OAuth providers 25 */ 26class Utilities{ 27 28 /** 29 * Fetches a list of provider classes in the given directory 30 * 31 * @return array<string, array<string, string>> 32 */ 33 public static function getProviders(string|null $providerDir = null, string|null $namespace = null):array{ 34 $providerDir = File::realpath(($providerDir ?? __DIR__.'/../Providers')); 35 $namespace = trim(($namespace ?? 'chillerlan\\OAuth\\Providers'), '\\'); 36 $providers = []; 37 38 foreach(new DirectoryIterator($providerDir) as $e){ 39 40 if($e->getExtension() !== 'php'){ 41 continue; 42 } 43 44 $r = new ReflectionClass($namespace.'\\'.substr($e->getFilename(), 0, -4)); 45 46 if(!$r->implementsInterface(OAuthInterface::class) || $r->isAbstract()){ 47 continue; 48 } 49 50 $providers[hash('crc32b', $r->getShortName())] = [ 51 'name' => $r->getShortName(), 52 'fqcn' => $r->getName(), 53 'path' => $e->getRealPath(), 54 ]; 55 56 } 57 58 return $providers; 59 } 60 61}