If you'd asked me a few years ago about building command line (console) applications in PHP I'd have told you it was a crazy idea. Fast forward to the present and the situation has changed quite a bit. The versions of PHP distributed everywhere and the available PHP libraries and scripts are in a much better place. It's no longer a crazy idea to have a command line application written in PHP.
What does a PHP CLI application look like?
When I think of a good example I think of composer, the PHP package manager. You have a single file and you execute it. Sound familiar? This is quite a bit different from a few years ago where I would have to expose libraries via Pear, make sure it was configured to my include path, and have a script in an include path. The amount of work to get going was too high a barrier to entry. Times have changed.
Why Build A CLI Application in PHP?
Just because you can do something doesn't mean you should. So, there should be a good reason to use PHP. PHP is the most popular language on the Internet. This has lead to a lot of libraries being written in PHP. If these libraries are being used to develop web applications some of the same libraries can be used in CLI applications removing the need to duplicate functionality. Removing code duplication or being able to spin out CLI applications using existing libraries with a low barrier to entry is a good thing.
Let me share an example. Recently I needed to have Markdown Extra available at the command line. This is a Markdown variant that has it's best support in PHP where it originated. While Maruku supports this varient and is popular it didn't support all my needs. So, what was I to do? I spent a short amount of time and built a markdown extra cli application. Since it used the same code to do the conversation I'd been using in websites I knew exactly how the content would fare. No need to spend lots of time making sure the content came out right.
Tools To Build PHP CLI Applications
The good stuff is in the tools you can use to build CLI applications. Here's my short list (though there are others).
- Composer - this package manager handles bringing the parts together. Part of this is publically available packages in Packagist and part of it is private packages in Git repos that aren't shared. For things I'm working on that should not be public I can use private packages and they work well.
- Symfony Console - a package that is part of Symfony. You don't need all of Symfony to use it and I often use it without any other parts of Symfony. This package brings input via prompts and writing to stdout easy. It supports colors, application options, and more.
- Phar files - these are PHP archive files that can hold an entire applications. Automating the generation of these files can be fairly simple and I use PHP scripts that generate these with simple commands. You can find some examples with Goutte (an appliction written by Fabien Potencier the creator of Symfony) or with Fortissimo CLI (what I use) in the compile script and library parts.
If you are writing an application with any kind of complexity I'd suggest using a framework to handle the routing. Here are a few options:
- In the first slot I have to list what I personally use and that is Fortissimo. If you're going to try that I'd suggest checking out the Fortissimo-CLI-Base project designed to make it really easy to get started. Since I write so many one off applications I made an easy to use starting point.
- Silex is a simple framework. You can think of it as Sinatra for PHP.
- Symfony and the console application portions. This is what composer is built on.