0

我正在尝试为 Chargify https://github.com/johannez/chargify设置这个 PHP 库的 Symfony 实现

我有点迷失了找出最好/正确的方法来设置它。

我想我需要将 Guzzle 设置为服务,然后创建一个 Chargify 工厂并将其添加为服务。

我的问题是在工厂类中,当我尝试使用 Guzzle 服务时出现致命错误

Fatal error: Using $this when not in object context in /symfony/src/Acme/ChargifyBundle/Factory/ChargifyFactory.php on line 8

这是我的工厂课

<?php
namespace Acme\ChargifyBundle\Factory;

class ChargifyFactory implements ChargifyFactoryInterface
{
    public static function build($type)
    {
        $client = $this->get('chargify.guzzle.client');

        $className = 'Chargify\\Controller\\' . ucfirst($type);

        if (class_exists($className)) {
            return new $className($client);
        }
        else {
            throw new Exception("Invalid controller type given.");
        }
    }
}

如果查看一些配置很有用,这是我的 services.yml 包

services:
    chargify.guzzle.client.curl_auth:
        class: %guzzle.plugin.curl_auth.class%
        arguments:
            api_key: %chargify_api_key%
    chargify.guzzle.client:
        class: %guzzle.client.class%
        tags:
            - { name: guzzle.client }
        calls:
            - [setBaseUrl, [%chargify_domain%]]
            - [addSubscriber, [@chargify.guzzle.client.curl_auth]]
        argument: %chargify_domain%
    chargify.factory:
        class: Acme\ChargifyBundle\Factory\ChargifyFactory
        arguments:
            - ["type"]
    chargify.customer:
        class: Acme\ChargifyBundle\Controller\CustomerController
        factory_class: Acme\ChargifyBundle\Factory\ChargifyFactory
        factory_method: build
        arguments:
            type: "customer"

我如何在工厂中使用 guzzle 客户端而不使用

$client = $this->get('chargify.guzzle.client');

编辑:

我已根据@alex 的回答更改了代码,但仍然出现错误。我认为这是因为该功能是静态的。我查看了文档,但看不到在没有静态函数的情况下可以在哪里设置工厂,当我摆脱静态函数时,我得到了一个不同的错误。

Runtime Notice: Non-static method Acme\ChargifyBundle\Factory\ChargifyFactory::build() should not be called statically, assuming $this from incompatible context

这是从一些生成的代码中抛出的

protected function getChargify_CustomerService()
{
    return $this->services['chargify.customer'] = \Acme\ChargifyBundle\Factory\ChargifyFactory::build('customer');
}
4

0 回答 0