zend framework2 - What is the way to bootstrap Zend2 plugins properly? -
currently i'm writing plugins that:
namespace lawyers\controller\plugin; use zend\mvc\controller\plugin\abstractplugin, braintree 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 transaction * * @return \lawyers\model\entity\externalpayment */ protected function spawn() { return new \lawyers\model\entity\externalpayment(); } /** * new sales transaction * * @param mixed $payer - person pays 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'], ); # .... } } what proper way initialize instance variables plugins without using $this->init() in every call? saw no constructor method plugins :(
you can adding initializer plugin manager
first have plugin implement zend\stdlib\initializableinterface. (you'll need make init method public)
namespace lawyers\controller\plugin; use zend\mvc\controller\plugin\abstractplugin, braintree braintreesdk; use zend\stdlib\initializableinterface; class braintree extends abstractplugin implements initializableinterface { /** * set braintree config settings * * @return void */ public function init() { // .. } } then in module bootstrap add initializer.
<?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 manager not add top of stack } } note: intializer added implementing zend\modulemanager\feature\controllerpluginproviderinterface in module class, , using getcontrollerpluginconfig method, or via controller_plugins key in module.config.php. neither of methods allow place initializer @ bottom of stack, necessary here otherwise plugin potentially init before other initializers have had chance inject dependencies.
Comments
Post a Comment