Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ServiceFactory.php
001 <?php
002
003 /**
004 * OAuth service factory.
005 *
006 * PHP version 5.4
007 *
008 * @author David Desberg <david@daviddesberg.com>
009 * @author Pieter Hordijk <info@pieterhordijk.com>
010 * @license http://www.opensource.org/licenses/mit-license.html MIT License
011 */
012
013 namespace OAuth;
014
015 use OAuth\Common\Consumer\CredentialsInterface;
016 use OAuth\Common\Exception\Exception;
017 use OAuth\Common\Http\Client\ClientInterface;
018 use OAuth\Common\Http\Client\StreamClient;
019 use OAuth\Common\Http\Uri\UriInterface;
020 use OAuth\Common\Service\ServiceInterface;
021 use OAuth\Common\Storage\TokenStorageInterface;
022 use OAuth\OAuth1\Signature\Signature;
023 use ReflectionClass;
024
025 class ServiceFactory
026 {
027 /**
028 *@var ClientInterface
029 */
030 protected $httpClient;
031
032 /**
033 * @var array
034 */
035 protected $serviceClassMap = [
036 'OAuth1' => [],
037 'OAuth2' => [],
038 ];
039
040 /**
041 * @var array
042 */
043 protected $serviceBuilders = [
044 'OAuth2' => 'buildV2Service',
045 'OAuth1' => 'buildV1Service',
046 ];
047
048 /**
049 * @return ServiceFactory
050 */
051 public function setHttpClient(ClientInterface $httpClient)
052 {
053 $this->httpClient = $httpClient;
054
055 return $this;
056 }
057
058 /**
059 * Register a custom service to classname mapping.
060 *
061 * @param string $serviceName Name of the service
062 * @param string $className Class to instantiate
063 *
064 * @return ServiceFactory
065 */
066 public function registerService($serviceName, $className)
067 {
068 if (!class_exists($className)) {
069 throw new Exception(sprintf('Service class %s does not exist.', $className));
070 }
071
072 $reflClass = new ReflectionClass($className);
073
074 foreach (['OAuth2', 'OAuth1'] as $version) {
075 if ($reflClass->implementsInterface('OAuth\\' . $version . '\\Service\\ServiceInterface')) {
076 $this->serviceClassMap[$version][ucfirst($serviceName)] = $className;
077
078 return $this;
079 }
080 }
081
082 throw new Exception(sprintf('Service class %s must implement ServiceInterface.', $className));
083 }
084
085 /**
086 * Builds and returns oauth services.
087 *
088 * It will first try to build an OAuth2 service and if none found it will try to build an OAuth1 service
089 *
090 * @param string $serviceName Name of service to create
091 * @param null|array $scopes If creating an oauth2 service, array of scopes
092 * @param string $apiVersion version of the api call
093 *
094 * @return ServiceInterface
095 */
096 public function createService(
097 $serviceName,
098 CredentialsInterface $credentials,
099 TokenStorageInterface $storage,
100 $scopes = [],
101 ?UriInterface $baseApiUri = null,
102 $apiVersion = ''
103 ) {
104 if (!$this->httpClient) {
105 // for backwards compatibility.
106 $this->httpClient = new StreamClient();
107 }
108
109 foreach ($this->serviceBuilders as $version => $buildMethod) {
110 $fullyQualifiedServiceName = $this->getFullyQualifiedServiceName($serviceName, $version);
111
112 if (class_exists($fullyQualifiedServiceName)) {
113 return $this->$buildMethod(
114 $fullyQualifiedServiceName,
115 $credentials,
116 $storage,
117 $scopes,
118 $baseApiUri,
119 $apiVersion
120 );
121 }
122 }
123
124 return null;
125 }
126
127 /**
128 * Gets the fully qualified name of the service.
129 *
130 * @param string $serviceName The name of the service of which to get the fully qualified name
131 * @param string $type The type of the service to get (either OAuth1 or OAuth2)
132 *
133 * @return string The fully qualified name of the service
134 */
135 private function getFullyQualifiedServiceName($serviceName, $type)
136 {
137 $serviceName = ucfirst($serviceName);
138
139 if (isset($this->serviceClassMap[$type][$serviceName])) {
140 return $this->serviceClassMap[$type][$serviceName];
141 }
142
143 return '\\OAuth\\' . $type . '\\Service\\' . $serviceName;
144 }
145
146 /**
147 * Builds v2 services.
148 *
149 * @param string $serviceName The fully qualified service name
150 * @param null|array $scopes Array of scopes for the service
151 * @param mixed $apiVersion
152 *
153 * @return ServiceInterface
154 */
155 private function buildV2Service(
156 $serviceName,
157 CredentialsInterface $credentials,
158 TokenStorageInterface $storage,
159 array $scopes,
160 ?UriInterface $baseApiUri = null,
161 $apiVersion = ''
162 ) {
163 return new $serviceName(
164 $credentials,
165 $this->httpClient,
166 $storage,
167 $this->resolveScopes($serviceName, $scopes),
168 $baseApiUri,
169 $apiVersion
170 );
171 }
172
173 /**
174 * Resolves scopes for v2 services.
175 *
176 * @param string $serviceName The fully qualified service name
177 * @param array $scopes List of scopes for the service
178 *
179 * @return array List of resolved scopes
180 */
181 private function resolveScopes($serviceName, array $scopes)
182 {
183 $reflClass = new ReflectionClass($serviceName);
184 $constants = $reflClass->getConstants();
185
186 $resolvedScopes = [];
187 foreach ($scopes as $scope) {
188 $key = strtoupper('SCOPE_' . $scope);
189
190 if (array_key_exists($key, $constants)) {
191 $resolvedScopes[] = $constants[$key];
192 } else {
193 $resolvedScopes[] = $scope;
194 }
195 }
196
197 return $resolvedScopes;
198 }
199
200 /**
201 * Builds v1 services.
202 *
203 * @param string $serviceName The fully qualified service name
204 * @param array $scopes
205 * @param UriInterface $baseApiUri
206 *
207 * @return ServiceInterface
208 */
209 private function buildV1Service(
210 $serviceName,
211 CredentialsInterface $credentials,
212 TokenStorageInterface $storage,
213 $scopes,
214 ?UriInterface $baseApiUri = null
215 ) {
216 if (!empty($scopes)) {
217 throw new Exception(
218 'Scopes passed to ServiceFactory::createService but an OAuth1 service was requested.'
219 );
220 }
221
222 return new $serviceName($credentials, $this->httpClient, $storage, new Signature($credentials), $baseApiUri);
223 }
224 }
225