Extension’s custom initialization on Magento start
To load custom mini-libraries with short popular global functions in Magento 2, run the following code:
1 2 3 4 5 6 |
<type name='\Magento\Framework\AppInterface'> <plugin name='Df\Framework\AppInterfacePlugin' type='Df\Framework\AppInterfacePlugin' /> </type> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace Df\Framework; use \Magento\Framework\AppInterface; class AppInterfacePlugin { /** * @see \Magento\Framework\AppInterface::launch() * @param AppInterface $subject * @return array */ public function beforeLaunch(AppInterface $subject) { // Place your custom initialization code here. return []; } } |
In case of console apps, do everything as follows:
1 2 3 4 5 6 7 |
<event name='store_collection_load_before'> <observer name='Df\Core\Boot' instance='Df\Core\Boot' method='storeCollectionLoadBefore' /> </event> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace Df\Core; use \Magento\Framework\Event\Invoker\InvokerDefault; use \Magento\Framework\Event\Observer as _O; class Boot { /** * @used-by InvokerDefault::_callObserverMethod() * @param _O $o * @return void */ public function storeCollectionLoadBefore(_O $o) { if (!self::$_done && 'cli' === PHP_SAPI) { self::run(); } } // other code here... } |