acquia / content-hub-php

一个用于消费Acquia Content Hub API的PHP客户端库。

3.5.2 2024-06-28 13:34 UTC

This package is auto-updated.

Last update: 2024-09-13 12:38:58 UTC


README

Build Status

一个用于消费Acquia Content Hub API的PHP客户端库。

版本信息

  • 0.6.x 分支:使用 guzzle 版本 ~5.0。Drupal 7 内容中心模块 依赖于此分支的构建。
  • master 分支:使用 guzzle 版本 ~6.0。目前正在进行的 Drupal 8 内容中心工作依赖于此分支的构建。

安装

使用 Composer 安装最新版本

$ composer require acquia/content-hub-php

使用方法

注册应用程序

应用程序必须向内容中心注册,以便分配一个唯一的标识符。该标识符是大多数API端点所必需的,并用作由应用程序创建并发布到中心的实体的“来源”。

<?php

use Acquia\ContentHubClient\ContentHub;

// The URL to the Content Hub instance, provided by Acquia. Note that us-east-1
// might be replaced by a region that is within your geographic proximity.
$url = 'https://us-east-1.content-hub.acquia.com';

// The API key and secret key provided by Acquia that are used to authenticate
// requests to Content Hub.
$apiKey    = 'AAAAAAAAAAAAAAAAAAAA';
$secretKey = 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB';

// For versions => 1.3, the api key is passed via the HMAC middleware.
$middleware = new MiddlewareHmacV1($apiKey, $secretKey, 'V1');
$client = new ContentHub('', $middleware, ['base_url' => $url]);

// For versions < 1.3, use the following client callback.
$client = new ContentHub($apiKey, $secretKey, '', ['base_url' => $url]);

// Register the application (or client site) with Content Hub. The parameter
// passed to this method is the human-readable name of the application.
$clientSite = $client->register('myclientsite');

// Stores the application's unique identifier that is assigned by Content Hub.
$clientId = $clientSite['uuid'];

添加webhook接收器端点

内容中心通过webhook发送推送通知和异步操作的状态消息。即使您的应用程序不需要近实时内容更新,也应该实现webhook接收器端点,以便您可以看到异步操作期间发生的事情。

<?php

// Add the endpoint that received webhooks posted from Content Hub.
$webhook = $client->addWebhook('http://example.com/content-hub/webhooks');

// Deleting the webhook receiver endpoint so that the application will no longer
// receive webhooks.
$client->deleteWebhook($webhook['uuid']);

创建实体

<?php

use Acquia\ContentHubClient\ContentHub;
use Acquia\ContentHubClient\Middleware\MiddlewareHmacV1;
use Acquia\ContentHubClient\Entities;
use Acquia\ContentHubClient\Entity;
use Acquia\ContentHubClient\Attribute;
use Acquia\ContentHubClient\Asset;

$middleware = new MiddlewareHmacV1($apiKey, $secretKey, 'V1');
$client = new ContentHub($clientId, $middleware, ['base_url' => $url]);

// The unique identifier of the entity, usually a randomly generated UUID.
// See https://github.com/ramsey/uuid to simplify UUID generation in PHP.
$uuid = '00000000-0000-0000-0000-000000000000'

// Build the entity, add required metadata
$entity = new Entity();
$entity->setUuid($uuid);
$entity->setType('product');
$entity->setOrigin($clientId);
$entity->setCreated('2014-12-21T20:12:11+00:00Z');
$entity->setModified('2014-12-21T20:12:11+00:00Z');

// Add attributes
$attribute = new Attribute(Attribute::TYPE_STRING);
$attribute->setValue('nothing', 'en');
$attribute->setValue('nada', 'es');
$attribute->setValue('nothing');
$entity->setAttribute('name', $attribute);

$attribute = new Attribute(Attribute::TYPE_INTEGER);
$attribute->setValue(4);
$entity->setAttribute('age', $attribute);

// Add references to binary assets, e.g. images.
$attribute = new Attribute(Attribute::TYPE_STRING);
$attribute->setValue('[asset-1]');
$entity->setAttribute('image', $attribute);

$asset = new Asset();
$asset->setUrl('http://placehold.it/100');
$asset->setReplaceToken('[asset-1]');
$entity->addAsset($asset);

// Create an entity container, add the entity to it.
$entities = new Entities();
$entities->addEntity($entity);

// Render the entities in Common Data Format (CDF). This should be the payload
// returned by requests to $resourceUrl (defined below).
$cdf = $entities->json();

// Queue the entity to be added to Content Hub. An important concept in Content
// Hub is that write operations are asynchronous, meaning that the actions are
// not performed right away. In this example, a URL is passed to Content Hub
// which is expected to render the entities that you want to add to the hub in
// CDF. Content Hub receives the request and immediately returns a 202 which
// signifies that the request was received. In the background, Content Hub then
// makes a request to the URL, reads the CDF, and adds the entities. Success and
// error messages are sent via webhooks, so it is important to implement a
// webhook receiver endpoint so that you know what is going on.
$resourceUrl = 'http://example.com/path/to/cdf';
$client->createEntities($resourceUrl);

读取实体

<?php

// Get the entity from Content Hub.
$entity = $client->readEntity($uuid);

// Get the "name" attribute in English, then Spanish.
$name   = $entity->getAttribute('name')->getValue('en');
$nombre = $entity->getAttribute('name')->getValue('es');

// Get the URL of the image attribute by dereferencing the token.
$token = $entity->getAttribute('image')->getValue();
$url = $entity->getAsset($token)->getUrl();

更新实体

<?php

// Update the value of the "age" attribute from 4 to 5.
$attribute = new Attribute(Attribute::TYPE_INTEGER);
$attribute->setValue(5);
$entity->setAttribute('age', $attribute);

// Updating entities is also an asynchronous operation, so it may take a couple
// of seconds for the changes to be reflected in the hub.
$client->updateEntity($resourceUrl, $uuid);

删除实体

<?php

// Delete the entity by passing it's UUID. Delete operations are asynchronous,
// so it may take a couple of seconds for entities to be purged from the hub.
$client->deleteEntity($uuid);

运行测试

为了更好地运行测试,此库现在配备了包含以下目标的Makefile:(运行 make help 也会显示此信息)

  • install:安装依赖项
    • 示例:make install
  • method_test file=path/to/test/file:运行特定的测试
    • 示例:make method_test method=testFromJSONStringCreation file=test/CDFObjectTest.php
  • file_test file=path/to/test/file:运行特定的测试
    • 示例:make file_test file=test/CDFObjectTest.php
  • dir_tests dir=path/to/test-directory:运行目录内的所有测试
    • 示例:make dir_tests test
  • all_tests:运行所有单元测试
    • 示例:make all_tests
  • coverage:为单元测试创建测试覆盖率
    • 示例:make coverage
  • infection:在现有测试上运行infection
    • 示例:make infection