Question

I'm working on a application that will connect to Intuit Quickbooks API trough theirs REST PHP SDK. In basic PHP I have things working without problem since I load files as follow:

require_once('../api/config.php');  // Default V3 PHP SDK (v2.0.1) from IPP
require_once(PATH_SDK_ROOT . 'Core/ServiceContext.php');
require_once(PATH_SDK_ROOT . 'DataService/DataService.php');
require_once(PATH_SDK_ROOT . 'PlatformService/PlatformService.php');
require_once(PATH_SDK_ROOT . 'Utility/Configuration/ConfigurationManager.php');

Now I need to use the libraries in Symfony2 controller from a Bundle and this is where my doubt comes from, how I can achieve this easily? I read a lot of docs link1, Symfony Class Loader Component and some others but even is not clear to me at all. For now I have created this structure in my /vendor folder as image shows:

vendor structure

The config.php file that yours can see there have this code:

/**
 * This file allows custom configuration of paths for XSD2PHP dependencies and
 * POPO classes.  Rarely necessary, but possible.
 *
 * @author Intuit Partner Platform Team
 * @version 1.0
 */

// Determine parent path for SDK
$sdkDir = __DIR__ . DIRECTORY_SEPARATOR;

if (!defined('PATH_SDK_ROOT'))
    define('PATH_SDK_ROOT', $sdkDir);

// Specify POPO class path; typically a direct child of the SDK path
if (!defined('POPO_CLASS_PATH'))
    define('POPO_CLASS_PATH', $sdkDir . 'Data');

// Include XSD2PHP dependencies for marshalling and unmarshalling
use com\mikebevz\xsd2php;
require_once(PATH_SDK_ROOT . 'Dependencies/XSD2PHP/src/com/mikebevz/xsd2php/Php2Xml.php');
require_once(PATH_SDK_ROOT . 'Dependencies/XSD2PHP/src/com/mikebevz/xsd2php/Bind.php');

// Includes all POPO classes; these are the source, dest, or both of the marshalling
set_include_path(get_include_path() . PATH_SEPARATOR . POPO_CLASS_PATH);
foreach (glob(POPO_CLASS_PATH.'/*.php') as $filename)
    require_once($filename);

// Specify the prefix pre-pended to POPO class names.  If you modify this value, you
// also need to rebuild the POPO classes, with the same prefix
if (!defined('PHP_CLASS_PREFIX'))
    define('PHP_CLASS_PREFIX',    'IPP');

And there is where class loading magic is, at least for QBO PHP SDK way, so:

  • How I should deal with this in order to use class as Symfony2 does I mean create objects and so on?
  • Since this is non PSR-0 library, will be Symfony Class Loader Component the solution?
  • Could you give me some ideas or even some code to help me get this working?
Was it helpful?

Solution

Symfony use Composer. So the simplest way to include your SDK is to roughly make it compatible with Composer, one way or another.

This will require a composer.json on the package's root. Create it.

Simplest solution

Put a classmap instruction in your composer.json's autoload section :

"autoload": {
    "classmap": ['src/'],
},

Composer will then recursively scan the src/ directory when generating the autoload files. And so every class will be imported when needed, without changing the namespaces or anything.

The downside is that the library still doesn't follow PSR0, with all risk included.

Laborious solution

PSR0-ify the library by editing each source file to add the right namespace and use instruction on top of each file. It is simple, but laborious given the number of classes in the SDK.

You can then add the autoload section of the package's composer.json, like this :

"autoload": {
    "psr-0": {
        "QBO\\": "src/"
    }
},

Alternative solution

When searching for this API, I found consolibyte/quickbooks-php this on packagist… maybe worth a try.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top