Recent Posts


Magento: Generating Class Preferences with pestle


Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

When I started work on pestle’s Magento 2 code generation, I deliberately left the the <preference/> feature of Magento’s di.xml files off the list of commands. This was, in short, to help encourage the use of plugins over preferences. i.e. to encourage developers to use the system that allowed multiple extensions to change system behavior – and not use the system that behaved like Magento 1 rewrites with a winner-take-all end result.

Unfortunately, while the plugin system shows promise, I’ve seen too many situations where it fails to provide the stable functionality a Magento developer needs. I suspect preferences will be the order of the day in the wild, and withholding easy code generation from pestle users seems short sighted.

Configuration a class preference is easy with pestle’s magento2:generate:preference command, just run the following (or use no arguments for interactive prompts).

$ pestle_dev magento2:generate:preference Pulsestorm_HelloWorld 'MagentoFrameworkUrl' 'PulsestormSimpleUiComponentModelUrl'

The first argument is the module you want to configure your preference in, the second is the the class or interface you’re looking to set a preference for, and the third is your new class. Pestle will add a <preference/> node to your module’s di.xml file, and generate a blank class file for you that automatically extends or implements the class or interface.

Upgrade instructions are in the github README file, and if you’re using the latest version before this upgrade, you should be able to use the selfupdate command to grab the latest version.

$ pestle.phar selfupdate

Copyright © Alan Storm 1975 – 2021 All Rights Reserved

Originally Posted: 4th September 2016