2011-07-28 13 views
1

私たちは私の仕事場でZendFrameworkをWebアプリケーション用に使用しています。それは大丈夫ですが、(依存性注入や制御の逆転、AOPなどの)現代的なプラクティスが欠けています。zendフレームワークとの統合

数ヶ月間、私はテストドライブとしてDIとAOPのコンテナとしてDingフレームワークを使用してきました。私は本当にそれが好きなので、私はそれを私たちのプロジェクトに持っていきたいと思います。

どのようにですか?そこで、Zend FrameworkアプリケーションでDingを正しく統合する方法についての質問があります。 ZFコントローラがBeanであると考えると(ディスパッチャからインスタンス化されているので)、すべての依存関係を正しくインジェクトする方法はありますか?

P.s:Zend Frameworkを使用しない(少なくとも中期的には)オプションはありません。 P.P.S:誰かが "ding"を新しいタグとして追加するよう気をつけますか?

+0

今日、新しいDingフレームワークをリリースしましたが、何か宣伝を探していますか? –

+0

ありがとうございますが、私は著者ではないことを確認できます:) – laurac

+0

Ding Frameworkとは何ですか?リンクしてください。 – curtisdf

答えて

1

丁さんがあなたを助けてくれてうれしいです。 私はこのプロジェクトに貢献し、Zend Frameworkアプリケーションとの統合も必要でした。私はこれを達成するためにZendのアプリケーションリソースとプラグインシステムを使用しました。

アプリケーションリソース(あなたはプロジェクト間で再利用することができます)

<?php 
class Application_Resource_Ding extends Zend_Application_Resource_ResourceAbstract 
{ 

    protected $_options = array(
     'factory' => array(
      'bdef' => array(
       'xml' => array(
        'filename' => array('beans.xml') 
       ), 
      ), 
     ), 
     'cache' => array(
      'proxy' => array('impl' => 'dummy'), 
      'bdef' => array('impl' => 'dummy'), 
      'beans' => array('impl' => 'dummy') 
     ) 
    ); 

    public function init() 
    { 
     // set default config dir before mergin options (cant be set statically) 
     $this->_options['factory']['bdef']['xml']['directories'] = array(APPLICATION_PATH .'/configs'); 

     $options = $this->getOptions(); 

     // parse factory properties (if set) 
     if (isset($options['factory']['properties'])) { 
      $options['factory']['properties'] = parse_ini_file(
       $options['factory']['properties'] 
      ); 
     } 

     // change log4php_properties for log4php.properties (if exists) 
     if (isset($options['log4php_properties'])) { 
      $options['log4php.properties'] = $options['log4php_properties']; 
      unset($options['log4php_properties']); 
     } 
     $properties = array(
      'ding' => $options 
     ); 

     return Ding\Container\Impl\ContainerImpl::getInstance($properties); 
    } 

} 

コントローラの内部で使用するためのアクションヘルパー:あなたapplication.iniあなたはこのような何か(プラス余分を追加する必要は

<?php 

class Application_Action_Helper_Ding extends Zend_Controller_Action_Helper_Abstract 
{ 

    protected $ding = null; 

    public function init() 
    { 
     // just once... 
     if (null !== $this->ding) { 
      return; 
     } 

     // get ding bootstrapped resource 
     $bootstrap = $this->getActionController()->getInvokeArg('bootstrap'); 

     $ding = $bootstrap->getResource('ding'); 

     if (!$ding) { 
      throw new Zend_Controller_Action_Exception(
       'Ding resource not bootstrapped' 
      ); 
     } 

     $this->ding = $ding; 
    } 

    public function getBean($bean) 
    { 
     return $this->ding->getBean($bean); 
    } 

    public function direct($bean) 
    { 
     return $this->getBean($bean); 
    } 

} 

必要な構成)

resources.frontController.actionHelperPaths.Application_Action_Helper = "Application/Action/Helper" 

resources.ding.factory.properties = APPLICATION_PATH "/configs/ding.properties" 
resources.ding.log4php_properties = APPLICATION_PATH "/configs/log4php.properties" 

そして、あなたのコントロールrsを使用してBeanをリクエストします。

$service = $this->_helper->ding('someService'); 

+0

ありがとう!私はこのアプローチを試してみましょう! – laurac

+0

Dingが依存性注入を提供する場合、コントローラをインスタンス化した後にZFによって呼び出される共通のコントローラスーパークラスの 'init()'メソッドから呼び出す。 –

+0

私は、開発者にオーバーヘッドを避けながら使い易さを与えるので、「アクションヘルパー」アプローチを好む。コントローラーの 'init()'メソッドを使用して、すべてのコントローラーのアクションに共通の依存関係をロードすることができます。私の経験では、 'init()'メソッドで何を持っているかを考えるよりも、いくつかのキーストロークを入力するほうが好きです – agvstin

関連する問題