|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @filesource oauth-example-common.php |
| 4 | + * @created 09.04.2018 |
| 5 | + * @author smiley <smiley@chillerlan.net> |
| 6 | + * @copyright 2018 smiley |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +use chillerlan\Database\{ |
| 11 | + Database, DatabaseOptionsTrait, Drivers\MySQLiDrv |
| 12 | +}; |
| 13 | +use chillerlan\HTTP\{ |
| 14 | + HTTPClientAbstract, HTTPResponseInterface, TinyCurlClient |
| 15 | +}; |
| 16 | +use chillerlan\Logger\{ |
| 17 | + Log, LogOptions, LogTrait, Output\LogOutputAbstract |
| 18 | +}; |
| 19 | +use chillerlan\OAuth\{ |
| 20 | + OAuthOptions, Storage\SessionTokenStorage |
| 21 | +}; |
| 22 | +use chillerlan\TinyCurl\Request; |
| 23 | +use chillerlan\Traits\{ |
| 24 | + ContainerInterface, DotEnv |
| 25 | +}; |
| 26 | + |
| 27 | +ini_set('date.timezone', 'Europe/Amsterdam'); |
| 28 | + |
| 29 | +/** @var string $ENVVAR */ |
| 30 | +/** @var string $CFGDIR */ |
| 31 | + |
| 32 | +$env = (new DotEnv($CFGDIR, '.env', false))->load(); |
| 33 | + |
| 34 | +$options_arr = [ |
| 35 | + // OAuthOptions |
| 36 | + 'key' => $env->get($ENVVAR.'_KEY'), |
| 37 | + 'secret' => $env->get($ENVVAR.'_SECRET'), |
| 38 | + 'callbackURL' => $env->get($ENVVAR.'_CALLBACK_URL'), |
| 39 | + 'dbUserID' => 1, |
| 40 | + 'dbTokenTable' => 'storagetest', |
| 41 | + 'dbProviderTable' => 'storagetest_providers', |
| 42 | + 'storageCryptoKey' => '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f', |
| 43 | + 'tokenAutoRefresh' => true, |
| 44 | + |
| 45 | + // DatabaseOptions |
| 46 | + 'driver' => MySQLiDrv::class, |
| 47 | + 'host' => $env->MYSQL_HOST, |
| 48 | + 'port' => $env->MYSQL_PORT, |
| 49 | + 'database' => $env->MYSQL_DATABASE, |
| 50 | + 'username' => $env->MYSQL_USERNAME, |
| 51 | + 'password' => $env->MYSQL_PASSWORD, |
| 52 | + |
| 53 | + // HTTPOptions |
| 54 | + 'ca_info' => $CFGDIR.'/cacert.pem', |
| 55 | + 'userAgent' => 'chillerlanPhpOAuth/3.0.0 +https://github.com/codemasher/php-oauth', |
| 56 | +]; |
| 57 | + |
| 58 | +/** @var \chillerlan\Traits\ContainerInterface $options */ |
| 59 | +$options = new class($options_arr) extends OAuthOptions{ |
| 60 | + use DatabaseOptionsTrait; |
| 61 | +}; |
| 62 | + |
| 63 | +/** @var \Psr\Log\LoggerInterface $logger */ |
| 64 | +$logger = (new Log)->addInstance( |
| 65 | + new class (new LogOptions(['minLogLevel' => 'debug'])) extends LogOutputAbstract{ |
| 66 | + |
| 67 | + protected function __log(string $level, string $message, array $context = null):void{ |
| 68 | + echo $message.PHP_EOL; |
| 69 | + |
| 70 | + if(!empty($context)){ |
| 71 | + echo print_r($context, true).PHP_EOL; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + }, |
| 76 | + 'console' |
| 77 | +); |
| 78 | + |
| 79 | +/** @var \chillerlan\HTTP\HTTPClientInterface $http */ |
| 80 | +$http = new class($options) extends HTTPClientAbstract{ |
| 81 | + use LogTrait; |
| 82 | + |
| 83 | + protected $client; |
| 84 | + |
| 85 | + public function __construct(ContainerInterface $options){ |
| 86 | + parent::__construct($options); |
| 87 | + $this->client = new TinyCurlClient($this->options, new Request($this->options)); |
| 88 | + } |
| 89 | + |
| 90 | + public function request(string $url, array $params = null, string $method = null, $body = null, array $headers = null):HTTPResponseInterface{ |
| 91 | + $args = func_get_args(); |
| 92 | + $this->debug('$args', $args); |
| 93 | + |
| 94 | + $response = $this->client->request(...$args); |
| 95 | + $this->debug(print_r($response, true)); |
| 96 | + |
| 97 | + usleep(100000); // flood protection |
| 98 | + return $response; |
| 99 | + } |
| 100 | + |
| 101 | +}; |
| 102 | + |
| 103 | +#$http->setLogger($logger); |
| 104 | + |
| 105 | +/** @var \chillerlan\Database\Database $db */ |
| 106 | +$db = new Database($options); |
| 107 | +#$db->setLogger($logger); |
| 108 | + |
| 109 | +/** @var \chillerlan\OAuth\Storage\TokenStorageInterface $storage */ |
| 110 | +$storage = new SessionTokenStorage($options); //new DBTokenStorage($options, $db); |
| 111 | +#$storage->setLogger($logger); |
0 commit comments