Code Generation with Pestle
July 18, 2016 | Write by opensource-services
We interrupt your regularly scheduled Magento tutorial for a quick announcement about a new (Magento related project) I’ve been working on.
Pestle is a new PHP (5.6+) framework for creating and organizing PHP programs into python like libraries of functions. It’s a little outside the mainstream direction PHP’s heading these days, but if python like library of functions whispers to a secret part of your programming heart, checkout the wiki and your feedback is more than welcome.
If python like library of functions does not whisper to a secret part of your programming heart, the other half of pestle is a growing list of Magento 2 code generation functions. I was always a little disappointed that Magento’s community driven code generation tools have been focused around a kitchen sink approach, and pestle’s
generate_ commands are a response to that.
Right now we have commands for generating a new module, a new route, a new view/block/layout for that route, a new observer, a new Magento CLI command (recursive!), a skeleton theme, a plugin’s configuration, and a command to automatically add the property, parameter, and assignment that injects a dependency into a class. We’re also keeping a running list of command suggestions, so if you have an idea or would like to work on an extension please head on over to GitHub.
If you’re not familiar with what code generation can do for you, the rest of this article explores the pestle command for creating a new module.
Creating the Module
If you’ve worked through the Introduction to Magento 2 — No More MVC, you know creating a basic Magento module is straight forward, but involved many different files. Pestle can handle the grunt work of creating those files for you.
For example — all you need to do to generate your
module.xml boiler plate is run the following.
<code> $ pestle.phar generate_module Vendor Namespace? (Pulsestorm)] Pulsestorm Module Name? (Testbed)] HelloPestle Version? (0.0.1)] 0.0.1 Created: /path/to/magento2/app/code/Pulsestorm/HelloPestle/etc/module.xml Created: /path/to/magento2/app/code/Pulsestorm/HelloPestle/registration.php </code>
Then enable it with Magento’s CLI tool
<code> $ php bin/magento module:enable Pulsestorm_HelloPestle </code>
Then tell Magento to update the setup resource versions
<code> $ bin/magento setup:upgrade </code>
Adding the Route/URL Endpoint
The next part of creating a Magento module is adding a URL controller endpoint, which is also known as a route. Pestle can handle this for you as well.
<code> $ pestle.phar generate_route Which module? (Magento_Catalog)] Pulsestorm_HelloPestle Which area? [frontend, adminhtml] (frontend)] frontend Frontname/Route ID? ()] hello_pestle /path/to/magento2/app/code/Pulsestorm/HelloPestle/etc/frontend/routes.xml /path/to/magento2/app/code/Pulsestorm/HelloPestle/Controller/Index/Index.php </code>
Open the generated controller and add the following debugging code to the
then clear your Magento cache and load the
hello_pestle URL in your browser
<code> http://magento.example.com/hello_pestle </code>
You should see an HTML page returned with
HTTP Status: 200, and the following content.
<code> string 'Pulsestorm\HelloPestle\Controller\Index\Index::execute' (length=54) </code>
Don’t forget to remove the
var_dump debugging code before moving on.
Adding a View
Now that we have a module and controller endpoint configured, we need to add Magento’s default view files. Once again, pestle can save us the hassle of needing to manually create our layout handle XML file, phtml template, and default block/view class.
<code> $ pestle.phar generate_view Pulsestorm_HelloPestle frontend hello_pestle_index_index Main content.phtml $ pestle.phar generate_view Which Module? (Pulsestorm_HelloGenerate)] Pulsestorm_HelloPestle Which Area? (frontend)] frontend Which Handle? (pulsestorm_hellopestle_index_index)] hello_pestle_index_index Block Name? (Main)] Main Template File? (content.phtml)] content.phtml Creating /path/to/magento2/app/code/Pulsestorm/HelloPestle/view/frontend/templates/content.phtml Creating: Pulsestorm\HelloPestle\Block\Main Creating: /path/to/magento2/app/code/Pulsestorm/HelloPestle/view/frontend/layout/hello_pestle_index_index.xml </code>
After running the above, clear your cache and reload the page. You should see a fully laid out Magento page, with your view’s content right in the middle.
In addition to helping developers automate the grunt work of a Magento module, it’s possible to specify any generate input as a command argument. This means you could recreate the above with a shell script that looks like this
<code> #!/bin/bash pestle.phar generate_module Pulsestorm HelloPestle 0.0.1 pestle.phar generate_route Pulsestorm_HelloPestle frontend hello_pestle pestle.phar generate_view Pulsestorm_HelloPestle frontend hello_pestle_index_index Main content.phtml </code>
Like what you see? There’s installation instructions in the README, and you can get a full list of commands by running
<code> $ pestle.phar list </code>
If you run into any problems/inefficiencies, or have command ideas, issue tickets are encouraged and welcomed.