2016-09-30 2 views

答えて

0

DIでは、extbase objectManagerで管理されているクラスが必要です。ただし、フック内のオブジェクトは通常DIをサポートしない\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceでインスタンス化されます。

しかし、extbase objectManagerで管理されているフックとサービスクラスの間にプロキシレイヤーを追加できます。

はここでコンストラクタ・インジェクション使用例を示します

use TYPO3\CMS\Core\Utility\GeneralUtility; 
use TYPO3\CMS\Extbase\Object\ObjectManager; 

class YourHook { 
    public function yourMethod(value) 
    { 
     $this->objectManager = $this->getObjectManager(); 
     $yourService = $this->objectManager->get(YourService::class); 
     $result = $yourService->process($value); 

     return $result; 
    } 

    protected function getObjectManager() 
    { 
     return GeneralUtility::makeInstance(ObjectManager::class); 
    } 

} 

class YourService { 
    public function __construct(OtherService $otherService) 
    { 
     $this->otherService = $otherService; 
    } 

    public function process($value) 
    { 
     return $this->otherService->doFancyStuff($value); 
    } 
} 

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3_hook.php']['hookName'][] = 'EXT:your_ext/Classes/YourHook.php:YourHook->yourMethod' 
+0

Thx!私はこの単純で効果的な可能性については考えなかった。これにより、テストフックに関連するものを単体テストするのが容易になります。 –

関連する問題