August, 2007


31
Aug 07

Using PHP for Code Generation

I’ve been using PHP lately to write the server-side of our product. One of the nice mechanics of PHP is that a PHP file is actually a text file that is output as it is read and the PHP code is simply escaped/executed… in other words, PHP is a templating language. While that’s not news for most folks, it hadn’t really hit home with me that because PHP is a templating language its an excellent tool to use for doing code generation. So, if its good enough for generating HTML pages then its certainly good enough for generating ActionScript source files! ;-)

A month or so ago I went looking for a command-line tool to take a list of assets (PNGs, MP3s) and make an SWF library out of them. My first stop was the excellent Swfmill. Unfortunately for me, Swfmill only supports up to version 8, and I’m using 9, and the last thing I wanted was to have to cross between AS’s two virtual machines just for loading assets.

I posed the question to the excellent Flexcoders group. I promptly got a suggestion from Alex Hurai (member of the Flex team at Adobe) that I could just do a bit of codegen to create an AS class full of embedded assets (using Flex’s embed metadata). So that’s what I did!

I’ll post the code sometime this weekend, once I’ve tested it completely and cleaned it up a bit, but it seems to work. I did a bit of reading on how the module loading code for Flex was written to give me a solid basis for how to handle things. Basically, I generate a class that implements my own IAssetLibrary interface, which is basically an interface that provides methods for querying and retrieving the embedded assets. The PHP codegen simply scans a specified directory, grabs the appropriate filenames, and populates a standard AS3 class. It then populates a few arrays (mapping filenames to embedded asset classes) and its done.

The way PHP-on-the-command-line works this just all spews out to stdout, so I just wrapped it all up in batch file that pipes things to .as file, then calls mxmlc (ActionScript3/Flex compiler) on that .as file. The result (after a very long time … mxmlc is dog slow because it has to fire up the Java VM, etc.) is a SWF with a single class in it, a Sprite (because root classes in an SWF have to inherit from Sprite or MovieClip) that implements the IAssetLibrary interface. I can then use the Flash API’s Loader class to load the SWF, cast it to IAssetLibrary, then retrieve my assets by filename!

The big reason for doing this is that our current build loads each asset individually from a URL (like a web browser rendering a webpage). This was entirely by-design (modeled after a web browser), but in practice (after many months of design revisions) we no longer require that degree of flexibility. And it comes with a cost: the current build makes 228 separates GETs to our server to gather all the assets, most of which are in the 5-10k ballpark. That’s a lot of network overhead. And code overhead, in both my code to manager all those requests and the Flash Player dealing with all those async calls.

And magically, an SWF compresses PNGs/MP3s smaller than just the raw files (elimination of redundant headers or something, I’m not sure)… the total weight of all the assets is 300k smaller (6.5MB compared to 6.8MB).


21
Aug 07

Piece of Cake…

CakePHP that is. Think Ruby on Rails is cool, but maybe too cool? Have a LAMP setup for your websites and don’t want to mess with shell access? This is the right framework for you.

I know a very small bit of PHP. It’s close enough to C (affectionately referred to as a “curly bracket” language) that I can read it, but I’m a bit short in vocabulary (you know, common idioms, standard API’s, etc.). I also know a small bit about how web backends are written, but I’ve never done it professionally so I don’t know on the ins and outs of how best to do redirects, or handle sessions and cookies, or sanitize data for SQL. CakePHP handles most all of that for me.

How cool is it? I wrote the website for playMockingbird.com this weekend. That’s all while learning CakePHP, taking a refresher course in PHP itself, designing the site, doing the CSS, and writing the web services for our tool to interact with. CakePHP helped with all of that and I finished the job in about 20 hours.

If I started from scratch right now I could probably do it in 10. If I had any decent amount of experience with PHP and CSS (you know, like if I was a professional web developer) I could do it in 5, or maybe less… hell, it could even be a screencast demosntrating it in under an hour.

CakePHP makes it that dead simple.

And the professional web “firms” out there should be ashamed of themselves. We paid out an exorbitant amount of money (you don’t want to know) to hire a good firm here in Austin (I won’t drop names) to build our site. Several months later we had a few decent page designs and some functionality. We pulled the plug and took everything they’d built. Digging through it was a nightmare. I’m sure it made sense to the guy writing it as it looked to be built on some kind of standard framework… but not one I’d seen before (or heard of). And it didn’t work very uniformly.

If they had used CakePHP, in the 100+ engineering hours they billed us for they would have been able to build out the whole website, early and agile (which was the real reason we had to part ways … too old school, not very agile, they were accustomed to building websites for lawyers and doctors and local businesses, not quite ready for the web2.0 world).

So now we changed our job request: it used to be looking for a good web developer with strong PHP chops (or RoR), etc. Now all I want is a good designer, someone who can layout a good HTML page, do good CSS, knock together functional designs in Photoshop… I’ll take care of the engineering.

Do yourself a favor. Go have a piece of cake. Knock up a nice little web 2.0 app this weekend… I did.