July 12, 2016

Magento 2 code generation tools

Yireo Blog Post

Now that Magento 2 is gaining popularity, there are also more and more tools that help developers build their modules more easily. One category for this is the category of code generation tools. In this blog, we'll discuss Pestle, Magerun and Mage2Gen.

Generating code

The term code generation is a bit confusing with Magento 2, because Magento itself generates code also: Based on the principle of Dependency Injection, tricks can be made to allow for virtual types, interceptors and factories to be created automatically. This blog is not about that kind of code generation.

Skipping common steps

Instead, this blog deals with the boring process of setting up a module. Once you have created a few Magento 2 modules, you will know that the definition of modules is always the same: A registration.php file to get started, etc/module.xml to specify some details, possibly a helper, some view files, etcetera. Having tools that can get you started, definitely make you more productive.

  • pestle
  • Magerun
  • Mage2Gen.com

Pestle

First of all, there is Pestle. Pestle is written by Alan Storm, who is known in the Magento community for his great blogs and his CommerceBug module (that every developer should have). Pestle is downloaded as a Phar file and offers a wizard-like configuration to build a module and its contents.

I had issues running Pestle under PHP 7, but as I always keep a PHP 5 CLI in my pocket, that issue was solved pretty quickly. To generate a module, you can use the command pestle.phar generate_module which guides you through a few questions and then generates the module.

After this, you can explore the numerous other commands to allow you to build specific functionality into your module - there are commands like generate_acl, generate_view and generate_crud_model. All in all, Pestle is pretty impressive and a lot of things are covered.

And you can simply redo steps as well: Once Pestle is overwriting a file that you have generated earlier as well, a backup file of the original is created.

Magerun

We all love Magerun, so under Magento 2 we all love Magerun2. The number 1 CLI tool for Magento is completely ready for Magento 2. And just like with Magento 1, magerun also ships with a command to create a new module. For instance, running magerun2 dev:module:create --add-all Yireo ExampleMagerun will create a new module with all of the elements available.

One downside is that the module only creates folders and XML files, but no classes yet. Consider this work in progress. The XML files that are generated make already a great kickstart though. And there is a lot more to come: The feature/code-generator branch on GitHub contains more addons for the dev:console commands.

Mage2Gen.com

A newcomer is Mage2Gen.com which offers a web GUI to click together your own module and download the source. The project is created by Experius, while the source code is hosted on https://github.com/krukas/Mage2Gen - so theoretically you can host your own generator.

The work so far is pretty impressive: Instead of just generating generic code, Mage2Gen dives deeper and allows you to configure various parameters before actually generating the classes. Because of the numerous options in the GUI, the classes also become much more complex.

For instance, setup classes and observers are generated complete with their execute and install methods plus dependencies. It is not perfect yet - my Plugin class seemed much more M1-like with some commas in the wrong places - but it is beta and for a tool of this magnitude, the work done is already awesome. And I should not be bitching about commas but fix things myself with a PR to their repo.

I definitely recommend you to check out this tool!

Cleaning up

All of these tools are great, but personally I like to gain control of my code: I want to know the PHP that I put on the web. Because of this, I will never use these tools to generate a module that I will not go through line-by-line afterwards. Cleaning up the code is logical. Most of the code is generated without PhpDocs, while I consider adding PhpDocs part of my daily programming. And running a tool to fix code compliance is also a smart thing to do (PSR-fixer or simply PhpStorms reformatting options).

Besides, when these tools are used, the work isn't done anyway. It's a quickstart to base your own work upon, it's a free way of building a complete functional module.

About tests

One final note can be made on testing: None of the tools include automatically generated tests. Obviously the real-life tests will be based on real-life code. And generating code might seem conflicting with the concept of TDD. But it would have been nice if there were empty testing classes created, so that afterwards the right methods could be added.

Anyway, I'm personally using all three tools: To educate others on coding in Magento 2. Or simply to quickly create a base module that I can then extend. I hope you enjoy them too!

EDIT: Mage2Gen added a test skeleton shortly after writing this blog: It creates an empty test class with all possible methods (setup, teardown, test methods). Awesome!

Posted on July 12, 2016

About the author

Author Jisse Reitsma

Jisse Reitsma is the founder of Yireo, extension developer, developer trainer and 3x Magento Master. His passion is for technology and open source. And he loves talking as well.

Sponsor Yireo

Looking for a training in-house?

Let's get to it!

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

Do not miss out on what we say

This will be the most interesting spam you have ever read

We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.