## Introduction

Unless you’re a seasoned Magento pro, you may be a little confused when you set out to research the platform online. On one side you’ll find legions of developers who swear by it as the way to build internet based retail solutions. On the other side you’ll find a contingent of folks griping about Magento’s steep learning curve, relative cost, and general ease of use when compared with software as a service solutions like Shopify or Etsy.

This discrepancy boils down to one thing: Magento is not a platform built to directly serve the end-user. Instead, it’s a technology/business platform that’s designed for other businesses. Instead of setting out to build the best platform for building a web-based store, Magento Inc. (now an eBay division), focused on creating opportunities for businesses in the internet enabled retail industry (i.e. e-commerce).

Was this the right choice? That’s another topic for another book. Our concern is how these choices impact Magento’s technology stack. Magento’s a massive codebase optimized for configurability over ease of use. It’s built with PHP but with Java/C# enterprise software patterns. Magento is many things, but “My First Development Project” is not one of them.

However, for those individuals and companies willing to invest some time learning the platform, as well as learning general software development techniques, Magento offers unprecedented power and control over the online retail experience. Additionally, there’s an always growing online community of open source developers using the platform and sharing techniques. Not even the cutthroat world of e-commerce can stop the power of open source software from growing and spreading.

### What This Book Is

No Frills Command Line Magento is two things.

First, it’s a primer/manual for the popular and powerful n98-magerun tool. This command line tool automates and simplifies many of Magento’s administrative tasks, and is a no brainer if you’re handling day to day IT tasks related to Magento administration.

Secondly, No Frills Command Line Magento is, in a stealthy way, a survey of modern PHP development. In teaching you about n98-magerun, you’ll also get a high level overviews of

• PHP phar Archives
• PHP Composer
• PHP Namespaces
• PHP Phing
• Symfony’s Console Application Framework
• Unix Shell Scripting

Beyond learning about these important development topics, the process of installing, building, and running n98-magerun will give you a methodology for approaching any new PHP project. No Frills Command Line Magento is a great, (and quick), read for those developers looking to hop on the programming track and take a step up from the world of entry level blue collar development.

This book is for PHP programmers of all experience levels. If you can install Magento and run ls from the terminal, there’s something for you in this book.

### About the Author and Publisher

No Frills Command Line Magento is authored by Alan Storm. Alan (that’s me) is the owner of Pulse Storm, a small technology publishing company focused on improving the working lives of the programmers who do the work that makes the web run.

His first book, No Frills Magento Layout, remains the go to resource for developers learning Magento’s Layout system, and Pulse Storm’s Magento debugging extension Commerce Bug has been saving Magento developer’s (tempeh) bacon for years.

### Continue the Conversation Online

In 2013 no development book is ever done. If you have any questions about the content, noticed a typo, or just want to see what others are saying about the book online, checkout the discussion forum at

https://leanpub.com/command-line-magento/feedback

If there’s a concern about the the book you’d like to share privately, or you’d just like to say hi, please contact Pulse Storm directly.

http://www.pulsestorm.net/contact-us/

The “No Frills” book series is all about getting you the information you need to get your job done while teaching you a thing or two along the way about the bigger picture. Shorter than traditional technology books, by sticking to electronic delivery we avoid the problems with padding and frills associated with physical publishing.

### Disclaimers

 1 THIS BOOK AND SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 2 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 3 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 4 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 5 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 6 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 7 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 8 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 9 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
10 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
11 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
12 THE USE OF THIS BOOK AND SOFTWARE, EVEN IF ADVISED OF THE
13 POSSIBILITY OF SUCH DAMAGE.


## Built In Shell Commands

Buy the full book for this chapter!

## Installing and Running n98-magerun

There’s a few different ways to install n98-magerun. While you can clone the github repository, it may be easier to download the latest magerun.phar directly

1 https://raw.github.com/netz98/n98-magerun/master/n98-magerun.phar


You can download this phar file via a web browser, or, if you’re already CLI savvy, with programs like curl or wget.

1 wget https://raw.github.com/netz98/n98-magerun/master/n98-magerun.phar
2 curl -o n98-magerun.phar https://raw.github.com/netz98/n98-magerun/master/n98-ma\
3 gerun.phar


After downloading the file, you can test it by running the following command from a terminal window

1 $php n98-magerun.phar  This should result in help output similar to the following.  1 $ php n98-magerun.phar
 2
 3      ___ ___
 4  _ _/ _ ( _ )___ _ __  __ _ __ _ ___ _ _ _  _ _ _
 5 | ' \_, / _ \___| '  \/ _ / _ / -_) '_| || | ' \
 6 |_||_/_/\___/   |_|_|_\__,_\__, \___|_|  \_,_|_||_|
 7                            |___/
 8 n98-magerun version 1.61.3 by netz98 new media GmbH
 9
10 Usage:
11   [options] command [arguments]
12
13 Options:
14   --help           -h Display this help message.
15   --quiet          -q Do not output any message.
16   --verbose        -v Increase verbosity of messages.
17   --version        -V Display this application version.
18   --ansi              Force ANSI output.
19   --no-ansi           Disable ANSI output.
20   --no-interaction -n Do not ask any interactive question.
21
22 Available commands:
23   help                             Displays help for a command
24   install                          Install magento
25   list                             Lists commands
26   mysql-client                     Opens mysql client by database config from lo\
27 cal.xml
28   open-browser                     Open current project in browser (experimental)
29   self-update                      Updates n98-magerun.phar to the latest versio\
30 n.
31   selfupdate                       Updates n98-magerun.phar to the latest versio\
32 n.
33   shell                            Runs n98-magerun as shell
34   uninstall                        Uninstall magento (drops database and empties\
35  current folder
36
37 [... commands ommited ...]
38
39 extension
40   extension:download               Download magento-connect package
41   extension:install                Install magento-connect package
42   extension:list                   List magento connection extensions
43   extension:search                 List magento connection extensions
44   extension:upgrade                Upgrade magento-connect package
45
46 [... commands ommited ...]


This help output lists all the commands available to an n98-magerun user. You can retrieve detailed help for a specific command by passing in help as the first parameter to the CLI program. For example if you wanted help on the extension:list command, just enter

1 $php n98-magerun.phar help extension:list  and you’ll see something like the following  1 Usage:  2 extension:list [search]  3  4 Aliases: extension:search  5 Arguments:  6 search Search string  7  8 Options:  9 --help (-h) Display this help message. 10 --quiet (-q) Do not output any message. 11 --verbose (-v) Increase verbosity of messages. 12 --version (-V) Display this application version. 13 --ansi Force ANSI output. 14 --no-ansi Disable ANSI output. 15 --no-interaction (-n) Do not ask any interactive question.  While seemingly terse, the value in these help entries is the list of arguments for the command. For example, reading the above we see that extension:list has a search argument, which will allow us to list specific Magento extensions. We’ll come back to this in a bit, but first we need to learn how to run commands ### Running n98-magerun Commands Let’s try running the extension:list command from above. To run a command, just pass it in as the first argument. The command is the entire colon separated string. If you try running 1 $ php n98-magerun.phar extension:list


you’ll probably see the following output.

1   [RuntimeException]
2   Magento folder could not be detected
3
4 extension:list [search]


Drat! An error. The first thing you need to know about running n98-magerun commands is they need to be run from within your Magento folder.

1 $cd /path/to/magento 2 $ php /path/to/n98-magerun.phar extension:list


This time you should see a huge list of extensions output to your screen.

1 +-----------------------------------------+--------------------+--------+
2 | Package                                 | Version            | Stabi  |
3 +-----------------------------------------+--------------------+--------+
4 | Lib_Google_Checkout                     | 1.5.0.0            | stable |
5 | ... almost 4,000 extensions snipped ... |                    |        |
6 | Contacts_Captcha                        | 0.1.0              | stable |
7 +-----------------------------------------+--------------------+--------+


Success! You’ve successfully run your first n98-magerun command.

You don’t need to be at the top of your Magento directory hierarchy to run commands. The n98-magerun program will correctly detect your Magento folder no matter how deep you are — even if the folders are symlinks. For example, all of the following commands will work

1 $cd /path/to/magento/app/code/community 2 $ php /path/to/n98-magerun.phar extension:list
3
4 $cd /path/to/magento/app/design/frontend 5 $ php /path/to/n98-magerun.phar extension:list


### Command Arguments

Remember the help text we saw earlier?

1 Usage:
2  extension:list [search]
3
4 Aliases: extension:search
5 Arguments:
6  search                Search string


This tells us n98-magerun has a single search parameter, which means running the command with Mage as the second parameter

1 $php /path/to/n98-magerun.phar extension:list Mage  will filter out any extension that doesn’t have Mage in the name. In other words, it will search for extensions with Mage in the name. It’s often necessary to make intuitive leaps like this when working with command line tools. Interpreting phrases like Search string may seem frustrating at first, but every command line tool has its own logic and culture. After a few days of use you’ll start to get the feel for these sorts of intuitive jumps, and there’s always communities like the Magento Stack Exchange to help out if you get stuck. ### Unix Shell Scripting 101 The command we’ve been using to run n98-magerun 1 $ php /path/to/n98-magerun.phar extension:list Mage


is a little verbose. Fortunately, the unix command line gives us the power to simplify command strings. Let’s take some steps to make our n98-magerun command strings as simple as any other command line program.

The first step is getting rid of the leading php. It’s possible to run PHP scripts as plain old *nix* executables by making them executable. Use the chmod command to do this.

1 $chmod +x /path/to/n98-magerun.phar  Here we’re using the chmod command to “add” (the +) the “executable bit” (the x) to the file /path/to/n98-magerun.phar. For those new to *nix, this tells the system it’s allowed to run (or “execute”) this file as a program. After doing the above, we’ll be able to run n98-magerun with the following 1 $ /path/to/n98-magerun.phar extension:list


For experienced PHP folks who are interested, this works because the first line of the phar archive is #!/usr/bin/env php.

### Removing the Path

Next up for our simplification is the problem of needing to traverse the full /path/to/n98-magerun.phar. There’s a few options here — we’re going to talk about *nix aliasing, but for the more experienced folks there’s no reason you can’t copy the phar into a location your $PATH can see, add the /path/to/n98-magerun.phar directory to your $PATH, or use a symlink.

The alias command allows you to assign a shorter, simpler command string to run a larger, more complex program. If you run the following from your shell.

1 $alias n98-magerun="/path/to/n98-magerun.phar"  you’ll then be able to run the /path/to/n98-magerun.phar command via the shorter n98-magerun alias. 1 $ n98-magerun


This will only last as long as the current terminal window is open. To have this alias run automatically whenever you open a terminal window, you’ll need to add a line to the your shell’s startup profile. This is probably the file .bash_profile, and if it isn’t it means you’re running a shell other than bash. Google around for instructions for your particular shell.

Here’s a quick one liner to add this alias to your bash profile

1 $printf "\nalias n98-magerun=\"/path/to/n98-magerun.phar\"\n" >> ~/.bash_profile  This command automatically appends the alias command to the .bash_profile file in your home directory. The above command is actually two commands. The first is a simple printf statement. If you execute this by itself, you’ll see the following 1 $ printf "\nalias n98-magerun=\"/path/to/n98-magerun.phar\"\n"
2
3 alias n98-magerun="/path/to/n98-magerun.phar"


That is, the command string alias n98-magerun="/path/to/n98-magerun.phar" is output the screen. “The screen” is sometimes refereed to as “standard output”. Next, and most importantly, we use unix redirection (>>) to append the output of the command to the file ~/.bash_profile.

1 [command] >> ~/.bash_profile


The ~ character is a shortcut that means “the current user’s home directory”. This sort of redirection is incredibly powerful, and while you won’t need to use it with n98-magerun, mastering it is a valuable skill to pickup if you’ll be using the unix command line.

If that’s all a little fancy, you could also open ~/.bash_profile with your favorite text editor and add the alias command yourself to the end.

1 $vim ~/.bash_profile  Regardless of how it got there, with this alias in place you should be able to open a terminal window and run the n98-magerun command with a single word 1 $ n98-magerun
2 \$ n98-magerun extension:list


We’re now ready to move on and start exploring the abundance of commands available to us with n98-magerun.

## Command Review

Buy the full book for this chapter!

## Development Environment Setup

Buy the full book for this chapter!

## Understanding PHAR

Buy the full book for this chapter!

## Hello World

Buy the full book for this chapter!

## Writing Tests

Buy the full book for this chapter!

## Test Driven Command Development

Buy the full book for this chapter!