turbine-kreuzberg/spryker-http-recorder

基于php-vcr (https://php-vcr.github.io/) 的 Spryker HTTP 记录器

0.2.5 2022-05-30 12:53 UTC

This package is auto-updated.

Last update: 2024-09-15 07:27:17 UTC


README

此包为 Spryker 提供了php-vcr的集成。
它是php-vcr/php-vcr的一个包装器。

安装

  • 通过 composer 安装此包(通常作为 dev 依赖项,使用标志 --dev
composer require [--dev] turbine-kreuzberg/spryker-http-recorder

用法

您需要在您的调用栈中的类自动加载之后尽快调用 HttpRecorderService::initialize();(通常在 index.php 中),因为 php-vcr 会拦截所有 HTTP 请求。

示例

粘合剂集成

public/Glue/index.php 中,在 Environment::initialize(); 之后添加 HttpRecorderService::initialize();

...
require_once APPLICATION_ROOT_DIR . '/vendor/autoload.php';

Environment::initialize();

HttpRecorderService::initialize();
...

HTTP 请求方法集成

在您想要记录 HTTP 请求的类中,您需要设置用于记录(和回放)的文件。

    public function makeHttpRequest(...)
    {
        // just define the basic filename, the file extension will be added
        // based on the configured storage type ('.yaml', '.json')
        $this->httpRecorderService->setRecordingFile('your-recording-file');

        try {
            $this->client->call(...);
        } finally {
            // stop recording (=turn off VCR client) after the request,
            // no matter if it succeeds or fails
            register_shutdown_function(function () {
                $this->httpRecorderService->stopRecording();
            });
        }
    }

配置

为了快速开始,将以下片段复制到您的 config_local.php

use TurbineKreuzberg\Shared\HttpRecorder\HttpRecorderConstants;

/**
 * MODES: 
 * To enable the usage of http recorder, set the mode.
 * - To just record requests and responses, use mode HttpRecorderConstants::MODE_RECORD
     $config[HttpRecorderConstants::MODE] = HttpRecorderConstants::MODE_RECORD;
 * - To just replay recorded requests and responses, use mode HttpRecorderConstants::MODE_REPLAY
 *   If no recording exists, the first request (and response) will be recorded
     $config[HttpRecorderConstants::MODE] = HttpRecorderConstants::MODE_REPLAY;
 * 
 * LIBRARY HOOKS:
 * By default all library hooks are enabled.
 * But you can also specifically enable only some hooks, e.g. 'soap':
   $config[HttpRecorderConstants::VCR_LIBRARY_HOOKS] = [
     'soap',
   ];
 * For more details, see https://php-vcr.github.io/documentation/configuration/#library-hooks
 *
 * RECORDING PATH:
 * By default, recordings are stored and read from /tmp
 * To use recordings from a different folder, set HttpRecorderConstants::VCR_CASSETTE_PATH to that path.
   $config[HttpRecorderConstants::VCR_CASSETTE_PATH] = APPLICATION_ROOT_DIR . '/your/custom/path/for/vcr/recordings';
 *
 * STORAGE TYPE: 
 * By default, recordings are stored in JSON format ('json'). 
 * To use yaml format, set storage type to 'yaml':  
   $config[HttpRecorderConstants::VCR_STORAGE_TYPE] = 'yaml';
 * For more details, see https://php-vcr.github.io/documentation/configuration/#storage   
 *
 * REQUEST MATCHERS:
 * Requests are matched by different criteria. By default, these request matchers are used:
   $config[HttpRecorderConstants::VCR_REQUEST_MATCHERS] =             [
     'method',
     'url',
     'host',
   ];
 * You can create your own request matchers and use them as callbacks
 * For more details, see https://php-vcr.github.io/documentation/configuration/#request-matching
 * 
 * ALLOW/DENY LISTS FOR PATHS TO INTERCEPT:
 * By default, there is an allow list with 'vendor/guzzle':  
   $config[HttpRecorderConstants::VCR_ALLOW_LIST] = [
     'vendor/guzzle',
   ];
 * The deny list is empty by default:  
   $config[HttpRecorderConstants::VCR_DENY_LIST] = [];
 * For more details, see https://php-vcr.github.io/documentation/configuration/#white--and-blacklisting-paths
*/

致谢

许可协议

MIT 许可证(MIT)。有关更多信息,请参阅许可文件