Skip to content
This repository has been archived by the owner on Mar 7, 2019. It is now read-only.

autoload.php.dist - fix ClassLoader #12

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
25 changes: 11 additions & 14 deletions autoload.php.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?php

/*
* Use vendor/autoload.php if you are using composer!
*/
define('DS', DIRECTORY_SEPARATOR);
define('BASE_PATH', dirname(__FILE__));

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();
$loader->register();
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name" : "bronto/bronto-api-php-client",
"type" : "library",
"description" : "Client Library for PHP",
"keywords" : ["bronto"],
"homepage" : "http://bronto.com",
"license" : "OSL-3.0",
"authors" : [
{
"name" : "Bronto Professional Services Engineering team",
"homepage": "http://bronto.com",
"email" : "[email protected]"
}
],
"require" : {
"php" : ">=5.3.3"
},
"require-dev" : {
"phpunit/phpunit": "3.7.*"
},
"autoload" : {
"psr-0": {
"Bronto_" : "src/",
"Bronto_Tests_": "tests/"
}
},
"minimum-stability": "stable"
}
74 changes: 69 additions & 5 deletions src/Bronto/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Bronto_Api
'type' => null,
'path' => null,
),
'observer' => false,
// SoapClient
'soap_version' => SOAP_1_1,
'compression' => true,
Expand Down Expand Up @@ -76,6 +77,11 @@ class Bronto_Api
*/
protected $_uuid;

/**
* @var Bronto_Observer
*/
protected $_observer;

/**
* @param string $token
* @param array $options
Expand Down Expand Up @@ -116,18 +122,43 @@ 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);
}

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
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -504,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;
Expand All @@ -516,6 +559,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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Bronto/Api/ContentTag/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Bronto/Api/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
35 changes: 35 additions & 0 deletions src/Bronto/Observer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @author Philip Cali <[email protected]>
* @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);
}
7 changes: 4 additions & 3 deletions src/Bronto/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ 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', '<')) {
$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);
}

return $result;
}
}
4 changes: 3 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down