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: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
<?php
namespace Baguette\Mastodon;
use Baguette\Mastodon\Grant;
use Baguette\Mastodon\Service;
use Baguette\Mastodon\Service\Scope;
function session($instance, $client_id, $client_secret, array $options)
{
$scope = null;
$grant = null;
$authorization = null;
$client = new Client($instance);
if (isset($options['scope'])) {
$scope = scope($options['scope']);
}
if (isset($options['credential'])) {
trigger_error('`credential` is obsolete option. Use `grant` instead.', E_USER_DEPRECATED);
$grant = credential($options['credential']);
}
if (isset($options['grant'])) {
$grant = grant($options['grant']);
}
if (isset($options['authorization'])) {
$authorization = authorization($options['authorization']);
}
$auth_factory = new Service\AuthFactory($client, $client_id, $client_secret);
if ($grant !== null) {
$auth_factory->setGrant($grant);
}
$session = new Service\SessionStorage($auth_factory, $scope);
if ($authorization !== null) {
$session->setAuthorization($authorization);
}
return new Mastodon($client, $session);
}
function scope($scope)
{
if (is_array($scope)) {
return new Scope($scope);
} elseif ($scope instanceof Scope) {
return $scope;
}
return new Scope(explode(' ', $scope));
}
function toot($toot_string, array $options = [])
{
return new Service\Toot($toot_string, $options);
}
function credential(array $data)
{
trigger_error('credential() is obsolete function. Use grant() instead.', E_USER_DEPRECATED);
return grant($data);
}
function grant(array $data)
{
if (isset($data['username'], $data['password'])) {
return new Grant\PasswordCredential($data['username'], $data['password']);
}
}
function authorization(array $data)
{
return Service\Authorization::fromObject((object)$data);
}
function http(\GuzzleHttp\ClientInterface $client = null)
{
static $cached_client;
if ($client !== null) {
$cached_client = $client;
} elseif ($cached_client === null) {
$cached_client = new \GuzzleHttp\Client;
}
return $cached_client;
}
function request(Mastodon $service, $method, $path, $options, $class = null)
{
$response = $service->client->requestAPI($method, $path, $options, $service->session);
if ($class === null) {
return $response;
}
return Entity\map($class, \GuzzleHttp\json_decode($response->getBody(), true));
}