digiaonline/lumen-contentful-sync

此包已被放弃,不再维护。未建议替代包。

一个同步Contentful内容到Lumen API的框架

4.2.0 2022-02-21 09:32 UTC

This package is auto-updated.

Last update: 2023-03-05 11:44:44 UTC


README

Test Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License

简介

此库在digiaonline/lumen-contentful之上提供了一个强大的抽象层,目标是使从Contentful同步内容到您的应用程序变得更加容易。

特性

  • 用于同步条目和资产的控制台命令
  • 用于处理webhook的控制器
  • 用于透明地验证webhook请求的中件
  • 用于webhook请求的正确New Relic事务名称仪表化的中件
  • 通过使用作业支持异步处理

要求

  • PHP >= 7.1

安装

  1. 首先将库作为依赖项添加到您的应用程序中
composer require digiaonline/lumen-contentful-sync
  1. config/contentfulSync.php复制到您的配置目录。只有一个必需的配置键 - content_types。此数组应包含您在Contentful空间中拥有的所有内容模型ID的列表,例如。
<?php

return [
    'content_types' => [
        'article',
        'person',
        'video',
    ],
    // ...
];
  1. 扩展Digia\Lumen\ContentfulSync\Services\AbstractContentfulSyncService并实现必要的方法。这是处理条目和资产逻辑所在的地方。参见下一节以获取更多详细信息。

  2. 扩展Digia\Lumen\ContentfulSync\Providers\AbstractContentfulSyncServiceProvider并实现registerContentfulSyncServiceBindings方法。典型的实现方式如下

protected function registerContentfulSyncServiceBindings(Application $app)
{
    // ContentfulSyncService is the concrete implementation we made in step 3
    $app->singleton(ContentfulSyncServiceContract::class, function (Application $app) {
        return new ContentfulSyncService($app->make(Queue::class));
    });
}
  1. 注册您刚刚实现的ServiceProvider
$app->register(\Your\ServiceProvider::class);
  1. 在您的内核中注册控制台命令
protected $commands = [
    // ...
    SyncAssetsCommand::class,
    SyncContentsCommand::class,
];
  1. 如果您打算使用webhook,您将不得不配置一个路由到控制器。在这个例子中,我们将使用New Relic和webhook身份验证中间件,但两者都是可选的
// The route URL is arbitrary, just make sure it matches what you have configured in Contentful
$app->post('/contentful/handleIncomingWebhook', [
    'middleware' => [
        \Digia\Lumen\ContentfulSync\Http\Middleware\WebhookAuthenticationMiddleware::class,
        \Digia\Lumen\ContentfulSync\Http\Middleware\NewRelicMiddleware::class,
    ],
    'uses'       => 'Digia\Lumen\ContentfulSync\Http\Controllers\ContentfulSyncController@handleIncomingWebhook',
]);
  1. 如果您使用webhook身份验证中间件,您可以通过将这些添加到您的.env文件中来配置预期的用户名和密码
CONTENTFUL_SYNC_WEBHOOK_USERNAME=username
CONTENTFUL_SYNC_WEBHOOK_PASSWORD=password

如果您需要更复杂的逻辑,您将不得不创建自己的中间件。

实现服务

由于所有应用程序都不同,因此如何处理您的条目和资产取决于您。

未实现的方法将给定的资产/条目作为JSON提供。您很可能会使用SDK将这些blob转换为实际的对象

/**
 * @inheritdoc
 */
public function handleEntryPublished(string $contentType, string $entryJson, bool $ignoreExisting): void
{
    // We're assuming here that you have injected an instance of ContentfulServiceContract
    $entry = $this->contentfulService->getClient()->reviveJson($entryJson);

    // You can now do e.g. $entry->getTitle(); etc. depending on your content model
}

用法

控制台命令

要同步所有资产和所有配置的内容类型,请运行以下两个命令

php artisan contentful:assets:sync
php artisan contentful:contents:sync

要同步特定内容类型(在这种情况下为article)的所有条目,请运行

php artisan contentful:contents:sync article

要同步新条目,请运行

php artisan contentful:contents:sync article --ignoreExisting

您可以在两个命令中都添加-v-vv-vvv以获取更详细的输出和进度条,例如。

$ php artisan contentful:contents:sync article --ignoreExisting -vvv
Synchronizing content of type "article"...
 300/300 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% 3 secs/3 secs 32.0 MiB

Done, synchronized 300 entries

New Relic中间件

如果您使用New Relic来监控您的应用程序,您可能会注意到所有Contentful webhook都被合并为单个事务(因为它们都使用相同的URL/路由)。

然而,如果您将Digia\Lumen\ContentfulSync\Http\Middleware\NewRelicMiddleware中间件应用到您的路由上,事务将被命名为topic@contentType,例如ContentManagement.Entry.publish@article。这使得您可以特别关注特别慢的webhook。

异步处理

在您的服务提供商实现中,您可以指定一个Illuminate\Contracts\Queue\Queue实例来注入到服务中。除非您已为特定队列进行配置,否则您的应用程序将使用SyncQueue实现来模拟一个完全同步的队列。

通过指定不同的队列实例,您可以将服务执行的所有工作卸载到队列工作者。

许可证

MIT