dozoisch/cachedbuzz

支持缓存处理的 Buzz 浏览器

安装次数: 36

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

1.0 2013-09-21 20:33 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:49:31 UTC


README

此包旨在提供一种缓存通过 buzz bundle 发出的请求的方法。此包可以像 buzz 一样使用,缓存直接集成。该包采用 MIT 许可证。更多信息请参阅路径根目录下的 LICENSE 文件。

安装

  1. 将以下内容添加到 composer.json

    {
        "require": {
            "dozoisch/cachedbuzz": "dev-master"
        }
    }
  2. app/AppKernel.php 中启用此包

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Dozoisch\CachedBuzzBundle\DozoischCachedBuzzBundle(),
        );
    }

配置

没有设置是强制性的,因为所有内容都会回退到包提供默认设置或默认实现。默认的 cache 使用 APC。如果您的 Web 服务器上没有此模块,模块初始化将失败。

以下是一个完整的配置示例(yaml 格式),包括默认值:

dozoisch_cached_buzz:
    http_client:
        timeout: 10
        verify_peer: true
        max_redirects: 5
        ignore_errors: true
    cache: null #takes a string
    validator: null #takes a string

http_client 用于配置 buzz 客户端。cache 设置接受一个字符串,该字符串应该是类或服务的名称。对于 validator 也是同样的道理。

cache 必须实现 Dozoisch\CachedBuzzBundle\Cache\CacheInterface 类。
validator 必须实现 Dozoisch\CachedBuzzBundle\Cache\CacheValidatorInterface 类。

作为服务运行

要将 Cached Buzz Bundle 作为服务运行,将其插入到您的 services.yml 文件中

parameters:
  dozoisch.bundle.name: Dozoisch\CachedBuzzBundle

services:
  # The actual service
  dozoisch.buzz.browser:
    class: '%dozoisch.bundle.name%\Browser'
    arguments: ['@dozoisch.buzz.cacher', '@dozoisch.buzz.client.curl']
    
  # Parametring.
  dozoisch.buzz.client.curl:
    class:  'Buzz\Client\Curl'
    public: false
    calls:
      - [setVerifyPeer, [false]] # this is optional
      - [setTimeout, [100]] # this is optional

  dozoisch.buzz.cacheinterface:
    class: '%dozoisch.bundle.name%\Cache\APCCache'

  dozoisch.buzz.cachevalidator:
    class: '%dozoisch.bundle.name%\Cache\CacheValidator'

  dozoisch.buzz.cacher:
    class: '%dozoisch.bundle.name%\Cacher'
    arguments: ['@dozoisch.buzz.cacheinterface', '@dozoisch.buzz.cachevalidator']

以这种方式使用时,它将覆盖一些正常的包设置,因此浏览器缓存和客户端参数不再是可选的。

如何使用它

现在您可以像调用其他服务一样调用浏览器。

###容器感知类

如果您希望从容器感知类中调用它,例如控制器,请执行 $this->get('dozoisch.buzz.browser');

###非容器感知类

要从非容器感知的服务中调用它,首先将其添加到您的 services.yml

services:
  my.super.service:
    class: xclass.class
    arguments: ['@dozoisch.buzz.browser']

并确保在您的类中具有适当的构造函数

/** @var Buzz\Browser */
protected $browser;

public function __construct(\Buzz\Browser $browser) {
    $this->browser = $browser;
}

###实际使用

检索浏览器后,您可以像使用正常的 buzz 浏览器一样使用它。此包旨在无缝覆盖正常的 buzz 实例。

$response = $this->browser->get("http://example.com");
$content = $response->getContent();

可用的函数有 post、head、patch、put、delete。