遅延ロードモジュールのブートストラップ

私のように、多くはModulesリソースを使用します。これにより、 モジュールごとに個別のBootstrapファイルを使用できます。 ただし、このリソースには1つの欠点があります。特定のリクエストで使用するモジュールに関係なく、常にすべてのBootstrapファイルをダウンロードします。 この問題の解決策を提供することにしました



開始するには、リソース自体をわずかに変更します

<?php

クラス System_Application_Resource_ModulesはZend_Application_Resource_ResourceAbstractを拡張します

{

パブリック関数init()

{

$ modulePlugin = new System_Controller_Plugin_ModuleBootstrap();

$ modulePlugin-> setBootstrap($ this-> getBootstrap());



$ this-> getBootstrap()

->ブートストラップ( 'FrontController'

-> getResource( 'FrontController'

-> registerPlugin($ modulePlugin);



return $ modulePlugin;

}

}



コードは簡単に不名誉です。 新しいコントローラープラグインを作成し、フロントコントローラーFrontControllerに登録します

ご想像のとおり、すべての「魔法」はSystem_Controller_Plugin_ModuleBootstrapで発生します

<?php

クラス System_Controller_Plugin_ModuleBootstrapはZend_Controller_Plugin_Abstractを拡張します

{

/ **

* var Zend_Application_Bootstrap_BootstrapAbstract

* /

protected $ _bootstrap = null ;



/ **

* var配列

* /

protected $ _bootstrapedModules = array();



/ **

*コンストラクター

* param array $オプション

** /

パブリック関数__construct($ options = null ){

if ($ options!== null ){

$ this-> setOptions($ options);

}

}



/ **

*構成可能なオブジェクトパターンを実装する

* param array $オプション

* /

パブリック関数setOptions($オプション)

{

foreach ((array)$ options as $ name => $ value ){

$ setter = 'set' .ucfirst($ name);

if (is_callable(array($ this 、$ setter))){

$ this-> $ setter($ value );

}

}

}



/ **

*ブートストラップセッター

* /

パブリック関数setBootstrap(Zend_Application_Bootstrap_BootstrapAbstract $ bootstrap)

{

$ this-> _ bootstrap = $ bootstrap;

}



/ **

*フロントコントローラーを取得する

* Zend_Controller_Frontを返します

* /

保護された関数_getFront()

{

return Zend_Controller_Front :: getInstance();

}

/ **

*戻りはモジュール実行です

* param string $モジュール

*ブール値を返す

* /

パブリック関数isBootsraped($モジュール)

{

return isset($ this-> _ bootstrapedModules [$ module]);

}





/ **

*実行されたブートストラップを取得する

*

*配列を返す

* /

パブリック関数getExecutedBootstraps()

{

return $ this-> _ bootstrapedModules;

}



/ **

*モジュール名をモジュールクラスプレフィックスにフォーマットする

*

* param string $ name

* 戻り文字列

* /

保護された関数_formatModuleName($ name)

{

$ name = strtolower($ name);

$ name = str_replace(array( '-''。' )、 '' 、$ name);

$ name = ucwords($ name);

$ name = str_replace( '''' 、$ name);

$名を返します。

}



/ **

* preDispatchフック

* /

パブリック関数preDispatch(Zend_Controller_Request_Abstract $ request)

{

$ module = $ request-> getModuleName();

if (empty($ module)){

$ module = $ this-> _ getFront()-> getDefaultModule();

}



if (!$ this-> isBootsraped($ module)){

$ moduleDirectory = $ this-> _ getFront()-> getControllerDirectory($ module);

$ bootstrapClass = $ this-> _ formatModuleName($モジュール)。 '_Bootstrap' ;



if (!class_exists($ bootstrapClass、 false )){

$ bootstrapPath = dirname($ moduleDirectory)。 '/Bootstrap.php' ;

if (file_exists($ bootstrapPath)){

$ eMsgTpl = 'モジュール "%s"のブートストラップファイルが見つかりましたが、ブートストラップクラス "%s"が見つかりません' ;

include_once $ bootstrapPath;

if (!class_exists($ bootstrapClass、 false )){

新しい Zend_Application_Resource_Exception(sprintf(

$ eMsgTpl、$モジュール、$ bootstrapClass

));

}

} else {

帰る

}

}



$ moduleBootstrap = new $ bootstrapClass($ this-> _ bootstrap);

$ moduleBootstrap-> bootstrap();

$ this-> _ bootstrapedModules [$ module] = $ moduleBootstrap;

}



}

}





プラグインでは、preDispatchイベントを処理します。このイベントでは、現在のモジュールがまだロードされていない場合、現在のモジュールのBootstrapをロードして実行します。



標準クラスの代わりにクラスを使用するには、configでクラスへのパスを追加するだけです

pluginPaths.System_Application_Resource = "システム/アプリケーション/リソース"







使って楽しむ



All Articles