<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>interfacelab &#187; Uncategorized</title>
	<atom:link href="http://interfacelab.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://interfacelab.com</link>
	<description></description>
	<lastBuildDate>Thu, 01 Mar 2012 16:25:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introducing makejs</title>
		<link>http://interfacelab.com/introducing-makejs/</link>
		<comments>http://interfacelab.com/introducing-makejs/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 19:17:28 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=141</guid>
		<description><![CDATA[I have recently been working on the mobile UI for a site I built called Sessions+.  Sessions+ is a poker bankroll tracker that has a companion iPhone application for live tracking during live poker games, and now a mobile UI for Android and other HTML5 capable mobile browsers which replicates the most of the important [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been working on the mobile UI for a site I built called <a href="http://sessionsplus.com" target="_blank">Sessions+</a>.  <a href="http://sessionsplus.com/" target="_blank">Sessions+</a> is a poker bankroll tracker that has a companion iPhone application for live tracking during live poker games, and now a mobile UI for Android and other HTML5 capable mobile browsers which replicates the most of the important functionality of the iPhone app.</p>
<p>The framework I used to build the app uses a lot of separate javascript and css files, though the entire app is housed in a single html file.  So I end up with a &lt;head&gt; element jam packed with stuff.  While this is perfect for development, it&#8217;s no good for deployment or QA.</p>
<p><span id="more-141"></span></p>
<p>For an example of what the &lt;head&gt; element looks like during development:</p>
<script src="http://gist.github.com/647344.js"></script>
<p>That&#8217;s a lot of stuff.  To optimize for mobile, I&#8217;m going to need to smash these files together and run them through <a href="http://developer.yahoo.com/yui/compressor/" target="_blank">yui-compressor</a> to make it as small as possible (and then serve it up with GZip to make it even smaller).</p>
<p>Now after some googling, I ran into <a href="http://cjohansen.no/en/ruby/juicer_a_css_and_javascript_packaging_tool" target="_blank">Juicer</a>.  Juicer is a great tool, but it still didn&#8217;t do everything I wanted, and specifying the dependencies within the javascript and CSS files was not as explicit as I desire.  It just seemed to prone to error.  Finally, after merging everything with Juicer, I&#8217;d have to manually update the html files by hand to replace all those script and link tags with the new ones.</p>
<p>Next up on the google list was <a href="http://ant.apache.org/" target="_blank">Apache Ant</a>.  I haven&#8217;t used Ant since doing a random Flex project a few years ago, and I was afraid it was simply too complicated to setup for my small little project.  But in my quest to avoid NIH syndrome, I gave it a spin anyhow.</p>
<p>I gave up after an hour or two as it was just too much.  Maybe I&#8217;m getting lazy in my older years, maybe I am suffering insufferable NIH syndrome, I&#8217;m not sure.  Also, I hate XML.</p>
<h3>Tick Tock</h3>
<p>So the clock is ticking down on my self-imposed deadline to get a system setup that &#8220;builds&#8221; my little HTML5 app into a production ready thing.  There were a couple of other options presented to me by google, but again, they either used the dependency thing of Juicer, or were horribly complicated.</p>
<p>So I did what any self-respecting developer would do: fired up TextMate and started hammering.</p>
<h3>Meet makejs</h3>
<p>When I set about building it, I had a few simple goals in mind:</p>
<ul>
<li>YAML based configuration files.  I love me some YAML.  I love being explicit.</li>
<li>Ability to modify HTML files to replace &lt;script&gt; and &lt;link&gt; tags</li>
<li>Generate an HTML5 cache manifest</li>
<li>Run in continuous integration mode where changes to files would trigger a re-build.</li>
</ul>
<p>So five hours later I had a working system.</p>
<h3>Using makejs</h3>
<p>makejs uses a YAML based configuration file to direct the build process.  Here is an example:</p>
<script src="http://gist.github.com/647465.js"></script>
<p>Now that&#8217;s a lot to process, but it&#8217;s pretty simple once you break it down.  The general layout for the build configuration is thus:</p>
<ul>
<li>Build Target
<ul>
<li>Version (Major.Minor.Build)</li>
<li>Pre-Requisite Build Targets</li>
<li>Build
<ul>
<li>Build Phase
<ul>
<li>Settings</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>And that&#8217;s essentially it.  But let&#8217;s look at it in more detail.</p>
<p>In the configuration above, you&#8217;ll notice I have two distinct build targets: &#8220;glamrock&#8221; and &#8220;example&#8221;.  GlamRock is the mobile framework I built, Example is a sample project that uses GlamRock.  For the GlamRock build target, you&#8217;ll see that I have 3 build phases: copy, compress and manifest.</p>
<p>The copy build phase does exactly as it sounds, it copies files to a destination in the build directory.  Nothing more, nothing less.  In this case, I like to keep jQuery and jQuery&#8217;s template stuff separate and not mashed into all of the other files.</p>
<p>The compress build phase takes a list of files, joins them together and then runs them through yui-compressor.  Let&#8217;s take a look at that build phase again:</p>
<script src="http://gist.github.com/647481.js"></script>
<p>If you look at the <strong>files</strong> element, you&#8217;ll see a filename (in this case, <strong>js/glam.rock.min.{VERSION}.js</strong>) with a list of files associated with it.  These files are the ones that are joined together and compressed to generate <strong>js/glam.rock.min.{VERSION}.js</strong>.  So what&#8217;s the deal with the {VERSION} thing?  That tells makejs to replace {VERSION} with the newest build version, which makejs takes care of automatically for you.  For example, if you&#8217;re configuration says version: 0.0.81 &#8211; the next time you build, makejs will auto-increment the build #, re-save the conf file, and then when it generates the compressed files, you&#8217;ll end up with a file name <strong>js/glam.rock.min.0.0.82.js</strong>.</p>
<p>The manifest phase is what builds the HTML5 cache manifest, a definite must for mobile HTML5 apps.  For GlamRock, it won&#8217;t actually build a manifest (see below for why), but it is important to list what files that this build produces which can be included in the cache manifest.</p>
<p>Now, let&#8217;s turn our attention to the example build target.  This build target has a copy, compress, alter and manifest build phase.  But before we dive into those, you&#8217;ll see that this build phase has a pre-requisite for GlamRock.  When this build target is built, GlamRock will be built first and the output of that build will be put into whatever the build directory for example is.  This is nice because we treat each build target as independent projects and get some nice dependency functionality out of it.  Keeps things clean and relatively simple.</p>
<p>Back to the build phases for the example build target.  I won&#8217;t go over copy and compress because there is nothing different happening here then there was in the GlamRock target.  So let&#8217;s look at the alter build phase:</p>
<p>The alter build phase takes any input file and replaces any denoted blocks with the proper &lt;script&gt; and &lt;link&gt; tags that have been generated during the entire build process.  If you look at the &lt;head&gt; example at the top of this post, you&#8217;ll see that there are actually two blocks surrounded by <strong>&lt;!&#8211; BEGIN {target} &#8211;&gt;</strong> and<strong> &lt;!&#8211; END {target} &#8211;&gt; </strong>comments.  During the alter phase, makejs will replace these blocks with the output of the specified build target.  For example, after building, this is what our new &lt;head&gt; looks like:</p>
<script src="http://gist.github.com/647498.js"></script>
<p>Pretty nice, right?  Note, that this never alters the original, it simply transforms the input into the destination in the build output directory.</p>
<p>The example build phase has a manifest phase, like GlamRock, but with one important difference: it has a name setting.  When building a manifest phase, if there is a name specified, than a physical cache manifest is generated.  So running this phase on the example build target yields a cache manifest saved to example.manifest in the output directory.  The example.manifest looks like this:</p>
<script src="http://gist.github.com/647508.js"></script>
<p>Perfect-o.</p>
<h3>Getting It</h3>
<p>Head on over to GitHub and check out the project: <a href="http://github.com/jawngee/makejs" target="_blank">http://github.com/jawngee/makejs</a></p>
<p>Hope someone finds this useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/introducing-makejs/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Is This Really The Future of Magazines or Why Didn&#8217;t They Just Use HTML 5?</title>
		<link>http://interfacelab.com/is-this-really-the-future-of-magazines-or-why-didnt-they-just-use-html-5/</link>
		<comments>http://interfacelab.com/is-this-really-the-future-of-magazines-or-why-didnt-they-just-use-html-5/#comments</comments>
		<pubDate>Thu, 27 May 2010 17:55:27 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=95</guid>
		<description><![CDATA[I just downloaded the Wired iPad application, and like most iPad applications (and most magazines for that matter), I found myself bored with it within the first 20 minutes. I&#8217;m sure the content is engaging, I&#8217;m sure the articles are worth reading &#8211; but I am stumped as to why I would chose this over [...]]]></description>
			<content:encoded><![CDATA[<p>I just downloaded the Wired iPad application, and like most iPad applications (and most magazines for that matter), I found myself bored with it within the first 20 minutes.  I&#8217;m sure the content is engaging, I&#8217;m sure the articles are worth reading &#8211; but I am stumped as to why I would chose this over the physical magazine itself, or their website for that matter.  In fact, for reasons I&#8217;ll get into below, I&#8217;m starting to believe that the physical magazine&#8217;s &#8220;interface&#8221; is vastly superior to it&#8217;s iPad cousin.</p>
<p>However, what strikes me most about the Wired app is how amazingly similar it is to a multimedia CD-ROM from the 1990&#8242;s.   This is not a compliment and actually turns out to be a fairly large problem&#8230;</p>
<p><span id="more-95"></span></p>
<h3>1990&#8242;s Here We Come &#8230; Again</h3>
<p>The only real differentiation between the Wired application and a multimedia CD-ROM is the delivery mechanism: you download it via the App Store versus buying a CD-ROM at the now defunct Egg Head store at your local strip mall.  And I really mean that comparison.  For all of the interactivity that was touted in the Flash prototype, what we&#8217;ve really ended up with is a glorified slide show.  Instead of the &#8220;Next&#8221; and &#8220;Previous&#8221; buttons you might have been used to on those old CD-ROMs of yore, you instead swipe left and right to change pages (well *cough* images of pages).</p>
<p>There are certain interactive elements to the articles, but &#8211; and I apologize to all of the people who put in a lot of back breaking work into this &#8211; they&#8217;re pretty lame.  Tapping on a button-looking element switches out part of the page with another image.  You can drag your finger across certain images to make them sort of animate like a flipbook (and in truth, that&#8217;s what it is &#8211;  a series of PNG or JPEG images).  There are videos you can tap on to view fullscreen.  There are audio clips that you can play.  The interactivity in the Wired application is very 1990&#8242;s.  I am not trying to be insulting either, it&#8217;s simply the truth.  The Wired application has pretty much brought back image rollovers.</p>
<p>And that&#8217;s about the extent of the interactivity.  Which makes me wonder if that should be the extent of it or should we be wanting more?  I don&#8217;t have an answer to that, though my gut feeling is that there is a massive opportunity to reinvent the concept of a magazine &#8211; yet we end up with something akin to what the web was like in the mid to late 90&#8242;s.  This basically boils down to a print designer&#8217;s vision of what the web should be like &#8211; but in this case it&#8217;s a print magazine person&#8217;s vision of what an interactive magazine should be like.</p>
<p>If you can think back that far, and you were doing web development during that time, you will glumly remember the frustration of web development driven by print design, where pages were essentially huge images cut and sliced and then reconstituted back into insane table structures for pixel perfect layouts.  That&#8217;s what we&#8217;ve essentially gotten with the Wired app: a giant step backwards and a complete dismissal of the lessons we learned from that dark period of interaction design and development.</p>
<h3>Holy Shit, That&#8217;s Big</h3>
<p>With the Wired app weighing in at a whopping 500 megabytes &#8211; just 100 shy of a full CD-ROM &#8211; how do they intend to maintain new editions of the magazine?  500 MB is too large for a 3G download (no help from AT&amp;T&#8217;s less than spectacular network performance) and for those with iPad&#8217;s with the smaller storage, each issue will take a significant chunk of space on the device.  With no apparent means for managing which issues you keep on your device, this will become huge issue for a lot of people.  Obviously they will fix this with updates to the application, but I&#8217;m still wondering what they were thinking to begin with.  I&#8217;m hoping there were voices of dissent that pointed out the end product was not worth it&#8217;s weight in megabytes.  A PDF version would have been a tenth of the size, though without the interactivity.  But is the interactivity worth the 500MB price?  I personally don&#8217;t think so.</p>
<p>Why is the magazine so large?  Being the intrepid hacker that I am (*wink*) I mounted my jail broken iPad via AppleTalk and quickly tore into the app itself to see how it was constructed.  Similar to the PopSci+ magazine application, each Wired issue is actually a bunch of XML files that lay out a bunch of images.  And by &#8220;a bunch of images&#8221; I mean 4,109 images weighing in at 397MB.</p>
<p>Each full page is a giant image &#8211; there are actually two images for each page: one for landscape and one for portrait mode.  Yes, I&#8217;m laughing on the inside too.  There is no text or HTML, just one gigantic image. The &#8220;interactive&#8221; pieces where you can slide your finger to animate it are just a series of JPG files.  When you press play on the audio file and see the progress meter animate?  A series of PNG files.</p>
<p>Something is wrong with this picture.  Something wrong and something very lazy and/or desperate.</p>
<h3>Over Architect Much? (or How Desperation Ruins Good Ideas)</h3>
<p>I have no inside knowledge on how the Wired app was produced, so the following is all conjecture on my part.  That said, my guess is that Adobe sold Conde Nast on doing the thing in Flash.  Or if Adobe didn&#8217;t do the selling, some Flash loving technologist at Conde Nast sold them on it.  Either way, since Flash CS5 was going to be able to target the iPhone/iPad, they&#8217;d be able to publish the thing as it had been shown to the press.  But then Steve Jobs came along and threw section 3.1.3 into the iPhone licensing terms and &#8230; well &#8230; Adobe and Conde Nast were pretty much fucked.  So fast forward to this moment in time and the best short term solution they could come up with was some jury rigged XML based layout framework and an epic shit ton of images.</p>
<p>The Wired app isn&#8217;t alone in this weird architectural choice either.  The PopSci+ magazine is based on a very similar architecture.  There are also other magazines that work along the same lines, or simply go the route of PDFs with a customized PDF viewer application.</p>
<p>The problem with these XML + images architectures is that they are essentially reinventing HTML with no added benefit.  When I showed the Wired app to a colleague of mine, someone I consider to be one of the top HTML/Javascript developers in NYC, his assessment was the same: Why the heck didn&#8217;t they use HTML5?  We stepped through each &#8220;page&#8221; of the Wired application, looked at each interactive piece &#8211; but failed to find anything that ruled out the use of HTML and JavaScript.</p>
<p>The argument might be that it needs to be cross platform &#8211; the very thing Adobe and Conde Nast were banking on by going with Flash &#8211; but guess what?  For all of the tablets about to fall on the heads of consumers in the coming years, each one of them uses WebKit.  If anything was built for this type of application, it most certainly is WebKit.  And even for harder interactivity puzzles &#8211; in terms of how do we do X and Y &#8211; one can easily hook into WebKit  to enable that stuff that might otherwise be more difficult to do in straight  HTML + CSS + JavaScript.  I have yet to see anything in any magazine application on the iPad that would really require this though.</p>
<p>So why didn&#8217;t they choose HTML5 and build a custom viewer application around WebKit?  It comes down to either a sense of desperation, a sense of Adobe overselling a bad idea or simply a dumb technology decision.  Possibly all three.  It certainly isn&#8217;t a development challenge and it certainly isn&#8217;t because WebKit isn&#8217;t capable.  I had a thought that perhaps memory management was an issue, but by going with HTML5/WebKit, you wouldn&#8217;t be showing pages and pages of huge images &#8211; you&#8217;d actually be able to build those pages the right way.  And doing it this way, in my professional opinion, the magazine itself would be slashed dramatically in size, as well as acting and reacting in ways familiar to people who&#8217;ve been browsing the web for the last 15+ years.  Furthermore, the cost savings from a production standpoint would be drastically lower as Wired already maintains a staff of web developers.   There wouldn&#8217;t be an impetus for Adobe to create some &#8220;solution&#8221; at Conde Nast&#8217;s expense and a lot of the great interactivity you saw in the YouTube videos of their prototype could very easily come to life.</p>
<p>But as it stands now, the Wired iPad app is even far behind their own website.  That&#8217;s embarrassing.  Did anyone at Conde Nast look at this and wonder why someone would choose to use this over their very own website?  That iPad &#8211; unless you are in a subway &#8211; is constantly connected to the intertubehighway. That fact alone makes one wonder what the point of the whole thing is. Specifically since they&#8217;ve not done any sort of interactivity or visual presentation that I think anyone can say is amazing.  Sure, it&#8217;s a print designers wet dream &#8211; but it really should be a consumer&#8217;s wet dream.  And it most certainly is not that.</p>
<p>So, from a technical perspective, I think what we are looking at is the result of equal parts desperation and ignorance.  Desperation on the part of Adobe to carry forward their relationship with Conde Nast in this new publishing market and ignorance on the part of their development team for ignoring the best solution to their &#8220;No Flash Allowed&#8221; problem: HTML5.</p>
<h3>Is This The Future For Magazine Publishing?</h3>
<p>I hope not.</p>
<p>I actually think it&#8217;s a huge step backwards and I think the wrong people are working on the problem &#8211; just like the wrong people were working on the web problem back in the day.   Sure, we corrected course and we&#8217;re seeing the web done correctly more and more these days &#8211; but can the magazine publishing industry afford to get this wrong for any amount of time?  Once could argue that the internet is quickly making their industry irrelevant.  By the time an article is published in Time, I&#8217;ve read six or seven different takes on the same story on the web well before it hits the newsstands.  I don&#8217;t think that&#8217;s a unique or new insight.  But now you want me to download 500MB a month just so some print designer can have pixel perfect layouts with custom fonts?</p>
<p>Unfortunately, as long as Conde Nast allows Adobe to dupe them into believing Adobe has a solution, it&#8217;s going to fail.  They need to go outside the box to get this right, and all the technology to do it is sitting right there in front of them.  And they&#8217;ve been using it very well &#8230; until now when they&#8217;ve chosen not to even use it all.</p>
<h3><span style="color: #ff0000;">Update</span></h3>
<p>People have been mentioning that you can&#8217;t do application specific fonts with the WebKit browser.  Not sure why.</p>
<p>Here is a simple iPad app that loads up a custom open type font, and then displays an html file in a UIWebView with that custom font:  <a href="http://github.com/jawngee/iPadFontExample" target="_blank">http://github.com/jawngee/iPadFontExample</a></p>
<p>So it can be done.  Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/is-this-really-the-future-of-magazines-or-why-didnt-they-just-use-html-5/feed/</wfw:commentRss>
		<slash:comments>525</slash:comments>
		</item>
		<item>
		<title>My Mom is Going to Love the iPad</title>
		<link>http://interfacelab.com/my-mom-is-going-to-love-the-ipad/</link>
		<comments>http://interfacelab.com/my-mom-is-going-to-love-the-ipad/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 08:24:18 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=83</guid>
		<description><![CDATA[My mom is totally going to love the shit out of this thing. Off the top of my head: She can read her books in the dark. She can use it in the kitchen to look up recipes and watch cooking videos. She can use the GPS to help my dad drive their RV around [...]]]></description>
			<content:encoded><![CDATA[<p>My mom is totally going to love the shit out of this thing.</p>
<p><span id="more-83"></span></p>
<p>Off the top of my head:</p>
<ul>
<li>She can read her books in the dark.</li>
<li>She can use it in the kitchen to look up recipes and watch cooking videos.</li>
<li>She can use the GPS to help my dad drive their RV around the country.</li>
<li>She can be better connected to her iPhone wielding grandchildren.</li>
<li>She can play Words with Friends on a bigger screen.</li>
<li>She can  browse the web on a readable screen.  While in the RV.</li>
<li>Watch all of her NFL games while sitting on the porch.</li>
</ul>
<p>And that&#8217;s just to start.  Later down the road, maybe after revision 2, she&#8217;ll also be able to:</p>
<ul>
<li>Draw her knitting patterns and sync it with her knitting machine via Bonjour over WiFi.</li>
<li>Have video chats with the kids.</li>
<li>Augmented reality tour guides for the new places they travel to.</li>
<li>Telemedical (god forbid) monitoring to save trips to the doctor.</li>
<li>Control surface for manipulating certain controls of the RV.</li>
</ul>
<p>The best part?  I won&#8217;t have to show her how to do those things, not most of them anyway.  So to do 80% of what she uses her laptop for, she can do all of those things without having to lug the thing out, plug it in, hook up the mouse, etc.  She&#8217;ll be able to take the information she is looking for to the place where she needs it to be: the kitchen, the sewing room, the co-pilot&#8217;s seat.</p>
<p>So thinking about my mother using it, and loving it, I started to think how I would use it.  My list is almost equally as long.</p>
<ul>
<li>Control Boxee.</li>
<li>Use it as a control surface for Final Cut or Ableton Live or Traktor via Bluetooth or WiFi.</li>
<li>Home automation.</li>
<li>The ultimate universal remote (extra hardware required)</li>
<li>Pornography</li>
<li>Better in-flight DVD viewing experience</li>
</ul>
<p>There are so many more integration points the iPad can make with day to day living and day to day information needs.  The key aspect of it all is mobility and portability, having the information you need not only when you need it, but <strong>where </strong>you need it.</p>
<p>I understand the let down from the perspective of people who were wishing for a multi-touch Mac Book.  But I think they are thinking about it wrong.  People are thinking about it as if it&#8217;s a computer, but it isn&#8217;t just a computer, it&#8217;s a whole new category of device, a whole new definition of what a computer is.</p>
<p>I also agree with most that the lock down to the App Store is a let down, though you don&#8217;t have to think too hard about it to understand it&#8217;s a necessary evil.  It reduces any potential confusion on how to make the thing do something new.  My Mom and I were playing Words with Friends without me having to walk her through how to install it and get it running.  Now compare that with what it was like to show her how to do something similar on her Windows laptop &#8230; yeah no thanks, I&#8217;ll take the App Store.  Not to mention not having to run that crappy antivirus and anti-spyware junk &#8211; the stuff that sucks the performance out of her laptop and yields endless complaints about how slow everything is.  Perhaps it&#8217;s a false sense of security, but I can live with that.</p>
<p>Some of the complaints about usability and related things are complete nonsense and even defy logic.  I&#8217;m pretty sure Apple did some usability testing on this thing &#8230; but maybe that&#8217;s just wild speculation on my part.</p>
<p>I think Apple is definitely onto something, maybe it&#8217;s not quite there yet, but they&#8217;ve definitely accomplished more than bringing an oversized iPhone to market.  But only time will tell.</p>
<p>And, yes, she does kick my ass in Words with Friends.  She hit me with two 70+ pointers yesterday.</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/my-mom-is-going-to-love-the-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slicehost API Notes for the Non-Rails Posse</title>
		<link>http://interfacelab.com/slicehost-api-notes-for-the-non-rails-posse/</link>
		<comments>http://interfacelab.com/slicehost-api-notes-for-the-non-rails-posse/#comments</comments>
		<pubDate>Sat, 09 May 2009 06:38:23 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=66</guid>
		<description><![CDATA[I just want to start out by stating that I, in fact, am a big fan of Slicehost and constantly recommend them to friends setting up anything more serious than blogs. They have a truly great offering and their customer service has only been rivaled, in my experience, by the support team from DataPipe &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>I just want to start out by stating that I, in fact, am a big fan of <a href="http://slicehost.com" target="_blank">Slicehost</a> and constantly recommend them to friends setting up anything more serious than blogs.  They have a truly great offering and their customer service has only been rivaled, in my experience, by the support team from <a href="http://datapipe.net">DataPipe</a> &#8211; whom I whole heartedly recommend for any colo or managed hosting.</p>
<p>One thing that is truly great about Slicehost is that they offer an awesome API that allows you to do most everything you can do in their management console, but instead through web services.  You can create new slices, reboot them, change and add DNS, as well as rebuild or destroy slices.</p>
<p>The only problem is that the API follows a “standard ActiveResource pattern” that comes straight from the bowels of Rails and is way too convoluted for something this simple.  </p>
<p><span id="more-66"></span></p>
<p>My biggest complaint is that instead of POSTing standard post variables, you POST XML, but none of the data being posted approaches any kind of structural complexity that warrants it’s usage.  It just a layer of crap that doesn’t need to be there.  Every modern language has some sort of HTTP request mechanism that handles all this stuff without some arbitrary object model forced on top of it.</p>
<p>Curiously, there appears to be another way where you can POST regular form data, but the variable names are in the form “resource[field]” which, again, seems completely unnecessary.  I also couldn’t get it to work reliably.</p>
<p>As a point of reference, I was able to implement S3, EC2 and SQS API’s from amazon in just under 6 hours.  It took me two days to figure out the Slicehost API.  The ActiveResource pattern is a failure in my estimation.</p>
<p>The following are my notes on implementing the slicehost API.  Hope you find it useful.</p>
<p>NOTE:  This post doesn’t cover their DNS API as I don’t have a particular need for it.  I will be posting the PHP library I wrote for the Slicehost API to github over the weekend.  Perhaps someone can build on top of that&#8230;</p>
<h3>Authentication</h3>
<p>I actually had a bit of a bitch with this.  The docs and ruby samples specify that you put your API key in the url, like this:</p>
<p><a href="https://APIKEY@api.slicehost.com">https://APIKEY@api.slicehost.com</a></p>
<p>Problem is that I just could not get it to work.  Even the ruby samples in their docs didn’t run without authentication errors.  Copying and pasting the URLs into Safari didn’t work either (simply pasting your API key into the user input when the Basic Authentication popup shows does work though).</p>
<p>So, make sure you always set the USER part of Basic Auth with your API key.  In PHP it looks like this (using PEAR’s HTTP_Request object):</p>
<pre class="precode">$request-&gt;basic_auth(YOUR_API_KEY,’’);</pre>
<p>&nbsp;</p>
<h2>Dealing With Slices</h2>
<p>This is the part that drove me nuts.  I ended up having to debug their ruby examples through Charles, after patching activeresource.rb to support proxies.  And even then, their examples didn’t work due to a typo and another error I couldn’t sort out.</p>
<h3>Listing Slices</h3>
<p>This is easy as pie.  Simply make a GET request to:</p>
<p><a href="https://api.slicehost.com/slices">https://api.slicehost.com/slices.xml</a></p>
<p>You will get back an XML document as a response:</p>
<pre class="precode">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;slices type="array"&gt;
	&lt;slice&gt;
		&lt;name&gt;slicename&lt;/name&gt;
		&lt;status&gt;active&lt;/status&gt;
		&lt;addresses type="array"&gt;
			&lt;address&gt;XXX.XXX.XXX.XXX&lt;/address&gt;
			&lt;address&gt;XXX.XXX.XXX.XXX&lt;/address&gt;
		&lt;/addresses&gt;
		&lt;id type="integer"&gt;666&lt;/id&gt;
		&lt;progress type="integer"&gt;100&lt;/progress&gt;
		&lt;bw-out type="float"&gt;0.0&lt;/bw-out&gt;
		&lt;bw-in type="float"&gt;0.0&lt;/bw-in&gt;
		&lt;image-id type="integer"&gt;10&lt;/image-id&gt;
		&lt;ip-address&gt;XXX.XXX.XXX.XXX&lt;/ip-address&gt;
		&lt;flavor-id type="integer"&gt;2&lt;/flavor-id&gt;
	&lt;/slice&gt;
&lt;/slices&gt;
</pre>
<p>The one issue I have with this is that the &lt;address&gt; element doesn’t specify if the address is internal or external.  Right now, you can only make the assumption that the first address is external and the second one is internal.  Not a big deal though.</p>
<p>Also, note the .xml extension.  I believe this is arbitrary, but it would be dope if you could specify .json or .yaml or some other format.  Again, not a big deal.</p>
<h3>Getting Slice Info</h3>
<p>This is equally as simple:</p>
<p><a href="https://api.slicehost.com/slices/666.xml">https://api.slicehost.com/slices/666.xml</a></p>
<p>Where 666 is the ID of the slice you want to get info about.  You get another XML document in return containing a &lt;slice&gt; element as above.</p>
<h3>Creating a Slice From an Image</h3>
<p>This is relatively simple, but there are a couple of steps to it.</p>
<p>First you need a list of the images that Slicehost offers.  Easy enough.  GET request to :</p>
<p><a href="https://api.slicehost.com/images">https://api.slicehost.com/images</a></p>
<p>Will return an XML document that looks like this:</p>
<pre class="precode">
&lt;images type="array"&gt;
	&lt;image&gt;
		&lt;name&gt;CentOS 5.2&lt;/name&gt;
		&lt;id type="integer"&gt;2&lt;/id&gt;
	&lt;/image&gt;
	&lt;image&gt;
		&lt;name&gt;Gentoo 2008.0&lt;/name&gt;
		&lt;id type="integer"&gt;3&lt;/id&gt;
	&lt;/image&gt;
	&lt;image&gt;
		&lt;name&gt;Debian 5.0 (lenny)&lt;/name&gt;
		&lt;id type="integer"&gt;4&lt;/id&gt;
	&lt;/image&gt;
	{ .. and so on .. }
&lt;/images&gt;
</pre>
<p>Once you have this list, you’ll have need to figure out what flavors of slices you can create.  Do this by sending another GET request to:</p>
<p><a href="https://api.slicehost.com/flavors">https://api.slicehost.com/flavors</a></p>
<p>Again, you’ll get another list that looks like this:</p>
<pre class="precode">
&lt;flavors type="array"&gt;
	&lt;flavor&gt;
		&lt;id type="integer"&gt;1&lt;/id&gt;
		&lt;name&gt;256 slice&lt;/name&gt;
		&lt;price type="integer"&gt;2000&lt;/price&gt;
		&lt;ram type="integer"&gt;256&lt;/ram&gt;
	&lt;/flavor&gt;
	&lt;flavor&gt;
		&lt;id type="integer"&gt;2&lt;/id&gt;
		&lt;name&gt;512 slice&lt;/name&gt;
		&lt;price type="integer"&gt;3800&lt;/price&gt;
		&lt;ram type="integer"&gt;512&lt;/ram&gt;
	&lt;/flavor&gt;
	&lt;flavor&gt;
		&lt;id type="integer"&gt;3&lt;/id&gt;
		&lt;name&gt;1GB slice&lt;/name&gt;
		&lt;price type="integer"&gt;7000&lt;/price&gt;
		&lt;ram type="integer"&gt;1024&lt;/ram&gt;
	&lt;/flavor&gt;
	{ ... and so on ... }
&lt;/flavors&gt;
</pre>
<p>So now we know the ID for the image and the ID for flavor of slice.  Let’s keep going!</p>
<p>This next bit took me awhile to get going.  When I tried to go through the method of posting good old post data, I would get back really obscure errors.  So debugging the HTTP traffic of the ruby examples, I unearthed the secret sauce.</p>
<p>To create the slice with the image of your choosing, POST an XML document to:</p>
<p><a href="https://api.slicehost.com/slices/">https://api.slicehost.com/slices/</a></p>
<p>But, make absolutely sure that you have set the “Content-Type” of your request to “text/xml” otherwise it no worky.  Your XML document will look like:</p>
<pre class="precode">
&lt;slice&gt;
	&lt;name&gt;NAME OF YOUR SLICE&lt;/name&gt;
	&lt;flavor-id type="integer"&gt;1&lt;/flavor-id&gt;
	&lt;image-id type="integer"&gt;2&lt;/image-id&gt;
&lt;/slice&gt;
</pre>
<p>The &lt;image-id&gt; must be a valid ID from the list above, of course.    Their API docs have a typo where the sample code to create a slice uses an ID of 1, which doesn’t exist.  A little hair pulling on that one, even though the bug was so obvious.  The same thing goes for the &lt;flavor-id&gt;.</p>
<p>The response you receive will be information about you slice, including the root password.  You only get this root password once, so make sure you take note of it.  The response looks like:</p>
<pre class="precode">
&lt;slice&gt;
	&lt;status&gt;build&lt;/status&gt;
	&lt;name&gt;SLICE NAME&lt;/name&gt;
	&lt;addresses type="array"&gt;
		&lt;address&gt;XXX.XXX.XXX.XXX&lt;/address&gt;
		&lt;address&gt;XXX.XXX.XXX.XXX&lt;/address&gt;
	&lt;/addresses&gt;
	&lt;id type="integer"&gt;666&lt;/id&gt;
	&lt;root-password&gt;ROOT PASSWORD&lt;/root-password&gt;
	&lt;progress type="integer"&gt;0&lt;/progress&gt;
	&lt;bw-out type="float"&gt;0.0&lt;/bw-out&gt;
	&lt;bw-in type="float"&gt;0.0&lt;/bw-in&gt;
	&lt;image-id type="integer"&gt;2&lt;/image-id&gt;
	&lt;ip-address&gt;XXX.XXX.XXX.XXX&lt;/ip-address&gt;
	&lt;flavor-id type="integer"&gt;1&lt;/flavor-id&gt;
&lt;/slice&gt;
</pre>
<p>And that’s it, you’ve created your slice!</p>
<h3>Creating a Slice From a Backup</h3>
<p>This is nearly the same as creating from an image, but this time we’ll need a list of available backups.  Backups are things you create through the Slicehost management interface.  What I like to do is build a base system and then take a snapshot.  With that snapshot I can create new slices based on it.</p>
<p>To grab a list of backups, send a GET request to:</p>
<p><a href="https://api.slicehost.com/backups">https://api.slicehost.com/backups</a></p>
<p>You will receive a list (if you have any backups):</p>
<pre class="precode">
&lt;backups type="array"&gt;
	&lt;backup&gt;
		&lt;id&gt;3-666&lt;/id&gt;
		&lt;slice_id&gt;666&lt;/slice_id&gt;
		&lt;name&gt;daily&lt;/name&gt;
		&lt;date&gt;Sat May 09 04:15:05 UTC 2009&lt;/date&gt;
	&lt;/backup&gt;
	&lt;backup&gt;
		&lt;id&gt;3-667&lt;/id&gt;
		&lt;slice_id&gt;666&lt;/slice_id&gt;
		&lt;name&gt;webserver_snapshot&lt;/name&gt;
		&lt;date&gt;Sat May 09 04:15:05 UTC 2009&lt;/date&gt;
	&lt;/backup&gt;
&lt;/backups&gt;
</pre>
<p>And, then, in the XML document you POST:</p>
<pre class="precode">
&lt;slice&gt;
	&lt;name&gt;NAME OF YOUR SLICE&lt;/name&gt;
	&lt;flavor-id type="integer"&gt;1&lt;/flavor-id&gt;
	&lt;backup-id type="integer"&gt;3-666&lt;/backup-id&gt;
&lt;/slice&gt;
</pre>
<p>Everything else is the same as creating a slice from an image.</p>
<h3>Updating a Slice</h3>
<p>The only thing you can update with your slice is it’s name.  This is accomplished by PUTing an XML document to:</p>
<p><a href="https://api.slicehost.com/slices">https://api.slicehost.com/slices</a></p>
<p>The XML doc looks like:</p>
<pre class="precode">
&lt;slice&gt;
	&lt;id type="integer"&gt;666&lt;/id&gt;
	&lt;name&gt;NAME OF YOUR SLICE&lt;/name&gt;
&lt;/slice&gt;
</pre>
<p>All you need is the ID of the slice and it’s new name.  The response will be an XML document with a &lt;slice&gt; node.</p>
<h3>Deleting a Slice</h3>
<p>Delete a slice by sending an XML document using the DELETE HTTP verb:</p>
<pre class="precode">
&lt;slice&gt;
	&lt;id type="integer"&gt;666&lt;/id&gt;
&lt;/slice&gt;
</pre>
<p>NOTE: You will have to have this option enabled in the API settings on your account.  You can edit those settings at:  <a href="https://manage.slicehost.com/api/">https://manage.slicehost.com/api/</a></p>
<h3>Rebooting Your Slices</h3>
<p>To reboot your slice with a hard reset, send a PUT request to:</p>
<p><a href="https://api.slicehost.com/slices/666/hard_reboot.xml">https://api.slicehost.com/slices/YOUR SLICE ID/hard_reboot.xml</a></p>
<p>To do a soft reboot, a PUT request to:</p>
<p><a href="https://api.slicehost.com/slices/666/reboot.xml">https://api.slicehost.com/slices/YOUR SLICE ID/reboot.xml</a></p>
<h3>Rebuilding Your Slices</h3>
<p>To rebuild your slice, you’ll need to use an image ID or a backup ID.  Once you’ve picked one, send a PUT request to:</p>
<p><a href="https://api.slicehost.com/slices/666/rebuild.xml?image_id=X">https://api.slicehost.com/slices/YOUR SLICE ID/rebuild.xml?image_id=X</a></p>
<p>or:</p>
<p><a href="https://api.slicehost.com/slices/666/rebuild.xml?backup_id=X">https://api.slicehost.com/slices/YOUR SLICE ID/rebuild.xml?backup_id=X</a></p>
<h2>Conclusion</h2>
<p>So that’s all I know.  As you can see the API is pretty simple, but not as simple as it could be, nor are the documents even remotely near as useful as they should be.  But, it’s awesome they have this API in the first place so I’ll stop bitching now!</p>
<p>Again, thanks to Slicehost for making awesome.  The world sure does need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/slicehost-api-notes-for-the-non-rails-posse/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Variables in CSS via PHP</title>
		<link>http://interfacelab.com/variables-in-css-via-php/</link>
		<comments>http://interfacelab.com/variables-in-css-via-php/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 03:19:10 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=22</guid>
		<description><![CDATA[UPDATED: Added support for expressions with variables so that you can add, multiply, divide, etc. variables when using them in the CSS. See below for more info. Back in April of 2008, I came across a proposal by Daniel Glazman and David Hyatt for using variables in CSS stylesheets.  I thought the proposal was absolutely [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATED:</strong> Added support for expressions with variables so that you can add, multiply, divide, etc. variables when using them in the CSS.  See below for more info.</p>
<p>Back in April of 2008, I came across a <a href="http://disruptive-innovations.com/zoo/cssvariables/">proposal</a> by Daniel Glazman and David Hyatt for using variables in CSS stylesheets.  I thought the proposal was absolutely brilliant, filling a much needed void for sites using complicated stylesheets across a variety of different pages.  Another part of their proposal was being able to include/import other stylesheets.  I don&#8217;t know anyone that couldn&#8217;t find this useful.</p>
<p>I put together a quick class for implementing most of their proposal using PHP.  Nothing fancy going on here, most of it is some simple regexes.</p>
<p><a href="http://interfacelab.com/code/css_compiler.zip">Download the code here</a>.</p>
<p><span id="more-22"></span></p>
<p><strong>Using the CSS Compiler</strong></p>
<p>Let&#8217;s first talk about how to setup your CSS stylesheets to use variables and imports.</p>
<p><strong>Important:</strong> Any stylesheet that uses variables or includes stylesheets that use variables <strong>must</strong> end with the extension &#8216;<strong>.cssp</strong>&#8216;.</p>
<p>To declare variables, you place them within an @variables at-rule like so:</p>
<pre class="brush: css; title: ; notranslate">

@variables
{
    titleFont:bold 18px &quot;Verdana, Arial&quot;;
    titleBG: #FF0000;
}
</pre>
<p>To use the variables:</p>
<pre class="brush: css; title: ; notranslate">

div.title { font: var(titleFont); background: var(titleBG); }
</pre>
<p>Couldn&#8217;t get much simpler.</p>
<p><strong>Importing CSS and Variables</strong></p>
<p>You can also import/include css and variables from an external stylesheet using @import:</p>
<pre class="brush: css; title: ; notranslate">

@import &quot;vars.cssp&quot;;

div.title { font: var(defaultFont); };
</pre>
<p>Alternately:</p>
<pre class="brush: css; title: ; notranslate">

@import url(&quot;vars.cssp&quot;);

div.title { font: var(defaultFont); };
</pre>
<p><strong>Evaluating Expressions</strong></p>
<p>You can also perform math operations with measurement units.  For instance, you could do something like:</p>
<pre class="brush: css; title: ; notranslate">
@variables {
    lc_width: 240px;
    rc_width: 180px;
    all_padding: 20px;
}

#some_div {
    width: eval((lc_width+rc_width)-(all_padding*2));
    padding: eval(all_padding/2);
}
</pre>
<p>You can only do this with px/em/% measurement units and the units must match.  For instance you can&#8217;t add a variable that is 2em with another that is 120px.  Doing so will throw an exception.  Also, if you use a variable reference that doesn&#8217;t exist, it will also throw an Exception.</p>
<p><strong>Using in HTML Pages</strong></p>
<p>To use these stylesheets, you&#8217;re going to have to write a little PHP, but not too much:</p>
<pre class="brush: php; title: ; notranslate">

&lt;?
    // include the compiler
    include 'css_compiler.php';

    // set the compile environment variable
    // otherwise it will spit out a static file
    // in a production environment comment this out:
    define('COMPILE_CSS',true);
?&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;? style(dirname(__FILE__),'testing'); ?&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div class=&quot;nice&quot;&gt;Hello World&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>At the top of this file, we&#8217;re including the css compiler and then defining a global variable <strong>COMPILE_CSS</strong>.  <strong>COMPILE_CSS</strong> tells the compiler to do the compilation and save the resulting .css files.  You will not want to do this in a production environment, so comment out the define when deploying to the masses.</p>
<p>In the head tags, the style() function is a helper function that compiles the css.  The first argument is the full path to where the css is.  The second argument is the css file to use without it&#8217;s extension.  Note: if your css files have a relative path of /css/whatever.css on your webserver, the relative path should be part of the second argument and not the first.</p>
<p>For example, let&#8217;s say our PHP application is located at /var/www/.  Our css files are located at /var/www/css/.  Our style function is going to look like:</p>
<pre class="brush: php; title: ; notranslate">

style('/var/www/','/css/testing');
</pre>
<p>Our actual usage of this code at <a href="http://massify.com/">massify</a> is way different.  I&#8217;ve added the style() function so that people reading this blog can use it, but you should definitely do your own legwork to blend it into whatever framework you are using.  This should be enough to get started however.</p>
<p>Also, make sure that your css directory is writable by your webserver or the whole thing won&#8217;t work at all.</p>
<p>Leave a comment or <a href="mailto:jon.gilkison@gmail.com">drop me a line</a> if you find this useful/find bugs/have suggestions.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/variables-in-css-via-php/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>Metadata/Attributes in PHP</title>
		<link>http://interfacelab.com/metadataattributes-in-php/</link>
		<comments>http://interfacelab.com/metadataattributes-in-php/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 07:56:09 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://interfacelab.com/metadataattributes-in-php/</guid>
		<description><![CDATA[There&#8217;s a ton of stuff I miss from C#, having moved to PHP. Did I say a ton? I meant a megaton. One of the things I miss the most (besides the sanity) are attributes (annotations for Java peeps). To be able to ascribe metadata to class, method and property definitions opens up a whole [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a ton of stuff I miss from C#, having moved to PHP. Did I say a ton? I meant a megaton.</p>
<p>One of the things I miss the most (besides the sanity) are attributes (<a href="http://en.wikipedia.org/wiki/Java_annotations" target="_blank">annotations</a> for Java peeps). To be able to ascribe metadata to class, method and property definitions opens up a whole new world of introspection which enables you to do some pretty wicked hacks.</p>
<p>In this post, I present a PHP class that allows you to do metadata/attribute programming with PHP.  You can download the class <a href="http://interfacelab.com/code/attribute.zip">here</a>.  But before we dig in, we must understand what attributes are and how they are useful&#8230;</p>
<p><span id="more-21"></span></p>
<p><strong>Introduction to Attributes</strong></p>
<p>For those that don&#8217;t know about attribute based programming, let me quote an excerpt from <a href="http://www.ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html" target="_blank">OnDotnet</a>:</p>
<blockquote><p>Attributes are a mechanism for adding metadata, such as compiler instructions and other data about your data, methods, and classes, to the program itself. Attributes are inserted into the metadata and are visible through ILDasm and other metadata-reading tools.</p>
<p>Reflection is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior.</p></blockquote>
<p>One of the last things I wrote in C# before switching over was a simplistic REST-esque framework for web services for C#. I used attributes on classes to mark which ones were exposed to the outside world and then used attributes on the methods to specify such things as the URI, description, etc. This allowed the services to be discoverable versus declared and to be self documenting. For example:</p>
<p>namespace SimpleWebService {<br />
    [RestService(baseURI='/simple/',Name='Simple', Description='Simple service', Persistent=false)]<br />
    class Service<br />
    {<br />
        [RestMethod(URI='something',Name='Do Something',Description='Does something.')]<br />
        public void DoSomething()<br />
        {<br />
            // &#8230;<br />
        }<br />
    }<br />
}</p>
<p>In the above example, the attributes are declared in the brackets.  On our <strong>Service</strong> class, we give the service a name, define it&#8217;s base URI, give it a description and declare if the service is persistant between requests (meaning that the class is only instantiated once, or is instantiated every time).  On the <strong>DoSomething</strong> method it&#8217;s a similar deal: we declare the URI endpoint for the method, the name of the method and it&#8217;s description.</p>
<p>By going to <strong><em>http://example.com/simple/something</em></strong> would eventually invoke the <strong>DoSomething</strong> method on our <strong>Service</strong> class.  The beautiful thing here is that my classes needn&#8217;t implement any interfaces or descend from any parent classes to be exposed as a web service.  I could drop the attributes on the class, recompile and they would then be instantly available.  When the application loaded, it scanned all of the classes in the loaded assemblies, did some reflection on them and created a cache of exposed services.  An <a href="http://www.developerfusion.co.uk/show/4643/" target="_blank">HTTPHandler</a> would dispatch the incoming requests to the corresponding service, map any POST/GET or URI fragments to the parameters of the method being called and then returned the results of the method call as serialized XML.</p>
<p>Nice and easy.</p>
<p><strong>Attributes and PHP</strong></p>
<p>PHP has no built-in mechanism for declaring attributes, but it does have a primitive retrospection capability.  For any class or function in PHP, there are a set of functions that you can call that will provide information about the class.  For instance, with methods, you can get it&#8217;s parameters and any associated block of comments:</p>
<pre class="brush: php; title: ; notranslate">
class DumbClass
{
    /**
     * Comment block
     */
    public function thing($parameter,$another)
    {
        return false;
    }
}

$method=new ReflectionMethod('DumbClass','thing');
echo $method-&gt;getDocComment();
</pre>
<p>In the above example, we create an instance of the ReflectionMethod class and echo the comment block.  If we run this script, the output would look like:</p>
<pre class="brush: php; title: ; notranslate">
    /**
     * Comment block
     */
</pre>
<p>Now if we put 2 and 2 together, you&#8217;ll see that we could use the comment block to insert our class metadata.  Now all we need &#8230;</p>
<p><strong>Introducting AttributeReader</strong></p>
<p>The AttributeReader class is a simple class that extracts <a href="http://devzone.zend.com/article/2585-Using-YAML-With-PHP-and-PECL">YAML</a> from the comment block on a class, it&#8217;s methods or properties.  You can download the class <a href="http://interfacelab.com/code/attribute.zip">here</a>.  Note:  You must have the pecl syck package installed, details are <a href="http://pecl.php.net/package/syck">here</a>.</p>
<p>Since we have access to the comment block, it&#8217;s the most obvious place to express our metadata.  Obviously, the 100% correct solution would be to patch PHP to support attributes out of the box, but that&#8217;s a much bigger effort that would require some serious coding.  The performance of this method is fast enough for most use cases.</p>
<p>So how do we use this?  At <a href="http://massify.com">massify</a>, we use metadata for our ORM layer.  All of our models have metadata attached to them that define what database table to use, the column name, etc.  Here&#8217;s an example:</p>
<pre class="brush: php; title: ; notranslate">
/**
 * Sample model
 *
 * [[
 * table: sample.item
 * database: default
 * read_only: false
 * ]]
 *
 */
class Item extends Model
{
    //@ fields

    /**
     * [[
     * label: Title
     * type: string
     * length: 32
     * description: Title of the item
     * validate:
     *   required: true
     *   length: 8-32
     *   unique: true
     * ]]
     */
    public $title;

    /**
     * [[
     * label: URI
     * type: string
     * length: 32
     * description: URI of the item
     * validate:
     *   required: true
     *   length: 4-32
     *   unique: true
     *   format: alpha_numeric
     * ]]
     */
    public $uri;

    /**
     * [[
     * label: Description
     * type: text
     * description: Description of the item
     * ]]
     */
    public $description;

    //@ end fields
}
</pre>
<p>In the above example, our metadata is nestled between double brackets [[]].  Inside the double brackets is YAML expressing the metadata.  The cool thing here is that we can nest attributes of the metadata.  Let&#8217;s look at what we&#8217;ve done:</p>
<p>On the declaration for the Item class, we&#8217;ve described metadata that tells us which database table this model represents, which database it resides in and if it&#8217;s read_only (a view, in database parlance).  On the properties of the model, the metadata describes the label to use for forms, what the database type is (simplified), the length (for string types), a description and a list of validators.  For instance, on the $uri property, we&#8217;ve declared validators that make sure the property has a value before saving (required), is of a specified length (4-32 characters), is unique in the database (unique) and matches a specific format (alpha_numeric).</p>
<p>Since the class extends from Model, the model knows to use the metadata to build the correct SQL statements for insert, updates, etc.  It also knows how to validate itself when saved to the database, returning the correct errors to the user if any of the validations fail.  Finally, we are able to automatically build forms to edit the models by using the metadata as a guide when constructing the form in code.</p>
<p>All of this is completely possible to do without using metadata, but would require a lot of redundant code with a predetermined set of use cases.  Metadata frees us from this because we can write use cases that work with a known quantity, rather than the other way around.</p>
<p><strong>Using the AttributeReader</strong></p>
<p>Using the class is straight forward.  Here&#8217;s an example.  Let&#8217;s assume we have the Item model from the previous example loaded and want to extract metadata for it and it&#8217;s URI property:</p>
<pre class="brush: php; title: ; notranslate">
$item=new Item();

$class=AttributeReader::ClassAttributes($item);

echo $class-&gt;database;
echo $class-&gt;table;

$method=AttributeReader::PropertyAttributes($item,'uri');

echo $method-&gt;label;
if ($method-&gt;validate-&gt;required)
    echo &quot;Required field&quot;;
else
    echo &quot;Not required.&quot;;
</pre>
<p>It&#8217;s that simple.</p>
<p><strong>Caveats</strong></p>
<p>Make sure you are familiar with the rules of YAML.</p>
<p>Performance is acceptable, but always make sure you measure performance for yourself.  We use a caching strategy via APC at <a href="http://massify.com">massify</a> for caching model metadata.  This requires that you restart apache if your models change (you can disable APC on your development/staging to get around this).  With the APC caching strategy, it&#8217;s super fast.  Find out what works best for you.</p>
<p>I hope people find this useful, I think it opens a whole new world for serious PHP development.  Feel free to drop me an <a href="mailto:jon.gilkison@gmail.com">email</a> if you have questions or successes.</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/metadataattributes-in-php/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

