it-bens/shopware-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

it-bens/shopware-sdk

Composer 安装命令:

composer require it-bens/shopware-sdk

包简介

A PHP SDK for Shopware 6 Platform (forked from vin-sw/shopware-sdk)

README 文档

README

Software License

Note

This package is a fork of the vienthuong Shopware SDK (https://github.com/vienthuong/shopware-php-sdk) but large parts of the code have been rewritten and new concepts were introduced.

A Symfony bundle for this package is also available at Shopware SDK Bundle.

Shopware PHP SDK is a SDK implementation of the Shopware 6 API. It helps to access the API in an object-oriented way.

Installation

The SDK can be installed via composer

composer require it-bens/shopware-sdk

Main Features

Tip

A Symfony bundle for this package is also available at Shopware SDK Bundle.

Tip

A Laravel 8 package for the forked package is also available at Laravel Shopware SDK Adapter.

Usage

Authentication and Context

Authentication

All requests to the API (except for the OAuth token request) require a Context object. The high-level services like the repositories and the API services are building the context by themself via dependency injection. They require a ContextBuilderFactoryInterface implementation. This factory requires an AccessTokenProvider implementation.

There currently two implementations available:

  • WithClientCredentials for an authentication with client ID and client secret
  • WithUsernameAndPassword for an authentication with username and password

If the application is done with username and password, Shopware will also return a refresh token. While the grant type is implemented, It is currently not used in the SDK.

The dependency injection chain for the ContextBuilderFactory looks like this:

AccessTokenFetcher --> AccessTokenProvider --> ContextBuilderFactory

The AccessTokenFetcher should be cached with a PSR-16 cache implementation with the CachedFetcher. The SimpleFetcher can be used directly but this would lead to a new token request for every request.

Warning

Requesting a new access token for every request will probably lead to a rate limit hit on the Shopware API.

Context

The Context also contains the Shopware URL and several parameters that will be sent to Shopware as headers:

  • Language ID
  • Currency ID
  • Version ID (for creating multiple versions of an entity)
  • Compatibility
  • inheritance (whether the entity should inherit from the parent entity)

More headers can be added as well.

The ContextBuilder takes care of the Context creation via builder pattern.

use Vin\ShopwareSdk\Context\ContextBuilder;
use Vin\ShopwareSdk\Auth\AccessTokenProvider;

$shopwareUrl = 'https://shopware.local';
/** @var AccessTokenProvider $accessTokenProvider */
$contextBuilder = new ContextBuilder($shopwareUrl, $accessTokenProvider);

$contextBuilder->withLanguageId('188208f3609a43d1a493698363fa3cce')
    ->withAdditionalHeader('indexing-behavior', 'disable-indexing');
$context = $contextBuilder->build();

Entity Definitions

The package contains complete sets of Shopwares native entities and their definitions for different Shopware versions. Every entity has a definition class, an entity class and a collection class. They can be found in the Vin\ShopwareSdk\Data\Entity namespace grouped by the Shopware version.

The DefinitionProvider collects these definitions via DefinitionCollectionPopulator implementations. This package already provides the WithSdkMapping implementation. It registers the native entities by using a JSON mapping file. Users of this package can add other implementations to add or overwrite definitions. This is useful if entities of Shopware plugins should be used. Overwriting the native definition and entity is necessary if the Shopware plugin adds an extension/relation.

The SchemaProvider requires a DefinitionProviderInterface implementation. It is a convenience class to prevent the construction of the Schema objects on every getSchema call. The repeated construction lead to a huge performance decrease in the hydration process of the original repository.

Entity hydration

The hydration is an internal process and will not be discussed too deeply here. The process was nearaly completely rewritten in comparison to the original package. This lead to a huge performance increase for entities with many relations. (I tested it in production with an entity that had about 1000 related entities: hydration took more than 15 minutes with the old process and about 20 seconds with the new one)

The process supports all kinds of searches, relations and extensions. Aggregations are not supported yet.

The hydrated entities contain scalar values, typed relations to other entities, collections for to-many relations and extensions (these are provided by the Struct class).

Entity Repository

An EntityRepository requires an EntityDefinition a ContextBuilderFactoryInterface implementation, an HttpClientInterace implementation and a HydratorInterface implementation.

The re-addition of a repository factory is planned but not yet implemented.

To simplify the creation and usage of repositories, this package provides a RepositoryProvider that requires a DefinitionProviderInterface and the dependencies mentioned above. The provider caches the created repositories. A repository can be requested with the getRepository method by its entity name. The entity name can be taken from the entity definition via getEntityName method.

Criteria

The criteria usage is similar the usage in the Shopware core DAL: Search Criteria Documentation.

This is an example to retrieve a product that have free shipping:

use Vin\ShopwareSdk\Repository\RepositoryProviderInterface;
use Vin\ShopwareSdk\Data\Entity\v0000\Product\ProductDefinition;
use Vin\ShopwareSdk\Data\Criteria;
use Vin\ShopwareSdk\Data\Filter\EqualsFilter;

/** @var RepositoryProviderInterface $repositoryProvider */
$productRepository = $repositoryProvider->getRepository(ProductDefinition::class);

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('shippingFree', true));

$products = $productRepository->search($criteria); // the formerly required context object is created by the repository itself

API Services

The supported APIs were already mentioned in the Main Features section. Most service calls allow the usage of additional HTTP headers.

PSR Usage

In order to make this package more compatible with existing code bases, it uses several PSR interfaces instead of concrete implementations.

PSR-7, PSR-17 and PSR-18

While the original package uses GuzzleHttp, this package works with any implementation of the mentioned interfaces. GuzzleHttp is one of them. But Nyholm and Symfony HTTP clients are working as well.

PSR-16

The CachedFetcher requires a PSR-16 cache implementation. This simple cache is provided by the Symfony cache component. But it can be implemented very easy by yourself, too.

PSR-20

The clock is used for the token expiration check instead of the native time function. This is especially useful for testing.

Contributing

I am really happy that the software developer community loves Open Source, like I do! ♥

That's why I appreciate every issue that is opened (preferably constructive) and every pull request that provides other or even better code to this package.

You are all breathtaking!

Special Thanks

Special thanks goes to the original author of the package, vienthuong and the other contributors of the original package.

it-bens/shopware-sdk 适用场景与选型建议

it-bens/shopware-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.09k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 it-bens/shopware-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 it-bens/shopware-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 it-bens/shopware-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.09k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 19
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 49
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-01