From ad14f9956f0b798723c78533aa5e4849eb0d9b8b Mon Sep 17 00:00:00 2001 From: lrobert Date: Tue, 14 Jan 2014 11:08:31 -0500 Subject: [PATCH 01/10] Added composer suppport --- autoload.php.dist | 4 +++- composer.json | 31 +++++++++++++++++++++++++++++++ tests/bootstrap.php | 4 +++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 composer.json diff --git a/autoload.php.dist b/autoload.php.dist index 293f4e4..15bf2e7 100644 --- a/autoload.php.dist +++ b/autoload.php.dist @@ -1,5 +1,7 @@ =5.3.3", + "symfony/class-loader": "2.4.*", + "symfony/console" : "2.4.*" + }, + "require-dev" : { + "phpunit/phpunit": "3.7.*" + }, + "autoload" : { + "psr-0": { + "Bronto_" : "src/", + "Bronto_Tests_": "tests/" + } + }, + "target-dir" : "Symfony/Component/Console", + "minimum-stability": "dist" +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fd2535e..a732331 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,7 +3,9 @@ * @copyright 2011-2013 Bronto Software, Inc. * @license http://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0) */ -if (file_exists($file = __DIR__.'/../autoload.php')) { +if (file_exists($file = __DIR__.'/../vendor/autoload.php')) { + require_once $file; +} elseif (file_exists($file = __DIR__.'/../autoload.php')) { require_once $file; } elseif (file_exists($file = __DIR__.'/../autoload.php.dist')) { require_once $file; From 2a22c6af531b5a249c43d7d6bceb0e2d61fae36d Mon Sep 17 00:00:00 2001 From: lrobert Date: Fri, 31 Jan 2014 11:55:56 -0500 Subject: [PATCH 02/10] Updated composer.json to have a correct minimum-stability --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 317e022..3d46c7c 100644 --- a/composer.json +++ b/composer.json @@ -27,5 +27,5 @@ } }, "target-dir" : "Symfony/Component/Console", - "minimum-stability": "dist" + "minimum-stability": "stable" } \ No newline at end of file From 4827094cecdc0f2ccdbb34f91350ba2547c62c55 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Wed, 5 Feb 2014 15:22:14 +0000 Subject: [PATCH 03/10] Adding observer for custom error handling and reusing sessions. --- src/Bronto/Api.php | 67 ++++++++++++++++++++++++++++++++++++--- src/Bronto/Api/Object.php | 2 +- src/Bronto/Observer.php | 35 ++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/Bronto/Observer.php diff --git a/src/Bronto/Api.php b/src/Bronto/Api.php index a777475..813411e 100644 --- a/src/Bronto/Api.php +++ b/src/Bronto/Api.php @@ -37,6 +37,7 @@ class Bronto_Api 'type' => null, 'path' => null, ), + 'observer' => false, // SoapClient 'soap_version' => SOAP_1_1, 'compression' => true, @@ -76,6 +77,11 @@ class Bronto_Api */ protected $_uuid; + /** + * @var Bronto_Observer + */ + protected $_observer; + /** * @param string $token * @param array $options @@ -116,11 +122,20 @@ public function login() // Get a new SoapClient $this->reset(); $client = $this->getSoapClient(false); - $sessionId = $client->login(array('apiToken' => $token))->return; - $client->__setSoapHeaders(array( - new SoapHeader(self::BASE_URL, 'sessionHeader', array('sessionId' => $sessionId)) - )); - $this->_authenticated = true; + // Allow observer to inject a session before login + if ($this->getObserver()) { + $this->getObserver()->onBeforeLogin($this); + } + // Check for auth changes + if (!$this->isAuthenticated()) { + $sessionId = $client->login(array('apiToken' => $token))->return; + $this->setSessionId($sessionId); + + // Allow observer to store session + if ($this->getObserver()) { + $this->getObserver()->onAfterLogin($this, $sessionId); + } + } } catch (Exception $e) { $this->throwException($e); } @@ -128,6 +143,22 @@ public function login() return $this; } + /** + * Resuse an existing session, if possible + * + * @param string $sessionId + * @return Bronto_Api + */ + public function setSessionId($sessionId) + { + $client = $this->getSoapClient(false); + $client->__setSoapHeaders(array( + new SoapHeader(self::BASE_URL, 'sessionHeader', array('sessionId' => $sessionId)) + )); + $this->_authenticated = true; + return $this; + } + /** * We want all Exceptions to be Bronto_Api_Exception for request/response * @@ -162,6 +193,11 @@ public function throwException($exception, $message = null, $code = null) $exception->setResponse($this->getLastResponse()); } + // Allow observer to handle exception cases + if ($this->getObserver()) { + $this->getObserver()->onError($this, $exception); + } + throw $exception; } @@ -516,6 +552,27 @@ public function getRetryer(array $options = array()) return $this->_retryer; } + /** + * Gets the observer for the API client + * + * @return Bronto_Observer + */ + public function getObserver() + { + if (!$this->_observer) { + if (isset($this->_options['observer'])) { + $observer = $this->_options['observer']; + if (is_string($observer) && class_exists($observer)) { + $observer = new $observer(); + } + if ($observer instanceOf Bronto_Observer) { + $this->_observer = $observer; + } + } + } + return $this->_observer; + } + /** * @return Bronto_Util_Retryer_RetryerInterface */ diff --git a/src/Bronto/Api/Object.php b/src/Bronto/Api/Object.php index 7f505db..615beaa 100644 --- a/src/Bronto/Api/Object.php +++ b/src/Bronto/Api/Object.php @@ -350,7 +350,7 @@ public function doRequest($method, array $data, $canUseRetryer = false) return $this->getApi()->throwException($exception); } else { // Attempt to get a new session token - sleep(5); + // sleep(5); $this->getApi()->login(); // If using readDirection, we have to start over if (isset($data['filter']['readDirection'])) { diff --git a/src/Bronto/Observer.php b/src/Bronto/Observer.php new file mode 100644 index 0000000..6e6dcde --- /dev/null +++ b/src/Bronto/Observer.php @@ -0,0 +1,35 @@ + + * @copyright 2011-2014 Bronto Software, Inc. + * @license http://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0) + */ +interface Bronto_Observer +{ + /** + * Observe when the Bronto_Api is about to perform the login + * + * @param Bronto_Api $api + * @return void + */ + public function onBeforeLogin($api); + + /** + * Observe when the Bronto_Api client makes a login call + * + * @param Bronto_Api $api + * @param string $sessionId + * @return void + */ + public function onAfterLogin($api, $sessionId); + + /** + * Observe when the Bronto_Api client throws an exception + * + * @param Bronto_Api $api + * @param string $sessionId + * @return void + */ + public function onError($api, $exception); +} From 5ea2d1e8e8b4078734ed2457b9c2d8c246e23227 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Thu, 6 Feb 2014 18:01:46 +0000 Subject: [PATCH 04/10] Ability to add a custom Retryer. --- src/Bronto/Api.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Bronto/Api.php b/src/Bronto/Api.php index 813411e..0ad4c7f 100644 --- a/src/Bronto/Api.php +++ b/src/Bronto/Api.php @@ -540,6 +540,13 @@ public function getRetryer(array $options = array()) if (!($this->_retryer instanceOf Bronto_Util_Retryer_RetryerInterface)) { $options = array_merge($this->_options['retryer'], $options); switch ($options['type']) { + case 'custom': + if ($options['object']) { + $this->_retryer = $options['object']; + } else { + $this->_retryer = new $options['path']; + } + break; case 'file': $this->_retryer = new Bronto_Util_Retryer_FileRetryer($options); break; From 3c5ac2d52f4854d6b47e94c990f5a52194a468a6 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Fri, 7 Feb 2014 19:58:00 +0000 Subject: [PATCH 05/10] Older versions of php were throwing warnings. --- src/Bronto/SoapClient.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bronto/SoapClient.php b/src/Bronto/SoapClient.php index ac744de..419b0db 100644 --- a/src/Bronto/SoapClient.php +++ b/src/Bronto/SoapClient.php @@ -14,12 +14,12 @@ class Bronto_SoapClient extends SoapClient */ public function __doRequest($request, $location, $action, $version, $one_way = 0) { $result = parent::__doRequest($request, $location, $action, $version); - + // Only replace unicode characters if PCRE version is less than 8.30 - if (version_compare(strstr(constant('PCRE_VERSION'), ' ', true), '8.30', '<')) { + $version = reset(explode(' ', constant('PCRE_VERSION'))); + if (version_compare($version, '8.30', '<')) { $result = preg_replace('/[\x{0}-\x{8}\x{B}-\x{C}\x{E}-\x{1F}\x{D800}-\x{DFFF}]/u', '', $result); } - return $result; } } From adf1c5c5be26eb043d496a0135ddc8a9198f37f2 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 11 Feb 2014 20:06:01 +0000 Subject: [PATCH 06/10] PHP Strict mode was complaining about references. --- src/Bronto/SoapClient.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Bronto/SoapClient.php b/src/Bronto/SoapClient.php index 419b0db..2dc7449 100644 --- a/src/Bronto/SoapClient.php +++ b/src/Bronto/SoapClient.php @@ -16,7 +16,8 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0 $result = parent::__doRequest($request, $location, $action, $version); // Only replace unicode characters if PCRE version is less than 8.30 - $version = reset(explode(' ', constant('PCRE_VERSION'))); + $parts = explode(' ', constant('PCRE_VERSION')); + $version = reset($parts); if (version_compare($version, '8.30', '<')) { $result = preg_replace('/[\x{0}-\x{8}\x{B}-\x{C}\x{E}-\x{1F}\x{D800}-\x{DFFF}]/u', '', $result); } From 8ab6a80b63f20bb84737fbdac94c32186058b314 Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 18 Feb 2014 21:54:17 -0500 Subject: [PATCH 07/10] Update composer.json --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3d46c7c..5e4435e 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,5 @@ "Bronto_Tests_": "tests/" } }, - "target-dir" : "Symfony/Component/Console", "minimum-stability": "stable" -} \ No newline at end of file +} From 78336ccd485371c27bd2fed8d46316b05f17c6ad Mon Sep 17 00:00:00 2001 From: Philip Cali Date: Tue, 18 Feb 2014 22:44:06 -0500 Subject: [PATCH 08/10] Update composer.json Removing Symfony, as it's not required to run the API client. --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 5e4435e..a28f296 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,7 @@ } ], "require" : { - "php" : ">=5.3.3", - "symfony/class-loader": "2.4.*", - "symfony/console" : "2.4.*" + "php" : ">=5.3.3" }, "require-dev" : { "phpunit/phpunit": "3.7.*" From 7f101622a9b80f1bd857ebacf9794b3d08148c03 Mon Sep 17 00:00:00 2001 From: Juraj Seffer Date: Wed, 3 Dec 2014 16:41:33 +0000 Subject: [PATCH 09/10] Fixes exception class name --- src/Bronto/Api/ContentTag/Exception.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bronto/Api/ContentTag/Exception.php b/src/Bronto/Api/ContentTag/Exception.php index fda8080..c28d35e 100644 --- a/src/Bronto/Api/ContentTag/Exception.php +++ b/src/Bronto/Api/ContentTag/Exception.php @@ -4,7 +4,7 @@ * @copyright 2011-2013 Bronto Software, Inc. * @license http://opensource.org/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0) */ -class Bronto_Api_Message_Exception extends Bronto_Api_Exception +class Bronto_Api_ContentTag_Exception extends Bronto_Api_Exception { const INVALID_CONTENTTAG = 1601; // The content tag specified is invalid. const MISSING_NAME = 1602; // You must specify a name. From 4d586563b01abadc513a7939c0b5e31904e24b92 Mon Sep 17 00:00:00 2001 From: Curtis Gibby Date: Fri, 13 Mar 2015 15:38:05 -0600 Subject: [PATCH 10/10] autoload.php.dist - fix ClassLoader The Symfony project has [removed the `UniversalClassLoader` component](https://github.com/symfony/ClassLoader/commit/2ae376a83b03004c8427b414ba4ba1b394179889). The current autoload.php.dist is broken because of this. I had to make the attached edits to get this to work. (I'm generally unfamiliar with autoloading and namespaces, but there doesn't appear to be an equivalent functionality in the `ClassLoader` class to the following chunk of code, so I just took it out. ```php $loader->registerNamespaces(array( 'Console' => BASE_PATH . '/bin', 'Symfony' => BASE_PATH . '/vendor/symfony/src', )); ``` --- autoload.php.dist | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/autoload.php.dist b/autoload.php.dist index 15bf2e7..20f53bf 100644 --- a/autoload.php.dist +++ b/autoload.php.dist @@ -9,26 +9,21 @@ if (file_exists($file = __DIR__.'/functions.php')) { require_once $file; } -require_once BASE_PATH . '/vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; -require_once BASE_PATH . '/vendor/symfony/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php'; +require_once BASE_PATH . '/vendor/symfony/src/Symfony/Component/ClassLoader/ClassLoader.php'; +require_once BASE_PATH . '/vendor/symfony/src/Symfony/Component/ClassLoader/ApcClassLoader.php'; -use Symfony\Component\ClassLoader\UniversalClassLoader; -use Symfony\Component\ClassLoader\ApcUniversalClassLoader; +use Symfony\Component\ClassLoader\ClassLoader; +use Symfony\Component\ClassLoader\ApcClassLoader; if (extension_loaded('apc')) { - $loader = new ApcUniversalClassLoader('bronto.'); + $loader = new ApcClassLoader('bronto.'); } else { - $loader = new UniversalClassLoader(); + $loader = new ClassLoader(); } -$loader->registerNamespaces(array( - 'Console' => BASE_PATH . '/bin', - 'Symfony' => BASE_PATH . '/vendor/symfony/src', -)); - -$loader->registerPrefixes(array( +$loader->addPrefixes(array( 'Bronto_Tests' => BASE_PATH . '/tests', 'Bronto_' => BASE_PATH . '/src', )); -$loader->register(); \ No newline at end of file +$loader->register();