Magento 2: Code Generation with Pestle

magento-logo
  1. 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.

    $ 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
    

    Then enable it with Magento’s CLI tool

    $ php bin/magento module:enable Pulsestorm_HelloPestle
    

    Then tell Magento to update the setup resource versions

    $ bin/magento setup:upgrade    
    

    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.

    $ 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    
    

    Open the generated controller and add the following debugging code to the executemethod.

    #File: app/code/Pulsestorm/HelloPestle/Controller/Index/Index.php
    public function execute()
    {
        var_dump(__METHOD__);
        return $this->resultPageFactory->create();  
    }
    

    then clear your Magento cache and load the hello_pestle URL in your browser

    http://magento.example.com/hello_pestle
    

    You should see an HTML page returned with HTTP Status: 200, and the following content.

    string 'Pulsestorm\HelloPestle\Controller\Index\Index::execute' (length=54)
    

    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.

    $ 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
    

    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.

    Powerful Scripting

    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

    #!/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
    

    Next Steps

    Like what you see? There’s installation instructions in the README, and you can get a full list of commands by running

    $ pestle.phar list
    

    If you run into any problems/inefficiencies, or have command ideas, issue tickets are encouraged and welcomed.

Leave a Reply

Your email address will not be published. Required fields are marked *