zend-framework2 - 正确引导 Zend2 插件的方法是什么?

标签 zend-framework2

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

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() 初始化插件实例变量的正确方法是什么?我没有看到类似构造函数的插件方法:(

最佳答案

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

首先让您的插件实现 Zend\Stdlib\InitializedInterface。 (您还需要公开 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() {
        // ..
    }

}

然后在模块 Bootstrap 中添加初始化程序。

<?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
    }
} 

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

关于zend-framework2 - 正确引导 Zend2 插件的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16482746/

相关文章:

php - 与 Doctrine 实体的矩阵类型映射

javascript - Zend Framework 2 和 ExtJS

zend-framework2 - 如何在 Zend Framework 2 中加载自定义库?

php - 从 Zend Framework 1 迁移到 Zend Framework 2

php - 使用 Doctrine 插入数据 - 存储库与实体

doctrine-orm - 有没有办法在 Zend Framework 2 中将 Firebird/Ibase 与 Doctrine 一起使用?

mysql - ZF2 如何使用连接重命名字段名称

php - Zend Framework 2 形式,他们是否消除了装饰者模式?

php - ZendFramework 2 - 慢得令人痛苦?

controller - ZF2 - 访问 Controller 中的数据库适配器