enm/shopware-sdk

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

访问 Shopware API 的实现

0.3.0 2019-09-06 09:00 UTC

This package is auto-updated.

Last update: 2019-09-06 09:02:51 UTC


README

Build Status SensioLabsInsight

此库为 Shopware REST-API 提供一个可扩展的抽象层。提供的默认实现基于 jms/serializerguzzlehttp/guzzle。所有提供的接口都可以使用,无需安装 Guzzle 或 Serializer。

安装

composer require enm/shopware-sdk

如果您想使用默认实现,还必须运行

composer require guzzlehttp/guzzle

composer require jms/serializer

使用

\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
    'JMS\Serializer\Annotation', __DIR__.'/../vendor/jms/serializer/src'
);

$client = new \Enm\ShopwareSdk\Http\GuzzleAdapter(new \GuzzleHttp\Client());
$client->withConfig('http://example.com', 'user', 'apiKey');

$serializer = \JMS\Serializer\SerializerBuilder::create()->build();


$entryPoint = new \Enm\ShopwareSdk\EntryPoint($client);

$entryPoint->addSerializer(new \Enm\ShopwareSdk\Response\ArticleHandler($serializer));
$entryPoint->addSerializer(new \Enm\ShopwareSdk\Response\OrderHandler($serializer));

$entryPoint->addDeserializer(new \Enm\ShopwareSdk\Response\ArticleHandler($serializer));
$entryPoint->addDeserializer(new \Enm\ShopwareSdk\Response\OrderHandler($serializer));

或者

$entryPoint = \Enm\ShopwareSdk\EntryPoint::buildDefault('http://example.com', 'user', 'apiKey');

端点可以像这样调用

$entryPoint->articles()->findAll();
$entryPoint->articles()->find(1);

$entryPoint->orders()->findAll();
$entryPoint->orders()->find(1);

概念

入口点

入口点应实现 \Enm\ShopwareSdk\EntryPointInterface 并负责创建和管理端点实例。

默认入口点(\Enm\ShopwareSdk\EntryPoint)需要一个配置好的 HTTP 客户端以及每个端点的序列化和反序列化器。

可以通过调用添加序列化器

    $entryPoint->addSerializer($serializer);

可以通过调用添加反序列化器

    $entryPoint->addDeserializer($serializer);

端点

端点负责提供访问 Shopware API 的特定子部分的访问权限。

例如,ArticelEndpoint 负责访问 /api/articles 下的所有 API 路由。

所有默认端点都是扩展抽象端点,需要 HTTP 客户端和响应处理器。

(HTTP-)客户端

HTTP 客户端负责请求 API 路由并使用结果创建 PSR-7 响应对象。

默认客户端(\Enm\ShopwareSdk\Http\GuzzleAdapter)使用 Guzzle HTTP 客户端。

序列化器 / 反序列化器

序列化器负责将给定的 PHP 对象转换为所需的 JSON 字符串。

反序列化器负责将 JSON 响应转换为所需的 PHP 对象。

默认处理器(序列化器和反序列化器的组合)基于 JMS Serializer,可以通过从默认的 EntryPoint 调用 addDefaultSerializersaddDefaultDeserializers 来添加。

如果您不想使用它,您可以编写自己的,必须实现 \Enm\ShopwareSdk\Serializer\JsonSerializerinterface 和/或 \Enm\ShopwareSdk\Serializer\JsonDeserializerinterface

当调用 getSupportedTypes 时,序列化器必须返回所有支持的类型。类型是模型接口的类名,例如:对于 OrderEnpoint,是 \Enm\ShopwareSdk\Model\Order\OrderInterface