我正在开发一个应用程序,它将通过他们的 REST PHP SDK 连接到 Intuit Quickbooks API。在基本的 PHP 中,我可以正常工作,因为我按如下方式加载文件:
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');
现在我需要使用 Bundle 中的 Symfony2 Controller 中的库,这就是我的疑问所在,我如何轻松实现这一点?我读了很多文档 link1 , Symfony Class Loader Component和其他一些,但我什至根本不清楚。现在我已经在我的 /vendor
文件夹中创建了这个结构,如图所示:
你在那里可以看到的config.php
文件有这段代码:
/**
* 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');
还有类加载魔法,至少对于 QBO PHP SDK 方式,所以:
- 我应该如何处理这个问题才能像 Symfony2 一样使用类?我是说创建对象等等吗?
- 既然这是非 PSR-0 库,那么 Symfony 类加载器组件将是解决方案吗?
- 你能给我一些想法甚至一些代码来帮助我完成这项工作吗?
最佳答案
Symfony 使用 Composer。因此,包含您的 SDK 的最简单方法是大致使其以某种方式与 Composer 兼容。
这需要在包的根目录下有一个 composer.json
。创建它。
最简单的解决方案
在 composer.json
的 autoload
部分放置一个类映射指令:
"autoload": {
"classmap": ['src/'],
},
Composer 将在生成自动加载文件时递归扫描 src/
目录。因此,每个类都将在需要时导入,而无需更改 namespace 或任何内容。
缺点是库仍然不遵循 PSR0,包括所有风险。
费力的解决方案
通过编辑每个源文件以在每个文件顶部添加正确的 namespace
和 use
指令来 PSR0-ify 库。考虑到 SDK 中的类数量,这很简单,但很费力。
然后您可以添加包的 composer.json
的 autoload
部分,如下所示:
"autoload": {
"psr-0": {
"QBO\\": "src/"
}
},
替代方案
在搜索此 API 时,我找到了 consolibyte/quickbooks-php这个在 packagist 上……也许值得一试。
关于php - 在 Symfony2 项目上使用外部非 PSR-0 库的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041485/