lullabot/mpx-php

一个用于访问和与ThePlatform(mpx.theplatform.com)的MPX服务交互的PHP库。

3.1.1 2023-10-19 09:15 UTC

README

CircleCI Maintainability Test Coverage Packagist

目录 DocToc生成

快速入门

  • PHP >= 7.4
  • Composer

composer require lullabot/mpx-php

示例

以下是一个完整示例,展示了如何从mpx加载帐户和媒体项。大多数实现不应在单个类中包含所有这些代码。相反,创建函数、类或服务来将客户端、缓存和锁桥接到您的应用程序中。有关如何做到这一点的示例,请参阅Drupal 8的media_mpx模块

此代码的可运行版本位于测试中 \Lullabot\Mpx\Tests\Functional\ReadmeTest::testExample()

<?php

use Lullabot\Mpx\Cache\Adapter\PHPArray\ArrayCachePool;
use GuzzleHttp\Psr7\Uri;
use Lullabot\Mpx\AuthenticatedClient;
use Lullabot\Mpx\Client;
use Lullabot\Mpx\DataService\DataObjectFactory;
use Lullabot\Mpx\DataService\DataServiceManager;
use Lullabot\Mpx\Service\IdentityManagement\User;
use Lullabot\Mpx\Service\IdentityManagement\UserSession;
use Lullabot\Mpx\TokenCachePool;
use Symfony\Component\Lock\Store\FlockStore;

// Only required if your application is not using Composer's autoloader already.
require_once './vendor/autoload.php';

// Create a new mpx client with the default configuration.
$defaults = Client::getDefaultConfiguration();
$client = new Client(new \GuzzleHttp\Client($defaults));

// Replace your username and password here. The username must begin with `mpx/`.
$user = new User('mpx/YOU@EXAMPLE.COM', 'secret');
// The optional lock and cache storage parameters are highly recommended for
// applications with multiple threads or requests.
$session = new UserSession($user, $client);

// This registers the annotation loader.
$dataServiceManager = DataServiceManager::basicDiscovery();

$accountFactory = new DataObjectFactory($dataServiceManager->getDataService('Access Data Service', 'Account', '1.0'), $authenticatedClient);

// Replace the ID with the account ID to load.
$account = $accountFactory->load(new Uri('http://access.auth.theplatform.com/data/Account/12345'))
    ->wait();
print "The loaded account is:\n";
var_dump($account);

$mediaFactory = new DataObjectFactory($dataServiceManager->getDataService('Media Data Service', 'Media', '1.10'), $authenticatedClient);

// Replace the ID to the media item to load. You can find it under "History -> ID" in the mpx console.
$media = $mediaFactory->load(new Uri('http://data.media.theplatform.com/media/data/Media/12345'), $account)
    ->wait();
print "The loaded media is:\n";
var_dump($media);

实现的数据服务

mpx API非常广泛,此库仅实现了整个API的一个子集。但是,始终欢迎包含新数据服务的新功能请求。此外,数据服务通过注解发现,因此可以根据需要从其他库中包含它们。

通过字段和Q查询过滤结果

select()selectRequest()的调用可以按精确匹配字段以及更复杂的搜索进行过滤。

<?php

// This skips the setup from above.
$mediaFactory = new DataObjectFactory($dataServiceManager->getDataService('Media Data Service', 'Media', '1.10'), $authenticatedClient);

// Search for "cats AND dogs" in any field.
$query = new ObjectListQuery();
$cats = new Term('cats');
$termGroup = new TermGroup($cats);
$termGroup->and(new Term('dogs'));
$query->add($termGroup);

// Limit to 10 results per page.
$query->getRange()->setEndIndex(10);
$results = $mediaFactory->select($query, $account);

foreach ($results as $media) {
    var_dump($media);
}

测试客户端

thePlatform提供了一个媒体数据服务Web客户端,可用于快速测试媒体数据服务API。不幸的是,此客户端不能用于其他数据服务API。要测试这些,请参阅tests/src/Functional中的功能测试以及phpunit.xml.dist中的配置。

日志记录

此库将记录对API的操作,这些操作对调用代码是透明的。例如,调用代码应处理无效凭据的记录,而此库将在API请求导致401时自动刷新认证的记录。

如果您的应用程序不希望记录这些操作,请在需要\Psr\Log\LoggerInterface的任何构造函数中使用\Psr\Log\NullLogger

实现自定义mpx字段

mpx数据服务对象可以定义最多100个自定义字段,每个帐户一个。这些字段可以包含各种数据类型,多个自定义字段命名空间应用于单个对象。此库允许开发者在自己的应用程序代码中创建结构化类,这些类将被自动发现和使用。

1. 使用控制台工具创建初始类

此CLI工具使用mpx字段API生成匹配的类。请考虑为mpx中的每个自定义字段添加描述,因为它们将被自动用于文档注释。运行bin/console help mpx:create-custom-field以获取此命令的最新文档。

例如,要为附加到媒体对象的全部自定义字段生成类

  1. 克隆此存储库。
  2. $ composer install
  3. $ bin/console mpx:create-custom-field 'Php\Namespace\For\These\Classes' 'Media Data Service' 'Media' '1.10'
  4. 输入您的用户名和密码。对于找到的每个字段都会显示进度。

由于mpx API中没有用于创建类名的有效数据,因此将为每个字段命名空间创建名为CustomFieldClassOne.php等名称的类。强烈建议将这些类重命名为与它们包含的字段相匹配。

每个生成的类将包含一个@CustomField注解。

/**
 * @CustomField(
 *     namespace="http://access.auth.theplatform.com/data/Account/555555",
 *     service="Media Data Service",
 *     objectType="Media",
 * )
 */

库使用此注解来确定哪个类对应于给定的命名空间。请注意,自定义字段没有模式版本。在删除或更改现有字段的数据类型时要小心。

这些自定义字段应存在于您的应用程序代码中。因此,您需要提供一个方法来发现这些类,因为不同的应用程序有不同的源代码结构。如果您使用的是Drupal等CMS的模块,它应该已经提供了该功能。如果没有,请参阅\Lullabot\Mpx\DataService\CustomFieldManager::basicDiscovery以获取一个可以适应许多情况的示例。

一旦类可用,它们将在加载mpx对象时自动使用。例如,要检索Media对象上上述命名空间的字段,请调用

$dof = new DataObjectFactory($manager->getDataService('Media Data Service', 'Media', '1.10'), $this->authenticatedClient);
$media = $dof->load(new Uri('http://data.media.theplatform.com/media/data/Media/12345'))->wait();
$fields = $media->getCustomFields('http://access.auth.theplatform.com/data/Account/555555'):

如果找不到自定义字段类,将记录一条通知,并将空的MissingCustomFieldsClass附加到每组字段。

相关项目

主要类概述

Lullabot\Mpx\Client

mpx API对GuzzleHttp\ClientInterface的实现。作为一个客户端,它不执行任何额外操作,而是抑制错误以强制返回HTTP 200响应。它还处理响应中的XML。

Lullabot\Mpx\AuthenticatedClient

管理认证会话,并使用ClientInterface实现代理API调用,自动刷新所需的过期API令牌。

Lullabot\Mpx\Service\IdentityManagement\UserSession

一个mpx用户。仅提供用户名和密码获取器。

Lullabot\Mpx\Token

登录后,平台返回的mpx身份验证令牌。

Lullabot\Mpx\TokenCachePool

用户身份验证令牌的缓存。此类是围绕一个\Psr\Cache\CacheItemPoolInterface对象的包装。

mpx 支持

此库不受thePlatform支持。如果您需要有关库的帮助,请在GitHub上打开一个问题。如果您需要有关mpx服务本身的支持,请参阅mpx支持门户以提交支持请求。

已知问题

#2001 in Guzzle强制处理通知以加载所有已通知的对象。考虑在推出新版本之前使用composer-patches应用此补丁。