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