Early Stage Plugins/Interceptors Don’t Work in Developer Mode · Issue #2674 · magento/magento2
This one was fun to track down. I was trying to plugin to Magento’s logger (
) but my plugin wasn’t working. Being new to Magento, I assumed the problem was an incorrect configuration, but everything seemed
ok. Madness. After a spelunk down into the bowels of Magento’s object manager code, I found the problem. If developer mode is enabled, there are certain classes you won’t be able to plugin to.
The short short version – pre-generate your classes like you would before deploying to production
php bin/magento setup:di:compile
The medium short version: In developer mode the object manager will dynamically generate the Interceptor classes Magento uses to implement plugins. However, Magento uses the object manager before it has loaded all the
di.xml files, and these
di.xml files are where plugins are configured. If the object manager has instantiated a singleton/single-instance object – this means post
di.xml loading it will still return the original, non-intercepted classes.
Classic bootstrap chicken/egg situation.
setup:di:compile command analyzes your Magento system and pre-generates any extra classes that are needed. There’s no automatic code generation when Magento’s running in production mode.
Here’s a quick list of classes you can’t “developer mode plugin to” in a stock 2.0 system. Keep in mind this may change if there’s additional classes you and/or Magento instantiates during the early bootstrap stages.