2

目前我正在编写这样的插件:

namespace Lawyers\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin,
    Braintree as BraintreeSDK;

class Braintree extends AbstractPlugin
{
    protected $__initialized = false;

    protected $__pm;
    protected $__em;

    /**
     * Set Braintree config settings
     *
     * @return void
     */
    protected function init() {
        if($this->__initialized) {
            return;
        }

        $this->__pm = $this->getController()->getEntityRepository();
        $this->__pm = $this->__pm['ExternalPayment'];
        $this->__em =  $this->getController()->getEntityManager();

        $config = $this->getController()->getServiceLocator()->get('Config');

        \Braintree_Configuration::environment($config['braintree']['env']);
        \Braintree_Configuration::merchantId($config['braintree']['merchant_id']);
        \Braintree_Configuration::publicKey($config['braintree']['public_key']);
        \Braintree_Configuration::privateKey($config['braintree']['private_key']);

        $this->__initialized = true;
    }  

    /**
     * Create new entity for transaction
     *
     * @return \Lawyers\Model\Entity\ExternalPayment
     */
    protected function spawn() {
        return new \Lawyers\Model\Entity\ExternalPayment();
    }



    /**
     * New sales transaction
     *
     * @param mixed $Payer - person who pays this transaction
     * @param mixed $Source - source of payment: Lawyers\Model\Entity\Questions or Lawyers\Model\Entity\Lead
     * @param array $transaction - payment details:
     *      'amount' => '1000.00',
     *      'creditCard' => array(
     *          'number' => '5105105105105100',
     *          'expirationDate' => '05/12'
     *      )
     * @return mixed - transaction id or null
     */
    public function sell($Payer, $Source, $transaction) {
        $this->init();

        $data = array(
            'status' => 'pending',
            'amount' => $transaction['amount'],
        );

        # ....
    }
}

为插件初始化实例变量而不$this->init()在每次调用中使用的正确方法是什么?我没有看到类似插件方法的构造函数:(

4

1 回答 1

2

您可以通过将初始化程序添加到插件管理器来做到这一点

首先让你的插件实现Zend\Stdlib\InitializableInterface. (您还需要公开 init 方法)

namespace Lawyers\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin,
    Braintree as BraintreeSDK;
use Zend\Stdlib\InitializableInterface;

class Braintree extends AbstractPlugin implements InitializableInterface
{
    /**
     * Set Braintree config settings
     *
     * @return void
     */
    public function init() {
        // ..
    }

}

然后在您的模块引导程序中添加初始化程序。

<?php
namespace Lawyers;

use Zend\Stdlib\InitializableInterface;

class Module
{
    public function onBootstrap(MvcEvent $e)

        $sm = $e->getApplication()->getServiceManager();

        $plugins = $sm->get('ControllerPluginManager');            

        $plugins->addInitializer(function($plugin, $pm) {
            if ($plugin instanceof InitializableInterface) {
                $plugin->init();
            }
        }, false); // false tells the manager not to add to top of stack
    }
} 

注意:可以通过Zend\ModuleManager\Feature\ControllerPluginProviderInterface在您的 Module 类中实现 并使用该方法getControllerPluginConfig或通过controller_plugins. module.config.php但是,这些方法都不允许您将初始化程序放在堆栈的底部,这是必要的,否则您的插件可能会init在任何其他初始化程序有机会注入依赖项之前。

于 2013-05-10T19:41:20.420 回答