Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Version 5 of the Facebook PHP SDK is a complete refactor of version 4. It comes
- Renamed `FacebookHttpable` to `FacebookHttpClientInterface`
- Added `FacebookApp` entity that contains info about the Facebook app
- Updated the API for the helpers
- Added `HttpClients`, `PersistentData` and `PseudoRandomString` factories to reduce main class' complexity
- Tests
- Added namespaces to the tests
- Grouped functional tests under `functional` group
Expand Down
117 changes: 40 additions & 77 deletions src/Facebook/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@
use Facebook\GraphNodes\GraphEdge;
use Facebook\Url\UrlDetectionInterface;
use Facebook\Url\FacebookUrlDetectionHandler;
use Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory;
use Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface;
use Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator;
use Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator;
use Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator;
use Facebook\HttpClients\FacebookHttpClientInterface;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookStreamHttpClient;
use Facebook\HttpClients\FacebookGuzzleHttpClient;
use Facebook\HttpClients\HttpClientsFactory;
use Facebook\PersistentData\PersistentDataFactory;
use Facebook\PersistentData\PersistentDataInterface;
use Facebook\PersistentData\FacebookSessionPersistentDataHandler;
use Facebook\PersistentData\FacebookMemoryPersistentDataHandler;
use Facebook\Helpers\FacebookCanvasHelper;
use Facebook\Helpers\FacebookJavaScriptHelper;
use Facebook\Helpers\FacebookPageTabHelper;
Expand Down Expand Up @@ -128,80 +122,43 @@ class Facebook
*/
public function __construct(array $config = [])
{
$appId = isset($config['app_id']) ? $config['app_id'] : getenv(static::APP_ID_ENV_NAME);
if (!$appId) {
$config = array_merge([
'app_id' => getenv(static::APP_ID_ENV_NAME),
'app_secret' => getenv(static::APP_SECRET_ENV_NAME),
'default_graph_version' => static::DEFAULT_GRAPH_VERSION,
'enable_beta_mode' => false,
'http_client_handler' => null,
'persistent_data_handler' => null,
'pseudo_random_string_generator' => null,
'url_detection_handler' => null,
], $config);

if (!$config['app_id']) {
throw new FacebookSDKException('Required "app_id" key not supplied in config and could not find fallback environment variable "' . static::APP_ID_ENV_NAME . '"');
}

$appSecret = isset($config['app_secret']) ? $config['app_secret'] : getenv(static::APP_SECRET_ENV_NAME);
if (!$appSecret) {
if (!$config['app_secret']) {
throw new FacebookSDKException('Required "app_secret" key not supplied in config and could not find fallback environment variable "' . static::APP_SECRET_ENV_NAME . '"');
}

$this->app = new FacebookApp($appId, $appSecret);

$httpClientHandler = null;
if (isset($config['http_client_handler'])) {
if ($config['http_client_handler'] instanceof FacebookHttpClientInterface) {
$httpClientHandler = $config['http_client_handler'];
} elseif ($config['http_client_handler'] === 'curl') {
$httpClientHandler = new FacebookCurlHttpClient();
} elseif ($config['http_client_handler'] === 'stream') {
$httpClientHandler = new FacebookStreamHttpClient();
} elseif ($config['http_client_handler'] === 'guzzle') {
$httpClientHandler = new FacebookGuzzleHttpClient();
} else {
throw new \InvalidArgumentException('The http_client_handler must be set to "curl", "stream", "guzzle", or be an instance of Facebook\HttpClients\FacebookHttpClientInterface');
}
}

$enableBeta = isset($config['enable_beta_mode']) && $config['enable_beta_mode'] === true;
$this->client = new FacebookClient($httpClientHandler, $enableBeta);

if (isset($config['url_detection_handler'])) {
if ($config['url_detection_handler'] instanceof UrlDetectionInterface) {
$this->urlDetectionHandler = $config['url_detection_handler'];
} else {
throw new \InvalidArgumentException('The url_detection_handler must be an instance of Facebook\Url\UrlDetectionInterface');
}
}

if (isset($config['pseudo_random_string_generator'])) {
if ($config['pseudo_random_string_generator'] instanceof PseudoRandomStringGeneratorInterface) {
$this->pseudoRandomStringGenerator = $config['pseudo_random_string_generator'];
} elseif ($config['pseudo_random_string_generator'] === 'mcrypt') {
$this->pseudoRandomStringGenerator = new McryptPseudoRandomStringGenerator();
} elseif ($config['pseudo_random_string_generator'] === 'openssl') {
$this->pseudoRandomStringGenerator = new OpenSslPseudoRandomStringGenerator();
} elseif ($config['pseudo_random_string_generator'] === 'urandom') {
$this->pseudoRandomStringGenerator = new UrandomPseudoRandomStringGenerator();
} else {
throw new \InvalidArgumentException('The pseudo_random_string_generator must be set to "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface');
}
}

if (isset($config['persistent_data_handler'])) {
if ($config['persistent_data_handler'] instanceof PersistentDataInterface) {
$this->persistentDataHandler = $config['persistent_data_handler'];
} elseif ($config['persistent_data_handler'] === 'session') {
$this->persistentDataHandler = new FacebookSessionPersistentDataHandler();
} elseif ($config['persistent_data_handler'] === 'memory') {
$this->persistentDataHandler = new FacebookMemoryPersistentDataHandler();
} else {
throw new \InvalidArgumentException('The persistent_data_handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface');
}
}
$this->app = new FacebookApp($config['app_id'], $config['app_secret']);
$this->client = new FacebookClient(
HttpClientsFactory::createHttpClient($config['http_client_handler']),
$config['enable_beta_mode']
);
$this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator(
$config['pseudo_random_string_generator']
);
$this->setUrlDetectionHandler($config['url_detection_handler'] ?: new FacebookUrlDetectionHandler());
$this->persistentDataHandler = PersistentDataFactory::createPersistentDataHandler(
$config['persistent_data_handler']
);

if (isset($config['default_access_token'])) {
$this->setDefaultAccessToken($config['default_access_token']);
}

if (isset($config['default_graph_version'])) {
$this->defaultGraphVersion = $config['default_graph_version'];
} else {
// @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
$this->defaultGraphVersion = static::DEFAULT_GRAPH_VERSION;
}
// @todo v6: Throw an InvalidArgumentException if "default_graph_version" is not set
$this->defaultGraphVersion = $config['default_graph_version'];
}

/**
Expand Down Expand Up @@ -257,13 +214,19 @@ public function getLastResponse()
*/
public function getUrlDetectionHandler()
{
if (!$this->urlDetectionHandler instanceof UrlDetectionInterface) {
$this->urlDetectionHandler = new FacebookUrlDetectionHandler();
}

return $this->urlDetectionHandler;
}

/**
* Changes the URL detection handler.
*
* @param UrlDetectionInterface $urlDetectionHandler
*/
private function setUrlDetectionHandler(UrlDetectionInterface $urlDetectionHandler)
{
$this->urlDetectionHandler = $urlDetectionHandler;
}

/**
* Returns the default AccessToken entity.
*
Expand Down
98 changes: 98 additions & 0 deletions src/Facebook/HttpClients/HttpClientsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright 2014 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\HttpClients;

use GuzzleHttp\Client;
use InvalidArgumentException;

class HttpClientsFactory
{
private function __construct()
{
// a factory constructor should never be invoked
}

/**
* HTTP client generation.
*
* @param FacebookHttpClientInterface|Client|string|null $handler
*
* @throws Exception If the cURL extension or the Guzzle client aren't available (if required).
* @throws InvalidArgumentException If the http client handler isn't "curl", "stream", "guzzle", or an instance of Facebook\HttpClients\FacebookHttpClientInterface.
*
* @return FacebookHttpClientInterface
*/
public static function createHttpClient($handler)
{
if (!$handler) {
return self::detectDefaultClient();
}

if ($handler instanceof FacebookHttpClientInterface) {
return $handler;
}

if ('stream' === $handler) {
return new FacebookStreamHttpClient();
}
if ('curl' === $handler) {
if (!extension_loaded('curl')) {
throw new Exception('The cURL extension must be loaded in order to use the "curl" handler.');
}

return new FacebookCurlHttpClient();
}

if ('guzzle' === $handler && !class_exists('GuzzleHttp\Client')) {
throw new Exception('The Guzzle HTTP client must be included in order to use the "guzzle" handler.');
}

if ($handler instanceof Client) {
return new FacebookGuzzleHttpClient($handler);
}
if ('guzzle' === $handler) {
return new FacebookGuzzleHttpClient();
}

throw new InvalidArgumentException('The http client handler must be set to "curl", "stream", "guzzle", be an instance of GuzzleHttp\Client or an instance of Facebook\HttpClients\FacebookHttpClientInterface');
}

/**
* Detect default HTTP client.
*
* @return FacebookHttpClientInterface
*/
private static function detectDefaultClient()
{
if (extension_loaded('curl')) {
return new FacebookCurlHttpClient();
}

if (class_exists('GuzzleHttp\Client')) {
return new FacebookGuzzleHttpClient();
}

return new FacebookStreamHttpClient();
}
}
65 changes: 65 additions & 0 deletions src/Facebook/PersistentData/PersistentDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright 2014 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\PersistentData;

use InvalidArgumentException;

class PersistentDataFactory
{
private function __construct()
{
// a factory constructor should never be invoked
}

/**
* PersistentData generation.
*
* @param PersistentDataInterface|string|null $handler
*
* @throws InvalidArgumentException If the persistent data handler isn't "session", "memory", or an instance of Facebook\PersistentData\PersistentDataInterface.
*
* @return PersistentDataInterface
*/
public static function createPersistentDataHandler($handler)
{
if (!$handler) {
return session_status() === PHP_SESSION_ACTIVE
? new FacebookSessionPersistentDataHandler()
: new FacebookMemoryPersistentDataHandler();
}

if ($handler instanceof PersistentDataInterface) {
return $handler;
}

if ('session' === $handler) {
new FacebookSessionPersistentDataHandler();
}
if ('memory' === $handler) {
return new FacebookMemoryPersistentDataHandler();
}

throw new InvalidArgumentException('The persistent data handler must be set to "session", "memory", or be an instance of Facebook\PersistentData\PersistentDataInterface');
}
}
Loading