[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * OAuth service factory. 5 * 6 * PHP version 5.4 7 * 8 * @category OAuth 9 * @author David Desberg <david@daviddesberg.com> 10 * @author Pieter Hordijk <info@pieterhordijk.com> 11 * @copyright Copyright (c) 2013 The authors 12 * @license http://www.opensource.org/licenses/mit-license.html MIT License 13 */ 14 15 namespace OAuth; 16 17 use OAuth\Common\Service\ServiceInterface; 18 use OAuth\Common\Consumer\CredentialsInterface; 19 use OAuth\Common\Storage\TokenStorageInterface; 20 use OAuth\Common\Http\Client\ClientInterface; 21 use OAuth\Common\Http\Client\StreamClient; 22 use OAuth\Common\Http\Uri\UriInterface; 23 use OAuth\Common\Exception\Exception; 24 use OAuth\OAuth1\Signature\Signature; 25 26 class ServiceFactory 27 { 28 /** 29 *@var ClientInterface 30 */ 31 protected $httpClient; 32 33 /** 34 * @var array 35 */ 36 protected $serviceClassMap = array( 37 'OAuth1' => array(), 38 'OAuth2' => array() 39 ); 40 41 /** 42 * @var array 43 */ 44 protected $serviceBuilders = array( 45 'OAuth2' => 'buildV2Service', 46 'OAuth1' => 'buildV1Service', 47 ); 48 49 /** 50 * @param ClientInterface $httpClient 51 * 52 * @return ServiceFactory 53 */ 54 public function setHttpClient(ClientInterface $httpClient) 55 { 56 $this->httpClient = $httpClient; 57 58 return $this; 59 } 60 61 /** 62 * Register a custom service to classname mapping. 63 * 64 * @param string $serviceName Name of the service 65 * @param string $className Class to instantiate 66 * 67 * @return ServiceFactory 68 * 69 * @throws Exception If the class is nonexistent or does not implement a valid ServiceInterface 70 */ 71 public function registerService($serviceName, $className) 72 { 73 if (!class_exists($className)) { 74 throw new Exception(sprintf('Service class %s does not exist.', $className)); 75 } 76 77 $reflClass = new \ReflectionClass($className); 78 79 foreach (array('OAuth2', 'OAuth1') as $version) { 80 if ($reflClass->implementsInterface('OAuth\\' . $version . '\\Service\\ServiceInterface')) { 81 $this->serviceClassMap[$version][ucfirst($serviceName)] = $className; 82 83 return $this; 84 } 85 } 86 87 throw new Exception(sprintf('Service class %s must implement ServiceInterface.', $className)); 88 } 89 90 /** 91 * Builds and returns oauth services 92 * 93 * It will first try to build an OAuth2 service and if none found it will try to build an OAuth1 service 94 * 95 * @param string $serviceName Name of service to create 96 * @param Credentials $credentials 97 * @param TokenStorageInterface $storage 98 * @param array|null $scopes If creating an oauth2 service, array of scopes 99 * @param UriInterface|null $baseApiUri 100 * 101 * @return ServiceInterface 102 * 103 * @throws Exception 104 */ 105 public function createService( 106 $serviceName, 107 CredentialsInterface $credentials, 108 TokenStorageInterface $storage, 109 $scopes = array(), 110 UriInterface $baseApiUri = null 111 ) { 112 if (!$this->httpClient) { 113 // for backwards compatibility. 114 $this->httpClient = new StreamClient(); 115 } 116 117 foreach ($this->serviceBuilders as $version => $buildMethod) { 118 $fullyQualifiedServiceName = $this->getFullyQualifiedServiceName($serviceName, $version); 119 120 if (class_exists($fullyQualifiedServiceName)) { 121 return $this->$buildMethod($fullyQualifiedServiceName, $credentials, $storage, $scopes, $baseApiUri); 122 } 123 } 124 125 return null; 126 } 127 128 /** 129 * Gets the fully qualified name of the service 130 * 131 * @param string $serviceName The name of the service of which to get the fully qualified name 132 * @param string $type The type of the service to get (either OAuth1 or OAuth2) 133 * 134 * @return string The fully qualified name of the service 135 */ 136 private function getFullyQualifiedServiceName($serviceName, $type) 137 { 138 $serviceName = ucfirst($serviceName); 139 140 if (isset($this->serviceClassMap[$type][$serviceName])) { 141 return $this->serviceClassMap[$type][$serviceName]; 142 } 143 144 return '\\OAuth\\' . $type . '\\Service\\' . $serviceName; 145 } 146 147 /** 148 * Builds v2 services 149 * 150 * @param string $serviceName The fully qualified service name 151 * @param CredentialsInterface $credentials 152 * @param TokenStorageInterface $storage 153 * @param array|null $scopes Array of scopes for the service 154 * @param UriInterface|null $baseApiUri 155 * 156 * @return ServiceInterface 157 * 158 * @throws Exception 159 */ 160 private function buildV2Service( 161 $serviceName, 162 CredentialsInterface $credentials, 163 TokenStorageInterface $storage, 164 array $scopes, 165 UriInterface $baseApiUri = null 166 ) { 167 return new $serviceName( 168 $credentials, 169 $this->httpClient, 170 $storage, 171 $this->resolveScopes($serviceName, $scopes), 172 $baseApiUri 173 ); 174 } 175 176 /** 177 * Resolves scopes for v2 services 178 * 179 * @param $string $serviceName The fully qualified service name 180 * @param array $scopes List of scopes for the service 181 * 182 * @return array List of resolved scopes 183 */ 184 private function resolveScopes($serviceName, array $scopes) 185 { 186 $reflClass = new \ReflectionClass($serviceName); 187 $constants = $reflClass->getConstants(); 188 189 $resolvedScopes = array(); 190 foreach ($scopes as $scope) { 191 $key = strtoupper('SCOPE_' . $scope); 192 193 if (array_key_exists($key, $constants)) { 194 $resolvedScopes[] = $constants[$key]; 195 } else { 196 $resolvedScopes[] = $scope; 197 } 198 } 199 200 return $resolvedScopes; 201 } 202 203 /** 204 * Builds v1 services 205 * 206 * @param string $serviceName The fully qualified service name 207 * @param CredentialsInterface $credentials 208 * @param TokenStorageInterface $storage 209 * 210 * @return ServiceInterface 211 * 212 * @throws Exception 213 */ 214 private function buildV1Service( 215 $serviceName, 216 CredentialsInterface $credentials, 217 TokenStorageInterface $storage, 218 $scopes, 219 UriInterface $baseApiUri = null 220 ) { 221 if (!empty($scopes)) { 222 throw new Exception( 223 'Scopes passed to ServiceFactory::createService but an OAuth1 service was requested.' 224 ); 225 } 226 227 return new $serviceName($credentials, $this->httpClient, $storage, new Signature($credentials), $baseApiUri); 228 } 229 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |