Skip to content
Browse files

Converted classes to namespaces, PSR-0 compliant

Converted classes to namespaces, PSR-0 compliant as per @BenMorel patch (pull request #42)

Signed-off-by: Matthew Muscat <matthew@mamis.com.au>
  • Loading branch information...
1 parent bf99924 commit 0d9fbbf68be694d72b664fbc15a71fb0007eec95 @matthew-muscat committed
View
1 .gitignore
@@ -1 +1,2 @@
/coverage/
+.DS_Store
View
5 examples/example.php
@@ -15,7 +15,10 @@
* under the License.
*/
-require '../src/facebook.php';
+use Facebook\Facebook;
+use Facebook\FacebookApiException;
+
+require '../src/Facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
View
5 examples/with_js_sdk.php
@@ -1,6 +1,9 @@
<?php
-require '../src/facebook.php';
+use Facebook\Facebook;
+use Facebook\FacebookApiException;
+
+require '../src/Facebook.php';
$facebook = new Facebook(array(
'appId' => '344617158898614',
View
92 src/base_facebook.php → src/BaseFacebook.php
@@ -15,96 +15,16 @@
* under the License.
*/
+namespace Facebook;
+
if (!function_exists('curl_init')) {
- throw new Exception('Facebook needs the CURL PHP extension.');
+ throw new \Exception('Facebook needs the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
- throw new Exception('Facebook needs the JSON PHP extension.');
+ throw new \Exception('Facebook needs the JSON PHP extension.');
}
-/**
- * Thrown when an API call returns an exception.
- *
- * @author Naitik Shah <naitik@facebook.com>
- */
-class FacebookApiException extends Exception
-{
- /**
- * The result from the API server that represents the exception information.
- */
- protected $result;
-
- /**
- * Make a new API Exception with the given result.
- *
- * @param array $result The result from the API server
- */
- public function __construct($result) {
- $this->result = $result;
-
- $code = isset($result['error_code']) ? $result['error_code'] : 0;
-
- if (isset($result['error_description'])) {
- // OAuth 2.0 Draft 10 style
- $msg = $result['error_description'];
- } else if (isset($result['error']) && is_array($result['error'])) {
- // OAuth 2.0 Draft 00 style
- $msg = $result['error']['message'];
- } else if (isset($result['error_msg'])) {
- // Rest server style
- $msg = $result['error_msg'];
- } else {
- $msg = 'Unknown Error. Check getResult()';
- }
-
- parent::__construct($msg, $code);
- }
-
- /**
- * Return the associated result object returned by the API server.
- *
- * @return array The result from the API server
- */
- public function getResult() {
- return $this->result;
- }
-
- /**
- * Returns the associated type for the error. This will default to
- * 'Exception' when a type is not available.
- *
- * @return string
- */
- public function getType() {
- if (isset($this->result['error'])) {
- $error = $this->result['error'];
- if (is_string($error)) {
- // OAuth 2.0 Draft 10 style
- return $error;
- } else if (is_array($error)) {
- // OAuth 2.0 Draft 00 style
- if (isset($error['type'])) {
- return $error['type'];
- }
- }
- }
-
- return 'Exception';
- }
-
- /**
- * To make debugging easier.
- *
- * @return string The string representation of the error
- */
- public function __toString() {
- $str = $this->getType() . ': ';
- if ($this->code != 0) {
- $str .= $this->code . ': ';
- }
- return $str . $this->message;
- }
-}
+require_once __DIR__ . '/FacebookApiException.php';
/**
* Provides access to the Facebook Platform. This class provides
@@ -1026,7 +946,7 @@ protected function parseSignedRequest($signed_request) {
*/
protected function makeSignedRequest($data) {
if (!is_array($data)) {
- throw new InvalidArgumentException(
+ throw new \InvalidArgumentException(
'makeSignedRequest expects an array. Got: ' . print_r($data, true));
}
$data['algorithm'] = self::SIGNED_REQUEST_ALGORITHM;
View
102 src/FacebookApiException.php
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+namespace Facebook;
+
+/**
+ * Thrown when an API call returns an exception.
+ *
+ * @author Naitik Shah <naitik@facebook.com>
+ */
+class FacebookApiException extends \Exception
+{
+ /**
+ * The result from the API server that represents the exception information.
+ */
+ protected $result;
+
+ /**
+ * Make a new API Exception with the given result.
+ *
+ * @param array $result The result from the API server
+ */
+ public function __construct($result) {
+ $this->result = $result;
+
+ $code = isset($result['error_code']) ? $result['error_code'] : 0;
+
+ if (isset($result['error_description'])) {
+ // OAuth 2.0 Draft 10 style
+ $msg = $result['error_description'];
+ } else if (isset($result['error']) && is_array($result['error'])) {
+ // OAuth 2.0 Draft 00 style
+ $msg = $result['error']['message'];
+ } else if (isset($result['error_msg'])) {
+ // Rest server style
+ $msg = $result['error_msg'];
+ } else {
+ $msg = 'Unknown Error. Check getResult()';
+ }
+
+ parent::__construct($msg, $code);
+ }
+
+ /**
+ * Return the associated result object returned by the API server.
+ *
+ * @return array The result from the API server
+ */
+ public function getResult() {
+ return $this->result;
+ }
+
+ /**
+ * Returns the associated type for the error. This will default to
+ * 'Exception' when a type is not available.
+ *
+ * @return string
+ */
+ public function getType() {
+ if (isset($this->result['error'])) {
+ $error = $this->result['error'];
+ if (is_string($error)) {
+ // OAuth 2.0 Draft 10 style
+ return $error;
+ } else if (is_array($error)) {
+ // OAuth 2.0 Draft 00 style
+ if (isset($error['type'])) {
+ return $error['type'];
+ }
+ }
+ }
+
+ return 'Exception';
+ }
+
+ /**
+ * To make debugging easier.
+ *
+ * @return string The string representation of the error
+ */
+ public function __toString() {
+ $str = $this->getType() . ': ';
+ if ($this->code != 0) {
+ $str .= $this->code . ': ';
+ }
+ return $str . $this->message;
+ }
+}
View
4 src/facebook.php
@@ -15,7 +15,9 @@
* under the License.
*/
-require_once "base_facebook.php";
+namespace Facebook;
+
+require_once __DIR__ . '/BaseFacebook.php';
/**
* Extends the BaseFacebook class with the intent of using
View
3 tests/bootstrap.php
@@ -1,5 +1,4 @@
<?php
$base = realpath(dirname(__FILE__) . '/..');
-require "$base/src/base_facebook.php";
-require "$base/src/facebook.php";
+require "$base/src/Facebook.php";
View
6 tests/tests.php
@@ -15,6 +15,10 @@
* under the License.
*/
+use Facebook\Facebook;
+use Facebook\BaseFacebook;
+use Facebook\FacebookApiException;
+
class PHPSDKTestCase extends PHPUnit_Framework_TestCase {
const APP_ID = '117743971608120';
const SECRET = '9c8ea2071859659bea1246d33a9207cf';
@@ -1414,7 +1418,7 @@ public function testLowercaseAuthRevokeAuthDestroysSession() {
}
/**
- * @expectedException FacebookAPIException
+ * @expectedException Facebook\FacebookAPIException
*/
public function testErrorCodeFromRestAPIThrowsException() {
$methods_to_stub = array(

0 comments on commit 0d9fbbf

Please sign in to comment.
Something went wrong with that request. Please try again.