|
32 | 32 | use Facebook\GraphNodes\GraphEdge; |
33 | 33 | use Facebook\Url\UrlDetectionInterface; |
34 | 34 | use Facebook\Url\FacebookUrlDetectionHandler; |
| 35 | +use Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory; |
35 | 36 | use Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface; |
36 | | -use Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator; |
37 | | -use Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator; |
38 | | -use Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator; |
39 | | -use Facebook\HttpClients\FacebookHttpClientInterface; |
40 | | -use Facebook\HttpClients\FacebookCurlHttpClient; |
41 | | -use Facebook\HttpClients\FacebookStreamHttpClient; |
42 | | -use Facebook\HttpClients\FacebookGuzzleHttpClient; |
| 37 | +use Facebook\HttpClients\HttpClientsFactory; |
| 38 | +use Facebook\PersistentData\PersistentDataFactory; |
43 | 39 | use Facebook\PersistentData\PersistentDataInterface; |
44 | | -use Facebook\PersistentData\FacebookSessionPersistentDataHandler; |
45 | | -use Facebook\PersistentData\FacebookMemoryPersistentDataHandler; |
46 | 40 | use Facebook\Helpers\FacebookCanvasHelper; |
47 | 41 | use Facebook\Helpers\FacebookJavaScriptHelper; |
48 | 42 | use Facebook\Helpers\FacebookPageTabHelper; |
@@ -130,80 +124,43 @@ class Facebook |
130 | 124 | */ |
131 | 125 | public function __construct(array $config = []) |
132 | 126 | { |
133 | | - $appId = isset($config['app_id']) ? $config['app_id'] : getenv(static::APP_ID_ENV_NAME); |
134 | | - if (!$appId) { |
| 127 | + $config = array_merge([ |
| 128 | + 'app_id' => getenv(static::APP_ID_ENV_NAME), |
| 129 | + 'app_secret' => getenv(static::APP_SECRET_ENV_NAME), |
| 130 | + 'default_graph_version' => static::DEFAULT_GRAPH_VERSION, |
| 131 | + 'enable_beta_mode' => false, |
| 132 | + 'http_client_handler' => null, |
| 133 | + 'persistent_data_handler' => null, |
| 134 | + 'pseudo_random_string_generator' => null, |
| 135 | + 'url_detection_handler' => null, |
| 136 | + ], $config); |
| 137 | + |
| 138 | + if (!$config['app_id']) { |
135 | 139 | throw new FacebookSDKException('Required "app_id" key not supplied in config and could not find fallback environment variable "' . static::APP_ID_ENV_NAME . '"'); |
136 | 140 | } |
137 | | - |
138 | | - $appSecret = isset($config['app_secret']) ? $config['app_secret'] : getenv(static::APP_SECRET_ENV_NAME); |
139 | | - if (!$appSecret) { |
| 141 | + if (!$config['app_secret']) { |
140 | 142 | throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"'); |
141 | 143 | } |
142 | 144 |
|
143 | | - $this->app = new FacebookApp($appId, $appSecret); |
144 | | - |
145 | | - $httpClientHandler = null; |
146 | | - if (isset($config['http_client_handler'])) { |
147 | | - if ($config['http_client_handler'] instanceof FacebookHttpClientInterface) { |
148 | | - $httpClientHandler = $config['http_client_handler']; |
149 | | - } elseif ($config['http_client_handler'] === 'curl') { |
150 | | - $httpClientHandler = new FacebookCurlHttpClient(); |
151 | | - } elseif ($config['http_client_handler'] === 'stream') { |
152 | | - $httpClientHandler = new FacebookStreamHttpClient(); |
153 | | - } elseif ($config['http_client_handler'] === 'guzzle') { |
154 | | - $httpClientHandler = new FacebookGuzzleHttpClient(); |
155 | | - } else { |
156 | | - throw new \InvalidArgumentException('The http_client_handler must be set to "curl", "stream", "guzzle", or be an instance of Facebook\HttpClients\FacebookHttpClientInterface'); |
157 | | - } |
158 | | - } |
159 | | - |
160 | | - $enableBeta = isset($config['enable_beta_mode']) && $config['enable_beta_mode'] === true; |
161 | | - $this->client = new FacebookClient($httpClientHandler, $enableBeta); |
162 | | - |
163 | | - if (isset($config['url_detection_handler'])) { |
164 | | - if ($config['url_detection_handler'] instanceof UrlDetectionInterface) { |
165 | | - $this->urlDetectionHandler = $config['url_detection_handler']; |
166 | | - } else { |
167 | | - throw new \InvalidArgumentException('The url_detection_handler must be an instance of Facebook\Url\UrlDetectionInterface'); |
168 | | - } |
169 | | - } |
170 | | - |
171 | | - if (isset($config['pseudo_random_string_generator'])) { |
172 | | - if ($config['pseudo_random_string_generator'] instanceof PseudoRandomStringGeneratorInterface) { |
173 | | - $this->pseudoRandomStringGenerator = $config['pseudo_random_string_generator']; |
174 | | - } elseif ($config['pseudo_random_string_generator'] === 'mcrypt') { |
175 | | - $this->pseudoRandomStringGenerator = new McryptPseudoRandomStringGenerator(); |
176 | | - } elseif ($config['pseudo_random_string_generator'] === 'openssl') { |
177 | | - $this->pseudoRandomStringGenerator = new OpenSslPseudoRandomStringGenerator(); |
178 | | - } elseif ($config['pseudo_random_string_generator'] === 'urandom') { |
179 | | - $this->pseudoRandomStringGenerator = new UrandomPseudoRandomStringGenerator(); |
180 | | - } else { |
181 | | - throw new \InvalidArgumentException('The pseudo_random_string_generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface'); |
182 | | - } |
183 | | - } |
184 | | - |
185 | | - if (isset($config['persistent_data_handler'])) { |
186 | | - if ($config['persistent_data_handler'] instanceof PersistentDataInterface) { |
187 | | - $this->persistentDataHandler = $config['persistent_data_handler']; |
188 | | - } elseif ($config['persistent_data_handler'] === 'session') { |
189 | | - $this->persistentDataHandler = new FacebookSessionPersistentDataHandler(); |
190 | | - } elseif ($config['persistent_data_handler'] === 'memory') { |
191 | | - $this->persistentDataHandler = new FacebookMemoryPersistentDataHandler(); |
192 | | - } else { |
193 | | - throw new \InvalidArgumentException('The persistent_data_handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface'); |
194 | | - } |
195 | | - } |
| 145 | + $this->app = new FacebookApp($config['app_id'], $config['app_secret']); |
| 146 | + $this->client = new FacebookClient( |
| 147 | + HttpClientsFactory::createHttpClient($config['http_client_handler']), |
| 148 | + $config['enable_beta_mode'] |
| 149 | + ); |
| 150 | + $this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator( |
| 151 | + $config['pseudo_random_string_generator'] |
| 152 | + ); |
| 153 | + $this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler()); |
| 154 | + $this->persistentDataHandler = PersistentDataFactory::createPersistentDataHandler( |
| 155 | + $config['persistent_data_handler'] |
| 156 | + ); |
196 | 157 |
|
197 | 158 | if (isset($config['default_access_token'])) { |
198 | 159 | $this->setDefaultAccessToken($config['default_access_token']); |
199 | 160 | } |
200 | 161 |
|
201 | | - if (isset($config['default_graph_version'])) { |
202 | | - $this->defaultGraphVersion = $config['default_graph_version']; |
203 | | - } else { |
204 | | - // @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set |
205 | | - $this->defaultGraphVersion = static::DEFAULT_GRAPH_VERSION; |
206 | | - } |
| 162 | + // @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set |
| 163 | + $this->defaultGraphVersion = $config['default_graph_version']; |
207 | 164 | } |
208 | 165 |
|
209 | 166 | /** |
@@ -259,13 +216,19 @@ public function getLastResponse() |
259 | 216 | */ |
260 | 217 | public function getUrlDetectionHandler() |
261 | 218 | { |
262 | | - if (!$this->urlDetectionHandler instanceof UrlDetectionInterface) { |
263 | | - $this->urlDetectionHandler = new FacebookUrlDetectionHandler(); |
264 | | - } |
265 | | - |
266 | 219 | return $this->urlDetectionHandler; |
267 | 220 | } |
268 | 221 |
|
| 222 | + /** |
| 223 | + * Changes the URL detection handler. |
| 224 | + * |
| 225 | + * @param UrlDetectionInterface $urlDetectionHandler |
| 226 | + */ |
| 227 | + private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler) |
| 228 | + { |
| 229 | + $this->urlDetectionHandler = $urlDetectionHandler; |
| 230 | + } |
| 231 | + |
269 | 232 | /** |
270 | 233 | * Returns the default AccessToken entity. |
271 | 234 | * |
|
0 commit comments