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 メソッドも public にする必要があります)

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_pluginsmodule.config.phpinit

于 2013-05-10T19:41:20.420 に答える