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