bigperson/exchange1c

1c - CommerceML 协议的目录加载器

v1.2.0 2022-07-21 10:46 UTC

This package is auto-updated.

Last update: 2024-09-21 15:42:49 UTC


README

Packagist Packagist Packagist Travis (.org) Codecov StyleCI

安装此库可以简化将1C集成到您的网站中的过程。

该库包含了一套必需的接口,实现这些接口可以使您与1C交换商品和文档。假设您有1C:Предприятие 8, Управление торговлей",版本11.3, 11.3.2,运行在8.3.9.2033平台。

如果您的配置版本更低,则此库可能仍然可以正常工作,因为在大多数情况下,1C与网站之间的交换在各个版本之间变化不大。

该库是在模块 https://github.com/carono/yii2-1c-exchange 的基础上编写的 - 所有的主要接口都是从该模块中提取的。

依赖项

  • php ^7.1
  • carono/commerceml
  • symfony/http-foundation ^4.1

安装

composer require bigperson/exchange1c

使用

要使用此库,您必须定义配置数组并在您的模型中实现接口。示例中使用了symfony/event-dispatcher适配器 - https://github.com/bigperson/exchange1c-symfony-bridge 您还可以使用用于与Laravel集成的包适配器 - https://github.com/bigperson/laravel-exchange1c

require_once './../vendor/autoload.php'; //Подулючаем автолоад

// Определяем конфиг
$configValues = [
    'import_dir'    => '1c_exchange',
    'login'    => 'admin',
    'password' => 'admin',
    'use_zip'       => false,
    'file_part' => 0,
    'models'    => [
        \Bigperson\Exchange1C\Interfaces\GroupInterface::class => \Tests\Models\GroupTestModel::class,
        \Bigperson\Exchange1C\Interfaces\ProductInterface::class => \Tests\Models\ProductTestModel::class,
        \Bigperson\Exchange1C\Interfaces\OfferInterface::class => \Tests\Models\OfferTestModel::class,
    ],
];
$config = new \Bigperson\Exchange1C\Config($configValues);
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher = new \Bigperson\Exchange1C\SymfonyEventDispatcher($symfonyDispatcher);
$modelBuilder = new \Bigperson\Exchange1C\ModelBuilder();
// Создаем необходимые сервисы
$loaderService = new \Bigperson\Exchange1C\Services\FileLoaderService($request, $config);
$authService = new \Bigperson\Exchange1C\Services\AuthService($request, $config);
$categoryService = new \Bigperson\Exchange1C\Services\CategoryService($request, $config, $dispatcher, $modelBuilder);
$offerService = new \Bigperson\Exchange1C\Services\OfferService($request, $config, $dispatcher, $modelBuilder);
$catalogService = new \Bigperson\Exchange1C\Services\CatalogService($request, $config, $authService, $loaderService, $categoryService, $offerService);


$mode = $request->get('mode');
$type = $request->get('type');

try {
    if ($type == 'catalog') {
        if (!method_exists($catalogService, $mode)) {
            throw new Exception('not correct request, mode=' . $mode);
        }
        //Запускаем сервис импорта каталога
        $body = $catalogService->$mode();
        $response = new \Symfony\Component\HttpFoundation\Response($body, 200, ['Content-Type', 'text/plain']);
        $response->send();
    } else {
        throw new \LogicException(sprintf('Logic for method %s not released', $type));
    }
} catch (\Exception $e) {
    $body = "failure\n";
    $body .= $e->getMessage() . "\n";
    $body .= $e->getFile() . "\n";
    $body .= $e->getLine() . "\n";

    $response = new \Symfony\Component\HttpFoundation\Response($body, 500, ['Content-Type', 'text/plain']);
    $response->send();
}

有关接口及其实现的更详细信息的文档可以在 https://github.com/carono/yii2-1c-exchange 中找到。文档将在以后添加。

许可证

本包是开源代码,采用MIT许可证。