Magerun for Magento 2 | News | PHPro
What's new in our world

Magerun for Magento 2

22 November 2016

At the end of October the second Yireo Magento 2 Seminar was held at Utrecht, the Netherlands. There were 2 tracks: one for developers and one for merchants. In these tracks each speaker had about 20 minutes to give their presentation which meant a lot of topics were covered. One of these talks was about Magerun2 presented by Christian Münch, Magento lead developer at @netz98 and creator of n98-magerun.

For those how do not know Magerun, it's a convenient command line tool for Magento developers, sysadmins and devops. And it's also easy to extend which gives you a ton of possibilities. Me and many of my colleagues use Magerun for Magento 1 in every single project.  

Now I must say when I learned that Magerun was also available for Magento 2 I was sceptical at first. Magento 2 has a build in cli tool unlike Magento 1. Will Magerun still be as convenient that I will use it in all my projects as I do for Magento 1?

Lets begin by having a look at what cli commands Magento 2 delivers out of the box. To list all the available commands enter the following command in your Magento root.

bin/magento list

On a Magento 2 version 2.1.2 you get a bit over 60 commands. These will perform tasks like installing Magento, flushing caches, running indexes, showing dependencies between modules and many more.

After installing Magerun (it's easy, just follow their instruction), use the command below to list the available options. Unlike the Magento cli tool you can use the command in any location. It doesn't have to be in your Magento root.

n98-magerun2.phar list

With Magerun you have acces to about 60 extra commands on top of the the original Magento commands (which can also be used with Magerun)!

n98-magerun2 version 1.2.2 by netz98 new media GmbH
 
Usage:
 command [options] [arguments]
 
Options:
 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question
 --root-dir            Force magento root dir. No auto detection
 --skip-config         Do not load any custom config.
 --skip-root-check     Do not check if n98-magerun runs as root
 --skip-core-commands  Do not include Magento 2 core commands
 
Available commands:
 help                                      Displays help for a command
 install                                   Install magento
 list                                      Lists commands
 mysql-client                              Opens mysql client by database config from env.php
 open-browser                              Open current project in browser (experimental)
 script                                    Runs multiple n98-magerun commands
 self-update                               Updates n98-magerun.phar to the latest version.
 selfupdate                                Updates n98-magerun.phar to the latest version.
 shell                                     Runs n98-magerun as shell
admin
 admin:user:change-password                Changes the password of a adminhtml user.
 admin:user:create                         Creates an administrator
 admin:user:delete                         Delete the account of a adminhtml user.
 admin:user:list                           List admin users.
 admin:user:unlock                         Unlock Admin Account
cache
 cache:clean                               Clean magento cache
 cache:disable                             Disables Magento caches
 cache:enable                              Enables Magento caches
 cache:flush                               Flush magento cache storage
 cache:list                                Lists all magento caches
 cache:status                              Checks cache status
catalog
 catalog:images:resize                     Creates resized product images
 catalog:product:attributes:cleanup        Removes unused product attributes.
config
 config:delete                             Deletes a store config item
 config:get                                Get a core config item
 config:set                                Set a core config item
cron
 cron:run                                  Runs jobs by schedule
customer
 customer:create                           Creates a new customer/user for shop frontend.
 customer:hash:upgrade                     Upgrade customer's hash according to the latest algorithm
 customer:info                             Loads basic customer info by email address.
 customer:list                             Lists all magento customers
db
 db:console                                Opens mysql client by database config from env.php
 db:create                                 Create currently configured database
 db:drop                                   Drop current database
 db:dump                                   Dumps database with mysqldump cli client according to informations from env.php
 db:import                                 Imports database with mysql cli client according to database defined in env.php
 db:info                                   Dumps database informations
 db:maintain:check-tables                  Check database tables
 db:query                                  Executes an SQL query on the database defined in env.php
 db:status                                 Shows important server status information or custom selected status values
 db:variables                              Shows important variables or custom selected
deploy
 deploy:mode:set                           Set application mode.
 deploy:mode:show                          Displays current application mode.
dev
 dev:console                               Opens PHP interactive shell with initialized Mage::app() (Experimental)
 dev:module:create                         Create and register a new magento module.
 dev:module:list                           List all installed modules
 dev:module:observer:list                  Lists all registered observers
 dev:report:count                          Get count of report files
 dev:source-theme:deploy                   Collects and publishes source files for theme.
 dev:symlinks                              Toggle allow symlinks setting
 dev:template-hints                        Toggles template hints
 dev:template-hints-blocks                 Toggles template hints block names
 dev:tests:run                             Runs tests
 dev:theme:list                            Lists all available themes
 dev:urn-catalog:generate                  Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.
 dev:xml:convert                           Converts XML file using XSL style sheets
eav
 eav:attribute:view                        View information about an EAV attribute
generation
 generation:flush                          Flushs generated code like factories and proxies
i18n
 i18n:collect-phrases                      Discovers phrases in the codebase
 i18n:pack                                 Saves language package
 i18n:uninstall                            Uninstalls language packages
index
 index:list                                Lists all magento indexes
indexer
 indexer:info                              Shows allowed Indexers
 indexer:reindex                           Reindexes Data
 indexer:reset                             Resets indexer status to invalid
 indexer:set-mode                          Sets index mode type
 indexer:show-mode                         Shows Index Mode
 indexer:status                            Shows status of Indexer
info
 info:adminuri                             Displays the Magento Admin URI
 info:backups:list                         Prints list of available backup files
 info:currency:list                        Displays the list of available currencies
 info:dependencies:show-framework          Shows number of dependencies on Magento framework
 info:dependencies:show-modules            Shows number of dependencies between modules
 info:dependencies:show-modules-circular   Shows number of circular dependencies between modules
 info:language:list                        Displays the list of available language locales
 info:timezone:list                        Displays the list of available timezones
maintenance
 maintenance:allow-ips                     Sets maintenance mode exempt IPs
 maintenance:disable                       Disables maintenance mode
 maintenance:enable                        Enables maintenance mode
 maintenance:status                        Displays maintenance mode status
module
 module:disable                            Disables specified modules
 module:enable                             Enables specified modules
 module:status                             Displays status of modules
 module:uninstall                          Uninstalls modules installed by composer
sampledata
 sampledata:deploy                         Deploy sample data modules
 sampledata:remove                         Remove all sample data packages from composer.json
 sampledata:reset                          Reset all sample data modules for re-installation
script
 script:repo:list                          Lists all scripts in repository
 script:repo:run                           Run script from repository
setup
 setup:backup                              Takes backup of Magento Application code base, media and database
 setup:config:set                          Creates or modifies the deployment configuration
 setup:cron:run                            Runs cron job scheduled for setup application
 setup:db-data:upgrade                     Installs and upgrades data in the DB
 setup:db-schema:upgrade                   Installs and upgrades the DB schema
 setup:db:status                           Checks if DB schema or data requires upgrade
 setup:di:compile                          Generates DI configuration and all missing classes that can be auto-generated
 setup:install                             Installs the Magento application
 setup:performance:generate-fixtures       Generates fixtures
 setup:rollback                            Rolls back Magento Application codebase, media and database
 setup:static-content:deploy               Deploys static view files
 setup:store-config:set                    Installs the store configuration
 setup:uninstall                           Uninstalls the Magento application
 setup:upgrade                             Upgrades the Magento application, DB data, and schema
sys
 sys:check                                 Checks Magento System
 sys:cron:history                          Last executed cronjobs with status.
 sys:cron:list                             Lists all cronjobs
 sys:cron:run                              Runs a cronjob by job code
 sys:info                                  Prints infos about the current magento system.
 sys:maintenance                           Toggles maintenance mode if --on or --off preferences are not set
 sys:setup:change-version                  Change module resource version
 sys:setup:compare-versions                Compare module version with setup_module table.
 sys:setup:downgrade-versions              Automatically downgrade schema and module versions
 sys:store:config:base-url:list            Lists all base urls
 sys:store:list                            Lists all installed store-views
 sys:url:list                              Get all urls.
 sys:website:list                          Lists all websites
theme
 theme:uninstall                           Uninstalls theme

During his talk, Christian Münch showed one command in particular which caught my attention: 

n98-magerun2.phar dev:console

This will open an interactive shell with a Magento application initialised. You can use this to quickly test run some code. But you can also use it to generate controllers, models, blocks, etc. for your modules, including stubs for the methods! 

For example, I have a module Phpro_Supplier (which you can also generate using Magerun) and I'd like to add a controller for the create action:

>>> module Phpro_Supplier
Use module Phpro_Supplier
Module: Phpro_Supplier >>> make:controller supplier.create
generated Controller/Supplier/Create.php

When I look in my Magento directory I'll find a new file app/code/Phpro/Supplier/Controller/Supplier/Create.php. The content of this file is:

<?php
 
namespace Phpro\Supplier\Controller\Supplier;
 
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
 
class Create extends Action
{
 
    /**
     * Dispatch request
     *
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function execute()
    {
        return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    }
 
 
}

And now you're all set to implement the action! I invite you to go look at the original presentation for more examples.

To conclude, after reviewing just a tiny bit of Magerun2 I'm certain that I'll use it in every single Magento 2 project.

Share