Code Snippets

HashBang: PHP console app microframework

One of the irritating constants of the development process is the necessity of doing the same things over and over again. Every time you deploy a Symfony2 project, for example, there’s pretty much a checklist that has to be followed:

  1. Pull from the remote repo
  2. Run bin/vendors install if any of the vendor requirements have changed
  3. Update the database schema if your entities have changed
  4. Clear the opcode cache
  5. Clear Symfony2’s production cache

And if you forget a step, it takes even longer to go back and correct it. So, I wrote a handful of shell scripts that automate some common processes; namely, testing and deployment. They worked fine until coworkers start coming to me and saying things like, “I really don’t want to have to run the vendors install every time if I KNOW that the vendors haven’t changed,” or, “Your deploy script requires a manual update of the database schema; can you add a flag that I can use locally to do it automatically?”

I’m no bash scripting expert, so when I realized that it was going to take longer to figure out how to update my deployment script than it would to rewrite it as a PHP CLI script, I sat down and hacked something out quickly. Then I looked at my testing script, and found that I’d be copy-pasting a lot of the same code to deal with command-line arguments and switches. So, I came up with another solution.

HashBang is a console app microframework that I hammered out in a couple of hours one night. Its purpose is to abstract away some of common code in a CLI script — dealing with optional and required arguments, and supporting command-line switches — and let you focus on what your script should actually be doing. You simply tell your HashBang app what arguments and switches to expect, and pass it a closure to execute when you call go(). Check it out:

That’s it. Obviously, it’s not as full-featured as some of the other solutions out there might be, but it’s quick and dirty and gets the job done.

Code Snippets

Quick Symfony2 deployment script

I find myself doing the same things a lot: add a vendor to the deps file, write some code, run the vendors install, update database, etc. etc. I got tired of it, so I put together a quick deployment script. It’s not perfect, but it should get you pointed in the right direction. Enjoy!