Categories


Archives


Recent Posts


Categories


N98-magerun: Development Commands Part 2

astorm

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!

This article is part of a longer series covering the n98-magerun power tool

Last time we covered the toggle related commands under the dev category. Today we’ll be covering the remaining development commands

dev:console                    Opens PHP interactive shell with initialized Mage::app() (Experimental)
dev:ide:phpstorm:meta          Generates meta data file for PhpStorm auto completion
dev:log:db                     Turn on/off database query logging
dev:log:size                   Get size of log file
dev:module:create              Creates an registers new magento module.
dev:module:list                List all installed modules
dev:module:observer:list       Lists all registered observers
dev:module:rewrite:conflicts   Lists all magento rewrite conflicts
dev:module:rewrite:list        Lists all magento rewrites
dev:report:count               Get count of report files
dev:theme:duplicates           Find duplicate files in your theme
dev:theme:list                 Lists all available themes

Let’s get started.

Miscellaneous Commands

The dev:console command purports to offer an interactive shell for Magento PHP programming. However, the feature is labeled as experimental, and currently has some blocking issues. That’s enough to put it on the skip list for now.

The dev:ide:phpstorm:meta command is for users of the PHP Storm IDE, (no relation)

$ n98-magerun.phar dev:ide:phpstorm:meta
Generated definitions for blocks group
Generated definitions for helpers group
Generated definitions for models group
Generated definitions for resource models group
Generated definitions for resource helpers group
File .phpstorm.meta.php generated

This command will generate a PHP Storm meta-data file that allows the IDE to auto-complete code, intellisense style. You can read about these meta-data files on the JetBrains site. I’m more a crusty old ctags crank, so that’s all I know about that.

Logging Commands

The dev:log:db command might seem like it belongs in our toggle article, but it’s not a system configuration toggle. Instead, this toggles database logging on or off.

What, what?! Magento has database logging? Let’s run the command

$ n98-magerun.phar dev:log:db
Looking in /path/to/magento/lib/Varien/Db/Adapter/Pdo/Mysql.php
Changed $_debug to true
Changed $_logAllQueries to true
Done. You can tail var/debug/pdo_mysql.log

Ah ha. The Varien_Db_Adapter_Pdo_Mysql class has two protected properties, $_debug and $_logAllQueries. These properties are used to toggle logging to the var/debug/pdo_mysql.log file. However, Magento doesn’t offer a straightforward “public” programming interface to toggle these values. So, the dev:log:db command modifies the core Varien_Db_Adapter_Pdo_Mysql library file to toggle these values to true.

You’ll need to make the decision on whether to use this command yourself. Personally, I wouldn’t use it outside of my development environment. The code that modifies this files is solid, but it’s regular expression based, and may blow up when it encounters an unexpected input file. This is less a comment on the skill of the Netz98 developers (which is legion), and more a comment on the seductive yet destructive powers of the regular expressions elder gods.

Next up is the dev:log:size command. This command can be used to monitor the size of your var/log/system.log, var/log/exception.log, and pdo_mysql.log files.

$ n98-magerun.phar dev:log:size 
[1] exception.log
[2] system.log
[3] pdo_mysql.log
Please select a log file: 2
14438845

The file size will be listed in bytes. You’ll need to do some maths, or have google do them, to get a human readable file size ouf of the command.

The dev:report:count command is a similar reporting command. This one will tell you how many “report” logs Magento has generated.

$ n98-magerun.phar dev:report:count

Magento “reports” are PHP exceptions that, for some reason or another, couldn’t be caught in the main application try/catch block that logs to exception.log. Instead, Magento will log these exceptions to var/reports/, one exception per file.

One possible use of the dev:report:count command would be in a cron to monitor this folder. Another would be as a generic diagnostic step to see what’s going on with an unfamiliar system.

Theme Commands

There’s two commands under the dev:theme category, dev:theme:list and dev:theme:duplicates. The dev:theme:list command will show you all the themes currently installed into your Magento system.

$ n98-magerun.phar dev:theme:list
+-----------------+
| name            |
+-----------------+
| base/default    |
| default/blank   |
| default/default |
| default/iphone  |
| default/modern  |
+-----------------+

The results are in the standard package-name/theme-name format.

The dev:theme:duplicates command is used for theme cleanup. Given two themes, it will tell you if they have identical theme files.

$ n98-magerun.phar dev:theme:duplicates default/default
/path/to/magento/app/design/frontend/default/default/layout/customer.xml

$ n98-magerun.phar dev:theme:duplicates default/default base/default
/path/to/magento/app/design/frontend/default/default/layout/customer.xml    

In the above example, n98-magerun is telling us the default/default theme has an identical customer.xml file to the base/default theme (if omitted, the second argument defaults to base/default). This command is mainly useful in cleaning up old “pre base design package” themes where the original default theme’s files were used as the starting point.

Module Commands

The last category of commands we’ll cover today are those under the dev:module hierarchy. These are commands to help you with Magento’s strict module system. That’s module as in Magento’s framework modules, and not Magento Connect extensions. Magento Connect extensions are collections of files, Magento modules are code files organized in a special way such that the framework addresses them as a unit.

First, dev:module:list will list every module in the system.

$ n98-magerun.phar dev:module:list

  Magento Modules

+-----------+--------------------------------------+------------+----------+
| codePool  | Name                                 | Version    | Status   |
+-----------+--------------------------------------+------------+----------+
| core      | Mage_Core                            | 1.6.0.2    | active   |
| core      | Mage_Eav                             | 1.6.0.0    | active   |
| core      | Mage_Page                            | 1.6.0.0    | active   |
| core      | Mage_Install                         | 0.7.0      | active   |
| core      | Mage_Admin                           | 1.6.1.0    | active   |
| core      | Mage_Rule                            | 1.6.0.0    | active   |
| core      | Mage_Adminhtml                       | 0.7.1      | active   |
| core      | Mage_AdminNotification               | 1.6.0.0    | active   |
| core      | Mage_Cron                            | 1.6.0.0    | active   |
| core      | Mage_Directory                       | 1.6.0.1    | active   |
| core      | Mage_Dataflow                        | 1.6.0.0    | active   |
| core      | Mage_Cms                             | 1.6.0.0.1  | active   |
| core      | Mage_Index                           | 1.6.0.0    | active   |
| core      | Mage_Customer                        | 1.6.2.0.1  | active   |
| core      | Mage_Catalog                         | 1.6.0.0.14 | active   |
| core      | Mage_CatalogRule                     | 1.6.0.3    | active   |
| core      | Mage_CatalogIndex                    | 1.6.0.0    | active   |
| core      | Mage_CatalogSearch                   | 1.6.0.0    | active   |
| core      | Mage_Payment                         | 1.6.0.0    | active   |
| core      | Mage_Sales                           | 1.6.0.7    | active   |
| core      | Mage_CatalogInventory                | 1.6.0.0.2  | active   |
| core      | Mage_Shipping                        | 1.6.0.0    | active   |
| core      | Mage_SalesRule                       | 1.6.0.3    | active   |
| core      | Mage_Usa                             | 1.6.0.1    | active   |
| core      | Mage_Paygate                         | 1.6.0.0    | active   |
| core      | Mage_Checkout                        | 1.6.0.0    | active   |
| core      | Mage_Backup                          | 1.6.0.0    | active   |
| core      | Mage_Paypal                          | 1.6.0.2    | active   |
| core      | Mage_Poll                            | 1.6.0.0    | active   |
| core      | Mage_GoogleCheckout                  | 1.6.0.1    | active   |
| core      | Mage_Log                             | 1.6.0.0    | active   |
| core      | Mage_Review                          | 1.6.0.0    | active   |
| core      | Mage_Rating                          | 1.6.0.0    | active   |
| core      | Mage_Widget                          | 1.6.0.0    | active   |
| core      | Mage_Tag                             | 1.6.0.0    | active   |
| core      | Mage_Reports                         | 1.6.0.0.1  | active   |
| core      | Mage_GoogleAnalytics                 | 1.6.0.0    | active   |
| core      | Mage_Tax                             | 1.6.0.3    | active   |
| core      | Mage_Wishlist                        | 1.6.0.0    | active   |
| core      | Mage_PaypalUk                        | 1.6.0.0    | active   |
| core      | Mage_Media                           | 1.6.0.0    | active   |
| core      | Mage_GiftMessage                     | 1.6.0.0    | active   |
| core      | Mage_Contacts                        | 1.6.0.0    | active   |
| core      | Mage_Sendfriend                      | 1.6.0.0    | active   |
| core      | Mage_Sitemap                         | 1.6.0.0    | active   |
| core      | Mage_Rss                             | 1.6.0.0    | active   |
| core      | Mage_ProductAlert                    | 1.6.0.0    | active   |
| core      | Mage_Api                             | 1.6.0.0    | active   |
| core      | Mage_Oauth                           | 1.0.0.0    | active   |
| core      | Mage_Authorizenet                    | 1.5.0.0    | active   |
| core      | Mage_Bundle                          | 1.6.0.0.1  | active   |
| core      | Mage_Captcha                         | 1.7.0.0.0  | active   |
| core      | Mage_Centinel                        | 1.6.0.0    | active   |
| core      | Mage_Compiler                        | 1.6.0.0    | active   |
| core      | Mage_Connect                         | 1.6.0.0    | active   |
| core      | Mage_Newsletter                      | 1.6.0.1    | active   |
| core      | Mage_Downloadable                    | 1.6.0.0.2  | active   |
| core      | Mage_ImportExport                    | 1.6.0.2    | active   |
| core      | Mage_Api2                            | 1.0.0.0    | active   |
| core      | Mage_PageCache                       | 1.6.0.0    | active   |
| core      | Mage_Persistent                      | 1.0.0.0    | active   |
| core      | Mage_Weee                            | 1.6.0.0    | active   |
| core      | Mage_CurrencySymbol                  | 1.0.0.0.0  | active   |
| core      | Mage_XmlConnect                      |            | inactive |
| local     | Alanstormdotcom_Commercebug          | 2.0.0      | active   |
| local     | Alanstormdotcom_Developermanual      | 1.1.2      | active   |
| local     | Alanstormdotcom_Systemsearch         | 0.1.0      | active   |
| local     | Alphacard_MageCatalogBlockNavigation |            | inactive |
| community | Belvg_Devhelper                      | 1.0.0      | active   |
| community | Fishpig_AttributeSplash              | 2.2.0.6    | active   |
| community | IG_LightBox2                         |            | active   |
| community | Insidetm_Integration                 | 0.1.0      | active   |
| local     | Nofrills_Booklayout                  | 0.1.0      | active   |
| community | Phoenix_Moneybookers                 | 1.6.0.0    | active   |
| community | Pulsestorm_Additemgrid               | 0.1.0      | active   |
| community | Pulsestorm_Chaos                     |            | active   |
| community | Pulsestorm_Contenttypes              | 0.1.0      | active   |
| community | Pulsestorm_Customerpage              | 0.1.0      | active   |
| community | Pulsestorm_Eschewxml                 | 0.1.0      | active   |
| community | Pulsestorm_Helloworld2               | 0.1.0      | active   |
| community | Pulsestorm_Launcher                  | 1.0.3      | active   |
| community | Pulsestorm_Modulelist                | 0.2.2      | active   |
| community | Pulsestorm_Requestset                | 0.1.0      | active   |
| community | Pulsestorm_Setuptest                 | 0.1.0      | active   |
| community | ST_Bettercms                         | 1.0.0.1    | active   |
| local     | Sp_Ajaxify                           | 0.1.0      | active   |
| community | Substance_Teamproducts               | 0.1.0      | active   |
| community | Wish_Scheduleproduct                 |            | inactive |
+-----------+--------------------------------------+------------+----------+

Next, the dev:module:observer:list command will list any observers configured in the system. Observers are code added to the system that listen for specific Magento events. Observer based programming allows you to add functionality to Magento without the need to change any Magento code.

The dev:module:observer:list command requires that you specify which configuration “area” (global, adminhtml, or frontend) you’d like to search in

$ n98-magerun.phar dev:module:observer:list
[1] global
[2] adminhtml
[3] frontend
Please select a area:1

  Observers: global                           
┌────────────────────────────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────────────────────────┐
│adminhtml_controller_action_predispatch_start                                   │adminhtml/observer::bindStore                                                   │
│                                                                                │adminhtml/observer::massactionPrepareKey                                        │
├────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤
│admin_user_authenticate_after                                                   │adminhtml/observer::clearCacheConfigurationFilesAccessLevelVerification         │
├────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤
etc ...

The report will list out the event name (adminhtml_controller_action_predispatch_start) as well as the model-name::method-name (adminhtml/observer::bindStore) of each observer.

The dev:module:rewrite:list command will list all configured Magento class rewrites in the system. For those new to the system, Magento has a class rewrite system which allows you to replace core Magento model classes with your own. This implements a primitive duck-typing/monkey-patching system, and can be used to modify system behavior where observers aren’t present.

$ n98-magerun.phar dev:module:rewrite:list

  Rewrites              

    +--------+---------------------------------+---------------------------------------------------------------+
    | Type   | Class                           | Rewrite                                                       |
    +--------+---------------------------------+---------------------------------------------------------------+
    | models | core/layout_update              | Alanstormdotcom_Commercebug_Model_Layout_Update               |
    | models | catalog/layer_filter_item       | Fishpig_AttributeSplash_Model_Layer_Filter_Item               |
    | models | sitemap/sitemap                 | Fishpig_AttributeSplash_Model_Sitemap                         |
    | blocks | catalog/product_view_attributes | Fishpig_AttributeSplash_Block_Catalog_Product_View_Attributes |
    | blocks | widget/adminhtml_widget         | ST_Bettercms_Block_Adminhtml_Widget                           |
    +--------+---------------------------------+---------------------------------------------------------------+

Running this command will let you know right away if you can safely rewrite a class without impacting another extension in the system. When you’ve inherited a system or just installed a new module, the dev:module:rewrite:conflicts command will give you a quick heads up as to any conflicting rewrites in the system.

$ n98-magerun.phar dev:module:rewrite:conflicts

  Conflicts  

┌────────┬──────────────────────────────┬────────────────────────────────────────────────────────────┬────────────────────────────────────────────────────────────┐
│blocks  │widget/adminhtml_widget       │Pulsestorm_Cantplaywithothers_Blocks_Adminhtml_Widget,      │ST_Bettercms_Block_Adminhtml_Widget                         │
│        │                              │ST_Bettercms_Block_Adminhtml_Widget                         │                                                            │
└────────┴──────────────────────────────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘
1 conflict was found!

For those new to Magento, the rewrite system works by replacing which class is instantiated, via a factory method. This means one rewrite always “wins out” over another. The dev:module:rewrite:conflicts command will identify these conflicts for you.

The last command we’ll mention today is dev:module:create. This command can be used to create the files and directories needed for a skeleton module

$ n98-magerun.phar dev:module:create Pulsestorm Testmodule local
Created directory: /path/to/magentoapp/code/local/Pulsestorm/Testmodule
Created directory: /path/to/magentoapp/code/local/Pulsestorm/Testmodule/etc
Created file: /path/to/magentoapp/etc/modules/Pulsestorm_Testmodule.xml
Created file: /path/to/magentoapp/code/local/Pulsestorm/Testmodule/etc/config.xml

This command takes three arguments. The first (Pulsestorm) is the module package name, the second (Testmodule) is the module name itself, and the third (local) argument is the module code pool.

This command also supports a number of arguments for creating modules of certain types, or containing certain features.

$ n98-magerun.phar help dev:module:create
Usage:
 dev:module:create [--add-blocks] [--add-helpers] [--add-models] [--add-setup] [--add-all] [--modman] [--add-readme] [--add-composer] [--author-name[="..."]] [--author-email[="..."]] [--description[="..."]] vendorNamespace moduleName [codePool]

Arguments:
 vendorNamespace       Namespace (your company prefix)
 moduleName            Name of your module.
 codePool              Codepool (local,community) (default: "local")

Options:
 --add-blocks          Adds blocks
 --add-helpers         Adds helpers
 --add-models          Adds models
 --add-setup           Adds SQL setup
 --add-all             Adds blocks, helpers and models
 --modman              Create all files in folder with a modman file.
 --add-readme          Adds a readme.md file to generated module
 --add-composer        Adds a composer.json file to generated module
 --author-name         Author for readme.md or composer.json
 --author-email        Author for readme.md or composer.json

Once you become more familiar with Magento’s various module features, the use of these options will become self evident.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 23rd April 2013

email hidden; JavaScript is required