<?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</title>
	<atom:link href="http://interfacelab.com/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>Objective-C Memory Management For Lazy People</title>
		<link>http://interfacelab.com/objective-c-memory-management-for-lazy-people/</link>
		<comments>http://interfacelab.com/objective-c-memory-management-for-lazy-people/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 05:14:58 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=155</guid>
		<description><![CDATA[I really am starting to get annoyed and disappointed with Hacker News. I&#8217;ve been on that site for 1,180 days and counting, and I can tell you that the quality of discussions have not only gone down, but the behavior of up voting and down voting comments has changed significantly.  It&#8217;s really made me question if [...]]]></description>
			<content:encoded><![CDATA[<p>I really am starting to get annoyed and disappointed with <a href="http://news.ycombinator.com/" target="_blank">Hacker News</a>.  I&#8217;ve been on that site for <strong>1,180</strong> days and counting, and I can tell you that the quality of discussions have not only gone down, but the behavior of up voting and down voting comments has changed significantly.  It&#8217;s really made me question if &#8220;social news&#8221; is viable at all, but we&#8217;ll save those rantings for another blog post.  Maybe.</p>
<p>What&#8217;s got me irked this time around (see <a href="http://interfacelab.com/nobody-on-hacker-news-masturbates/" target="_blank">Nobody Masturbates on Hacker News</a> for other reasons) started out with this lazy and inane blog post: <a href="http://whereoscope.wordpress.com/2010/12/07/android-vs-ios-a-developers-perspective/" target="_blank">Android vs iOS: A Developer&#8217;s Perspective</a>.  I&#8217;m going to dissect and refute the post later on here, but what got me heated was that the <a href="http://news.ycombinator.com/item?id=1981520" target="_blank">comments</a> <a href="http://news.ycombinator.com/item?id=1981507" target="_blank">dispelling</a> the myths put forth in the blog post about memory management were being consistently down voted in the thread on HN and, instead, the <a href="http://news.ycombinator.com/item?id=1981570" target="_blank">glib</a>, <a href="http://news.ycombinator.com/item?id=1981374" target="_blank">snarky</a> and <a href="http://news.ycombinator.com/item?id=1981505" target="_blank">unhelpful</a> ones scored much higher.</p>
<p>The fuck Hacker News?  If it wasn&#8217;t for the <a href="http://www.reddit.com/r/programming/comments/ei4ln/android_vs_ios_a_developers_perspective/c189md7" target="_blank">sanity</a> on <a href="http://www.reddit.com/r/programming/comments/ei4ln/android_vs_ios_a_developers_perspective/" target="_blank">reddit</a>, I&#8217;d be totally losing my shit right about now.</p>
<p>Anyways, let&#8217;s get to memory management in Objective-C, because that&#8217;s what started this all off.</p>
<p><span id="more-155"></span></p>
<h2>Memory Management in Objective-C is Easier Than James Gregory Thinks</h2>
<p>I know the concept of memory management scares the shit out of script kids and the average java developers, but it really isn&#8217;t that scary.  But before we get into it, let&#8217;s see what <a href="http://whereoscope.wordpress.com/2010/12/07/android-vs-ios-a-developers-perspective/" target="_blank">James Gregory</a> has to say about it:</p>
<blockquote><p>iPhone doesn’t have it. Seriously, Apple: What The? <a href="http://www.joelonsoftware.com/articles/fog0000000006.html">Various</a> <a href="http://www.jwz.org/doc/gc.html">luminaries</a>have waxed geeky on why garbage collection is so great, so I’ll keep this brief. Basically there’s two reasons it’s awesome: I really don’t have time to learn the rules of memory ownership and when to free, and all that stuff for yet another language. Secondly, I just can’t think of another language feature that accelerates development as much as garbage collection. Android has it, and for anything not pushing the envelope in terms of what the hardware can do (ie, almost everything), it works really well. I might have understood why Apple didn’t include it in the first couple of iPhones — it does come at a computational premium — but they really don’t have an excuse any more. It’s a big enough deal that I would advise the first-time mobile app developer to start on Android for this reason alone.</p></blockquote>
<p>Jesus, there are so many things wrong with this, but mostly it reinforces wrong and lazy ideas about memory management.  His statement, &#8220;<em>I really don’t have time to learn the rules of memory ownership and when to free, and all that stuff for yet another language</em>&#8221; is about the most ignorant thing I&#8217;ve ever heard a developer say.  Basic object-oriented memory management is universal, no matter if you are doing C++, ObjectPascal or any other non-garbage collected language.  It&#8217;s some pretty basic computer science and the concepts, specifically how they apply to Objective-C, are nearly trivial.  If I were interviewing someone for a job and they said something similar, the interview would be concluded at that point.</p>
<p>I agree with him that the iPhone should have GC though, so don&#8217;t think for a second I don&#8217;t see its advantages.  That&#8217;s why you should listen when I tell you that manual memory management is not a significant effort, specifically in Objective-C.  I sometimes, though rarely, use it for Cocoa desktop apps that I build, and I build a lot of them.  The typical use case for me to use GC is when a quick app idea I&#8217;m sketching out in XCode becomes a fairly full featured thing.  So while I am in the process of exploration, GC is on because I&#8217;m not typically writing code I&#8217;ll reuse.  Once my idea is proven, however, the GC goes off and I refactor all of that code.  The other case where I have it on is when I discover a leak in cocoa itself, but even that is pretty rare.</p>
<p>So if I use GC sometimes, why don&#8217;t I use it all of the time?  Because it&#8217;s so easy to do memory management that I don&#8217;t need it.  I also tend to write a lot of private frameworks that I re-use across all sorts of projects, which may or may not have GC turned on.</p>
<p>The thing is, the retain/release model in Objective-C is roughly a middle ground from no memory management to a basic GC so you are already ahead of the curve.  Unlike C++ or ObjectPascal, you actually never technically &#8220;free&#8221; objects you are using, you simply are saying &#8220;I&#8217;m done with you&#8221; and then let the runtime take care of the rest.</p>
<h2>The Four Basic Rules</h2>
<p>There are only 4 basic rules when it comes to memory management in Objective-C:</p>
<ul>
<li>If you own it, release it.</li>
<li>If you don&#8217;t own it, don&#8217;t release it.</li>
<li>Override dealloc in your classes to release the fields that you own.</li>
<li>Never call dealloc directly.</li>
</ul>
<p>That&#8217;s it.  The first two are the most important and we&#8217;ll get into the basics of object ownership next.</p>
<h2>Rule #1.  When You Own It.</h2>
<p>You own an object in Objective-C when you <strong>alloc</strong> it, <strong>copy</strong> it or <strong>new</strong> it.  For example:</p>
<script src="http://gist.github.com/734215.js"></script>
<p>How easy is that?  I can&#8217;t even think of anything more to write about this, it&#8217;s that simple.  But to reiterate:</p>
<ul>
<li>You own it if you <strong>alloc</strong> it.</li>
<li>You own it if you <strong>copy</strong> it.</li>
<li>You own it if you <strong>new</strong> it.  (New is simply a shortcut for alloc/init).</li>
</ul>
<p>James if you are still reading this, you are 1/4th the way to learning it buddy.  Just stick with me for the next couple of bits and you&#8217;ll be writing software I trust in no time.</p>
<h2>Rule #2.  When You Don&#8217;t Own It.</h2>
<p>This is the tricky part.  You don&#8217;t own it when you don&#8217;t own it.  So if you did not <strong>alloc</strong> it, or you did not <strong>copy</strong> it, or you did not <strong>new</strong> it &#8211; you don&#8217;t own it.</p>
<p>What about the following?</p>
<script src="http://gist.github.com/734233.js"></script>
<p>Do you own that NSString?  Nope, you didn&#8217;t <strong>alloc</strong>/<strong>copy</strong>/<strong>new</strong> it.</p>
<p>The NSImage?  Yes you own this, you <strong>alloc</strong>&#8216;d that bad boy.</p>
<p>The NSData?  Nope, again, you didn&#8217;t <strong>alloc</strong>/<strong>copy</strong>/<strong>new</strong> it.</p>
<p>Here is how that method should really look, btw:</p>
<script src="http://gist.github.com/734252.js"></script>
<p>Yes, it&#8217;s so much harder than GC.  How do us Cocoa devs ever get anything done?</p>
<h2>Rules #3 and #4.  Dealloc.</h2>
<p>Ok, so here&#8217;s the hardest part of the whole thing.  If you have any class with object properties that you retain, you&#8217;ll have to release them when your object has been deallocated via the [dealloc] message.  An example to demonstrate:</p>
<script src="http://gist.github.com/734299.js"></script>
<p>In the above example, our <strong>SomeObject</strong> has two fields: <strong>things</strong> and <strong>someOtherThings</strong>.  You can see in our init method that we create the objects and assign them to our properties.  For <strong>things</strong>, because we are using <strong>[NSMutableArray arrayWithObjects:]</strong>, we have to call <strong>retain</strong>.  Remember, we didn&#8217;t <strong>alloc</strong>, <strong>copy</strong> or <strong>new</strong>, instead we called a convenience method that returns an autoreleased object that we must explicitly retain.  What is autorelease you might be asking?  I&#8217;ll explain in the next part, but for now all you have to remember is the basics of object ownership described above.</p>
<p>In the <strong>[dealloc]</strong> method you&#8217;ll see that we are releasing the objects we created.  Nothing more, nothing less.</p>
<p>James if you have made it this far, you&#8217;ve essentially learned all you really need to know.  Everything below here is optional, but recommended to know.  That wasn&#8217;t so hard was it.</p>
<h2>Properties</h2>
<p>Objective-C 2.0 includes properties, and while I won&#8217;t get into the particulars of them, there is some confusion around the retain/release cycle with regards to them.  There is really only two rules:</p>
<ul>
<li>If you <strong>retain</strong> or <strong>copy</strong> your property, you need to set it to <strong>nil</strong> in your <strong>dealloc</strong>.</li>
<li>If you initialize the property during <strong>init</strong>, <strong>autorelease</strong> it.</li>
</ul>
<p>Let&#8217;s look at some properties:</p>
<script src="http://gist.github.com/734355.js"></script>
<p>The <strong>title</strong> property we are calling a convenience method on NSString that returns an autoreleased object, so we don&#8217;t have to do a thing.  For subtitle, we are allocating a new NSString ourselves, therefore we need to <strong>autorelease</strong> it.  The reason being is that when the value is assigned to the property, the retain count is incremented by 1, so without the autorelease, that object will maintain a retain count of 2 (one for the alloc/init and another for the assignment to the property).  With a retain count of 2, the object will never be released and you&#8217;ll leak memory.</p>
<h2>Autorelease?</h2>
<p>Ok, I totally lied as there is one more complicated aspect to this whole thing, and that complexity comes in the form of Autorelease Pools.  The heck is that?  Autorelease pools maintain a list of objects that will be sent <strong>release</strong> messages when those pools are destroyed.  You really needn&#8217;t worry about when all of that happens, but rest assured that your autorelease objects are, if everything is done correctly, being released without your explicit telling it to do so.</p>
<p>Following our rules of object ownership, one has to wonder what happens to objects that are returned from methods.  Who owns those?  This is where autorelease and autorelease pools come in to play.  For example:</p>
<script src="http://gist.github.com/734324.js"></script>
<p>Who owns that NSImage?  Well, in this example, the caller of this method is the owner and it is, therefore, the caller&#8217;s responsibility to release it.  However, this method is <strong>bad form</strong>.  The correct form:</p>
<script src="http://gist.github.com/734328.js"></script>
<p>Notice the <strong>autorelease</strong> message at the end.  What happens when <strong>autorelease</strong> is called, the object in question is added to an autorelease pool so that it will receive a <strong>release</strong> message when the pool is destroyed.  The caller needn&#8217;t have to worry about it, nor does the callee.</p>
<p>Autorelease pools can be nested, too.  This is great for situations where you are creating thousands upon thousands of temporary objects and want to keep your maximum memory footprint slim and tight.  Also, if you are doing threaded work, anytime you create a new thread, you&#8217;ll have to create an autorelease pool for that thread.</p>
<h2>Responding to Hacker News</h2>
<p>Below are some responses to some of the more incorrect responses on HN.  From <a href="http://news.ycombinator.com/user?id=orangecat" target="_blank">orangecat</a>:</p>
<blockquote><p><span style="color: #000000;">And don&#8217;t get me started on those lazy kids and their assemblers. I mean, how hard is it to remember a few dozen opcode hex values?Less snarkily: Developer resources are not infinite. Time spent futzing with memory management in non-performance-critical areas is time not spent improving performance where it actually matters, adding features, or improving the user interface.</span></p>
<p><em>For example, Angry Birds on the Galaxy Tab versus Angry Birds on the iPad are no where near the same experiences. The Galaxy Tab is jerky and slow, while the iPad is smooth.</em></p>
<p><span style="color: #000000;">The Android code for Angry Birds is primarily in native code, so garbage collection is unlikely to be the cause of your observations. And it&#8217;s perfectly smooth on my Nexus One.</span></p></blockquote>
<p><span style="color: #000000;">He is somehow brilliantly equating the difference between garbage collection and manual memory management in objective-c to the difference between writing raw machine code and writing assembly.  <em>Bravo<strong>. </strong><span style="font-style: normal;">The last line, he tells me Angry Birds is written in native code, which means no garbage collection (the NDK doesn&#8217;t do GC) and doing C++ memory management, which renders the whole point of James Gregory&#8217;s post moot. </span>Slow clap.</em></span></p>
<p><a href="http://news.ycombinator.com/user?id=binaryfinery" target="_blank">binaryfinery</a> says:</p>
<blockquote><p><span style="color: #000000;">I&#8217;ve written memory managers used in console games for the N64 (4Mb), PS2 (32Mb) and a complete memory tracking system for Unreal Engine 3 on XBox360 (OMG 512Mb).I use MonoTouch for iOS development. Why? Because I can build complex object graphs without having to maintain, in my head or in external documentation, the acyclic version of the graph. In a GC language I can create graphs that are happily cyclic, and have every reason to be, and yet have them deallocated when the application nulls the last reference.</span></p>
<p>In a reference counted system that desires cyclic pointers, one or more of those pointers have to be chosen as non-reference-incrementing pointers &#8211; and likewise one must remember not to release them either.</p>
<p>Memory management in Objective-C (without GC) is no effort provided one is making applications with simple object interactions, such as the plethora of tree-based hierarchies. Reference counting is great for that.</p>
<p><span style="color: #000000;">So it could be that the OP is lazy, or it could be that they have experience with problems that you do not. If you believe that OP actually requires a lesson on the basic rules of reference counted memory management (since thats what you provided) then I suggest that you are underestimating the experience of your detractors, which in turn leads me to believe that you are overestimating yours.</span></p></blockquote>
<p>Cyclic object graphs in Objective-C are not a problem.  At all.  In fact the retain/release facilitate them pretty easily, and some very basic comp sci 101 OOP theory will have you moving around them just fine.  Again, it boils down on what you own and what you don&#8217;t own.  Also, if you&#8217;re shit is too complicated to document, you are doing it wrong.  Straight up.  I don&#8217;t care if you wrote the internet or not, btw.</p>
<p>And finally, the one that made me the most irate, from the blog poster himself:</p>
<blockquote><p><span style="color: #000000;">It&#8217;s totally about laziness! But that&#8217;s what computers are all about &#8212; I could store printed versions of all of my documents in a filing cabinet, and go and manually sort them every time I needed a different ordering, but I&#8217;m lazy! I use a database!I just don&#8217;t see why laziness should be restricted to users. Developers are lazy too.</span></p>
<p>You&#8217;re right that there are only 4 rules (or more or less depending on your formulation), but I don&#8217;t care. I&#8217;d rather take the time to have another martini. Or, y&#8217;know, implement features that make my users happy.</p>
<p><span style="color: #000000;">And it definitely gets harder when there are more moving parts. You&#8217;re right that the rules are simple, but the execution of those rules gets more complex as you add more components, more threads, remoting, etc. I never said it was impossible, or up there with Fermat&#8217;s Last Theorem or anything like that. Just that this is work the computer could be doing for me. I <em>want</em> to be lazy, but Apple won&#8217;t let me.</span></p></blockquote>
<p><span style="color: #000000;">Brother, I am all about laziness, but I am also all about doing it <strong>right</strong>.  So, go ahead and drink up friend, because while you are wallowing in your own ignorance, people are out there doing it better than you, smarter than you.  I&#8217;m not sure about you, but in my profession and my career, I am constantly striving to do the best job that I can, and a large part of that goes towards ways to allow myself to be lazier, but to get it right at the same time.  Also, it&#8217;s not a matter of what you <em>want</em>, it&#8217;s a matter of making these damn computer things do what you <em>want</em>.  The trip, the technical aspects of it, have to be done right or your ship is going to sink.</span></p>
<h2><span style="color: #000000;">In Summary</span></h2>
<p><span style="color: #000000;">As you can see, this whole thing is not as complicated or scary as it seems.  Sure, GC is a lot easier, but even if you had it, there are so many instances where manual memory management is preferred: games and media apps.  Memory management is such a crucial aspect of the performance of these types of apps that not knowing how to do it wouldn&#8217;t work.  Thankfully, Cocoa and Objective-C make it pretty painless.</span></p>
<h2><span style="color: #000000;">For More Info</span></h2>
<p><span style="color: #000000;">Really, the only document you need to read is the one apple provides <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html" target="_blank">here</a>.  But, in case you need some more help, the following links are fantastic:</span></p>
<ul>
<li><a href="http://www.cocoadev.com/index.pl?MemoryManagement" target="_blank">http://www.cocoadev.com/index.pl?MemoryManagement</a></li>
<li><a href="http://www.wooji-juice.com/blog/cocoa-6-memory.html" target="_blank">http://www.wooji-juice.com/blog/cocoa-6-memory.html</a></li>
<li><a href="http://www.dikant.de/2007/08/23/cocoa-memory-management-101/" target="_blank">http://www.dikant.de/2007/08/23/cocoa-memory-management-101/</a></li>
<li><a href="http://mattpatenaude.com/ch3-memory-management.html" target="_blank">http://mattpatenaude.com/ch3-memory-management.html</a></li>
<li><a href="http://aspirement.com/2009/09/mastering-cocoa-memory-management-on-the-iphone/">http://aspirement.com/2009/09/mastering-cocoa-memory-management-on-the-iphone/</a></li>
<li><a href="http://www.duckrowing.com/2010/03/08/the-cocoa-memory-management-rules-of-the-road/" target="_blank">http://www.duckrowing.com/2010/03/08/the-cocoa-memory-management-rules-of-the-road/</a></li>
<li>and on and on and on&#8230;</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/objective-c-memory-management-for-lazy-people/feed/</wfw:commentRss>
		<slash:comments>103</slash:comments>
		</item>
		<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>Bootstrapping Technology For Eight Bucks a Day</title>
		<link>http://interfacelab.com/bootstrapping-technology-for-eight-bucks-a-day/</link>
		<comments>http://interfacelab.com/bootstrapping-technology-for-eight-bucks-a-day/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 03:50:51 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[37signals]]></category>
		<category><![CDATA[Massify]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[pragmatism gone wild]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=53</guid>
		<description><![CDATA[In the following article, I&#8217;m going to attempt to describe how one might bootstrap the technical components of their startup as cheaply as humanly possible, while still giving you room to easily scale. A lot of this is based on what I&#8217;ve learned in the last 2+ years as the CTO of massify.com, an online [...]]]></description>
			<content:encoded><![CDATA[<p>In the following article, I&#8217;m going to attempt to describe how one <em>might</em> bootstrap the technical components of their startup as cheaply as humanly possible, while still giving you room to easily scale.  A lot of this is based on what I&#8217;ve learned in the last 2+ years as the CTO of <a href="http://massify.com">massify.com</a>, an online networking site for film makers.  Your mileage may vary, of course, and I&#8217;m sure there are a variety of alternatives to some of the things I&#8217;ll be recommending.  I welcome all commenters to pitch in with their own experiences in hopes that we can grow this post into a useful resource for up and coming entrepreneurs.</p>
<p><span id="more-53"></span></p>
<p>There are some caveats.  You will need to be mildly technical to pull this off, but Google is there to hold your hand through some of the tough stuff which, honestly, isn&#8217;t that tough.  And while some of the concepts might fit Microsoft developers, this article is Linux/BSD/Unix focused.  I will tell you, though, from years of being a .NET consultant, trying to bootstrap a Microsoft centric solution is futile if you&#8217;re trying to sell your ideas as cheaply as possible.  I&#8217;m not trying to start a flame war, I&#8217;m just sayin&#8217;.</p>
<h2>Register Your Domain</h2>
<p>Let&#8217;s start at the top.  You&#8217;ve got a great idea for a website.  You&#8217;ve done the leg work, written up your business plan and are eager to get going on actually building out the product and the supporting internals for the company. The first thing we&#8217;re going to need to do is register a domain.</p>
<p>You have a variety of options here, and depending on the domain and it&#8217;s TLD, you&#8217;ll have various pricing levels to work with.  For the sake of the argument, let&#8217;s assume you are going to register a .COM instead of the more expensive .TV, .ME, .JOBS and others.  Any one of these is a fine registrar:</p>
<ul>
<li><a href="http://namecheap.com " target="_blank">namecheap.com </a>- $9.68</li>
<li><a href="http://godaddy.com" target="_blank">godaddy.com</a> &#8211; $9.99</li>
<li><a href="http://powerpipe.com" target="_blank">powerpipe.com</a> &#8211; $10</li>
</ul>
<p>I also prefer to do private domain registration, which can add $15 to the purchase.  This is entirely optional but it&#8217;s a good way to protect your private information.  If you are registering a .US domain, though, you can&#8217;t do private registrations.  Also, some places protect the privacy of your registration differently, so do some research if this is important to you.  Namecheap has free private domain registration.  Godaddy charges extra and I&#8217;m not sure about PowerPipe.  <strong></strong></p>
<p><strong>Startup cost: $10</strong></p>
<p><strong><br />
</strong></p>
<h2>DNS Made Easier</h2>
<p>This part is entirely optional, but after the register.com fiasco, it&#8217;s worth the peace of mind to run your DNS through a third party.  I love <a href="http://dnsmadeeasy.com" target="_blank">DNSMadeEasy</a> for a variety of reasons, chiefly DNS changes are live nearly instantly, but they also scale incredibly well, providing options for doing round robin DNS to balance DNS requests between load balancers, as well as fail over monitoring.  They also have an API which you can use in your applications for all kinds of neat stuff.</p>
<p>Best part?  Cheap.  <strong></strong></p>
<p><strong>Run Rate: $0.08 a day</strong></p>
<p><strong><br />
</strong></p>
<h2>The Nuts and Bolts</h2>
<p>So we have a domain now.  The next thing we&#8217;ll need to do is setup some infrastructure so our team can communicate and collaborate as we start building this mother out.</p>
<p>I will confess that I made a very dumb move when we were getting Massify going.  Having spent the prior 7 years as a .NET consultant, all I really knew was Windows and the host of enterprisey things you find on Windows.  Having done some SharePoint consulting, I decided SharePoint was the way to go.  An avid Outlook and Blackberry user, the only option I could see through my rose tinted glasses was Microsoft Exchange.  So we hooked up Microsoft Exchange with BES for my blackberry addiction and off we went to focus on bigger and better things.  Fast forward a year and a half and the company has grown to 14+ people and our monthly hosted Exchange bill was close to $450 a month.  The worst part?  Massify is an entirely Apple OS X shop.  The only Windows machines we own are the ones we run in virtualization software.  So while we were running Exchange (and have all moved on from Blackberries to iPhones) we were not reaping any of its benefits.  No calendaring, contact sharing, none of that.  Had we been on Exchange 2007, we might have been able to capitalize on some of those basic things, but we were stuck on Exchange 2003 which doesn&#8217;t work with any OS X productivity apps without some spendy third party utilities that only sometimes work.</p>
<p>So, yeah, dumb move Gilkison!</p>
<p>Well I am here to tell you that you can get NEARLY all the benefits of Exchange for FREE.  Yes, you read right:  FREE.  My friends, I would like to introduce you to <a href="http://www.google.com/apps/intl/en/business/index.html" target="_blank">Google Apps for Business</a>.</p>
<p>Shared calendaring?  Check.  Contact syncing?  Check.  Kick ass web interface for reading/writing mail?  Check.  IMAP support for desktop and handheld clients?  Check.  Push email?  Negative.</p>
<p>I did say NEARLY.</p>
<p>And the best part, beyond the awesome price tag?  I don&#8217;t have to install/maintain shit.  I&#8217;ve been test driving Google Apps for Business for awhile for my personal stuff, to make sure that it has legs, and I couldn&#8217;t be happier.  So we transitioned off Exchange and we are all now much happier individuals.</p>
<p><strong>Run Rate: FREE</strong></p>
<p><strong><br />
</strong></p>
<h2>Working Together</h2>
<p>There are a ton of collaborative apps out there and they&#8217;re all like gloves.  Each has their own utility and fits its wearers in different ways.</p>
<p>The most popular, of course, is <a href="http://basecamphq.com" target="_blank">BaseCamp</a> from 37Signals.  I won&#8217;t get into what I think of 37Signals, but suffice to say a lot of people have a huge app crush on BaseCamp.  I personally, and this is just my opinion, don&#8217;t particularly care for it.  Everything starts out well, but once you get into the thick of big projects it becomes a bit of a mess.  But, like I said, different strokes for different folks.  Nothing I&#8217;m preaching here is gospel, so find out what works best for you.</p>
<p>Now you get a variety of collaborative tools with the Google thing, in addition to the email.  Google docs is great for document sharing, but the other apps we&#8217;re not so keen on.  We&#8217;ve found a nice balance between <a href="http://pivotaltracker.com" target="_blank">PivotalTracker</a> and <a href="http://pbwiki.com" target="_blank">PBWiki</a>.  Both apps cost money and there are a lot of free, open source alternatives.  In fact, we use Deki Wiki from <a href="http://www.mindtouch.com/" target="_blank">Mindtouch</a> for our development wiki.  For agile development, there are quite a few free backlog management apps, but Pivotal is easy enough for everyone to use, so that&#8217;s what we went with.</p>
<p>However, the goal of this article is to do things cheaply, but functionally.  There is no open source equivalent of BaseCamp, so let&#8217;s price with BaseCamp in mind.</p>
<p><strong>Run Rate: $0.78</strong></p>
<p><strong><br />
</strong></p>
<h2>Let&#8217;s Talk Hosting</h2>
<p>The following is based on the assumption that you&#8217;re planning to build a web app based on today&#8217;s standards.  This means you&#8217;ll be using some kind of framework like Rails, or a language like PHP, with a database backend like PostgreSQL.  Yeah I could have said MySQL, but why when PostgreSQL exists?</p>
<p>I imagine this will be the most &#8220;controversial&#8221; aspect of this article because every techie has their own vision of the right way of setting things up.  I also want to talk to you about planning for scale.</p>
<p>&#8220;Premature optimization is the root of all evil&#8221; is a phrase people like to bandy about when people approach the topic of planning for scale before the need for it.  Well, first things first, the phrase is often taken way out of the context in which it was used.  Obviously you don&#8217;t need to unroll your loops in PHP, because listen, you&#8217;re using PHP and optimizing it&#8217;s performance doesn&#8217;t have a substantial effort/reward ratio.  Same goes with Rails/Ruby and Django/Python.  But, it doesn&#8217;t mean that you can&#8217;t plan to be successful and get things in the right position for when you do need to scale.  There are some easy things you can do to make sure when the need comes at you full bore, you&#8217;ll be ready for it.</p>
<p>So let&#8217;s talk about hosting.  The Web 1.0 way was to colocate racks of metal in a datacenter somewhere or to pay for managed hosting through someone like RackSpace.  But that is a crazy thing to do now, it only makes sense when your site hits a certain threshold of scale.  If you&#8217;re at that scale now, what are you doing reading this article?</p>
<p>We&#8217;re working under the pretense that your business idea has to prove itself in the marketplace and that you need to get it into the marketplace as cheaply as possible.  So what options do we have?  You can sign up for a VPS like dreamhost, but you&#8217;ll be cursing yourself when your site hits the front page of Digg or Reddit and the social news monkeys are kicking the shit out of your application.  A day is forever in internet time, so what are you going to do when you need to take your site down for a couple of days to migrate it to something else?</p>
<p>The next step up, then, is this &#8220;cloud&#8221; thing everyone is talking about.  And it&#8217;s the right step.  However, a number of people instantly assume we are talking about Amazon&#8217;s EC2 offering, and that is the wrong way to go.  Bandwidth on Amazon is not the cheapest thing around and I&#8217;m of the fundamental belief that EC2&#8242;s best purpose is for bursts of additional computing resources.  For instance, on Massify, we use EC2 to do parallel processing of media.  We can scale it up and down at will, taking it entirely off-line if we need to.  Having priced out hosting our site on EC2, the end costs were not that far off of managed hosting.  So where does that leave us?  I heartily recommend SliceHost.  And I recommend the following starting setup:</p>
<p style="text-align: center;"><img class="size-full wp-image-57 aligncenter" title="starting" src="http://interfacelab.com/wp-content/uploads/2009/04/starting.png" alt="starting" width="400" height="250" /></p>
<p>Obviously, this isn&#8217;t the absolute cheapest way to start out, but it prepares you for growth.  If you develop with a <strong>share nothing</strong> architecture, you can easily slot in more application servers as needed, and take them down as needed, until such a point as your database becomes a bottleneck.  But that shouldn&#8217;t happen for awhile and the total cost for this rig on Slicehost?  $160 a month.  If you were to stuff it all into a single slice, you&#8217;d go for their 4GB or 8GB slices which run $250 and $450 respectively.  <strong></strong></p>
<p><strong>Run Rate: $5.20</strong></p>
<p><strong><br />
</strong></p>
<h2>Spread The Love Around</h2>
<p>Like the DNS management, this step is entirely optional, but if you&#8217;re serving a ton of static content or media, I can&#8217;t imagine going without for very long.  The advantage of serving all your static content (this includes static html pages, javascript, css, images, media) is that it&#8217;s way cheaper than serving off even Slicehost and it takes a ton of load off.</p>
<p>Good news for you is that CDN&#8217;s are getting cheaper and cheaper.  We use <a href="http://pantherexpress.net" target="_blank">Panther Express</a> at Massify and our monthly bill is amazingly low.  I can&#8217;t really publish those numbers, so let&#8217;s talk about Amazon&#8217;s <a href="http://aws.amazon.com/s3/" target="_blank">S3</a> and <a href="http://aws.amazon.com/cloudfront/" target="_blank">CloudFront</a> pricing to get a rough number.  The basic way this works is that all of your static content gets pushed to S3 and then served up to your users via CloudFront.  CloudFront has a lot of nice features, though if you&#8217;re planning on doing video through it, you might look elsewhere as they don&#8217;t support HTTP chunking &#8211; which is the poor man&#8217;s way of doing video streaming and seeking.  Panther Express does support this, though <a href="http://www.bitgravity.com/" target="_blank">BitGravity</a> has some awesome looking capability as well.</p>
<p>Anyways, assuming we&#8217;re going to transfer 250 GB of data via our CDN, the numbers will surprise you:</p>
<p>S3 Storage and Transfer: <strong>$37.50 for 250GB</strong></p>
<p>CloudFront delivery: <strong>$42.50 for 250GB</strong></p>
<p>Combined, you would be paying roughly $80 a month for 250GB of data transfer.  For most sites that&#8217;s overestimating, but if you&#8217;re doing a user content generated site, you should probably aim higher.  I&#8217;m not at liberty to divulge how much we transfer per month, but it&#8217;s significant.  And despite that our CDN bills are shockingly low.</p>
<p><strong>Run Rate:  $2.63</strong></p>
<p><strong><br />
</strong></p>
<h2>Total It Up</h2>
<p>So we&#8217;ve covered everything from domain registration to company infrastructure to hosting and content distribution.  Here&#8217;s how the costs breakdown:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top"><span>DNS</span></td>
<td valign="top"><span>$0.08</span></td>
</tr>
<tr>
<td valign="top"><span>Email</span></td>
<td valign="top"><span>$0.00</span></td>
</tr>
<tr>
<td valign="top"><span>Collaboration </span></td>
<td valign="top"><span>$0.78</span></td>
</tr>
<tr>
<td valign="top"><span>Hosting</span></td>
<td valign="top"><span>$5.20</span></td>
</tr>
<tr>
<td valign="top"><span>CDN</span></td>
<td valign="top"><span>$2.63</span></td>
</tr>
</tbody>
</table>
<p>For a grand total of: <strong>$8.69 a day.</strong> Not too shabby.</p>
<p>So those are my tips based on my experience for the last two plus years.  Would love to hear comments and tips from others!</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/bootstrapping-technology-for-eight-bucks-a-day/feed/</wfw:commentRss>
		<slash:comments>70</slash:comments>
		</item>
		<item>
		<title>NGINX + PHP-FPM + APC = Awesome</title>
		<link>http://interfacelab.com/nginx-php-fpm-apc-awesome/</link>
		<comments>http://interfacelab.com/nginx-php-fpm-apc-awesome/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 04:42:15 +0000</pubDate>
		<dc:creator>Jon Gilkison</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php-fpm]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://interfacelab.com/?p=29</guid>
		<description><![CDATA[The following guide will walk you through setting up possibly the fastest way to serve PHP known to man.  If there is a faster way, I've not yet found it climbing through zillions of blog posts out there on the subject.  In this article, we'll be installing nginx http server, PHP with the PHP-FPM patches, as well as APC.  The end result?  Pure awesome.]]></description>
			<content:encoded><![CDATA[<p>The following guide will walk you through setting up possibly the fastest way to serve PHP known to man.  If there is a faster way, I&#8217;ve not yet found it climbing through zillions of blog posts out there on the subject.  In this article, we&#8217;ll be installing nginx http server, PHP with the PHP-FPM patches, as well as APC.  The end result?  Pure awesome.<br />
<span id="more-29"></span></p>
<h3>Some Background</h3>
<p>For the last 2+ years, we&#8217;ve been running Apache with mod_php at <a href="http://massify.com/" target="_blank">Massify</a>.  And, for the most part, it&#8217;s been painless and hassle free.  But as traffic increases, we&#8217;ve noticed Apache struggling to keep up.  It chews up memory like crazy and don&#8217;t even get me started about CPU.  Add in the fact that we are currently moving the site from co-located metal to hosting on the cloud, we&#8217;ve needed to find a faster way to serve up these pages on machines that are a quarter of the spec we had with the metal.</p>
<p>It&#8217;s a pretty well know fact by now that <a href="http://nginx.net/" target="_blank">Nginx</a> (pronounced engine-x, though I call it n-jinx) in typical scenarios outpaces Apache on all kinds of fronts: i/o, cpu, memory, reqs/sec.  Feel free to <a href="http://www.google.com/search?q=nginx+vs+apache" target="_blank">google</a> around for more comparitive information.  I can tell you from my own informal load testing on both setups of Massify that I&#8217;ve seen a pronounced difference between the two, specifically in the configuration I&#8217;ll be writing about in this post.  I won&#8217;t be posting numbers, because my testing wasn&#8217;t scientific and it&#8217;s not really the focus of this article, but I am more than confident that we&#8217;ll be getting a level of performance a few steps above Apache.  (Please don&#8217;t write me telling me I can recompile and reconfigure Apache to approach Nginx&#8217;s performance.  I&#8217;m certain you are correct, but I don&#8217;t really care at this point.)</p>
<h3>PHP-FPM?</h3>
<p>The typical nginx configuration involves using <em>spawn-fcgi</em> from the LightTPD project to get nginx serving up PHP.  There are a few issues with spawn-fcgi that are covered on more in-depth posts across the interwebs, so I won&#8217;t rehash them here.  Suffice to say, it can lead to major headaches, so we want to do what we can to avoid those headaches.</p>
<p>Enter <a href="http://php-fpm.anight.org/" target="_blank">PHP-FPM</a>, which stands for PHP FastCGI Process Manager.  It is actually a set of patches for the PHP source that bake in FastCGI process management into your PHP binaries.</p>
<p>Note: Even if you are sticking with Apache, there are a variety of reasons to skip mod_php and go straight for PHP via FastCGI.  For starters, with mod_php, each request that Apache handles loads PHP &#8211; and all of it&#8217;s libraries.  That&#8217;s an epic shit ton of overhead.  With FastCGI, PHP acts more akin to an application server.  PHP-FPM, as well as spawn-fcgi, manage this; loading and killing PHP instances as needed.  This has a lot of benefits, not the least of which is reduced memory overhead.</p>
<h3>Let&#8217;s Get Rocking</h3>
<p>We&#8217;re using a fresh install of Ubuntu 8.10 Intrepid.  With a little tooling, this can work equally well on other linux distros.  I&#8217;ve deployed this on CentOS, with minor changes, just fine.</p>
<p>First things first, we&#8217;re going to have to install all the dependencies for building everything:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev
</pre>
<p>Now that we have everything we need, now it&#8217;s time to &#8230;</p>
<h3>Compile PHP</h3>
<p>We&#8217;re going to download the source for PHP 5.2.8, as well as the matching patches for PHP-FPM.  We&#8217;ll then apply the patches and get compling.</p>
<pre class="brush: bash; title: ; notranslate">
cd /usr/local/src/
sudo wget http://us.php.net/get/php-5.2.8.tar.gz/from/this/mirror
sudo tar zvxf php-5.2.8.tar.gz
sudo wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz
sudo gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | sudo patch -d php-5.2.8 -p1
cd php-5.2.8
sudo ./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --disable-pdo --with-pgsql --with-curl --disable-debug --enable-pic --disable-rpath --enable-inline-optimization --with-bz2 --with-xml --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-xslt --enable-memcache --enable-zip --with-pcre-regex
sudo  make all install
sudo  strip /usr/local/bin/php-cgi
</pre>
<p>The above should have been completely auto-pilot.  If you run into any errors, more than likely you don&#8217;t have some of the dependencies installed.  <strong>Note</strong>: Make sure you enable and disable whatever PHP configuration options you need for your particular setup.  The above are what we use at Massify, more or less.  (We actually apply a couple of other custom patches, but that&#8217;s another post for another day).</p>
<p>While we are at it, let&#8217;s install some of the modules we&#8217;re going to need via PECL:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo pecl install memcache
sudo pecl install apc
sudo pecl install syck-beta
</pre>
<p>Now, when you build the APC extension, make sure you turn off the option to compile for apache.  It will prompt you to make this change.</p>
<p>Let&#8217;s copy the stock php.ini:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo cp /usr/local/src/php-5.2.8/php.ini-recommended /usr/local/lib/php.ini
</pre>
<p>Finally, let&#8217;s setup symbolic links to make things easier to find:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo mkdir /etc/php/
sudo ln -s /usr/local/lib/php.ini /etc/php/php.ini
sudo ln -s /usr/local/etc/php-fpm.conf /etc/php/php-fpm.conf
</pre>
<p>That&#8217;s it for compiling PHP.  The only thing we have left to do is change some setting in the php-fpm conf.  Open up <strong>/etc/php/php-fpm.conf</strong> in your editor of choice.  Do some searching and change the users who own the processes to www-data.  The file is too big to post in here, but the values we want to change are on lines 51, 52, 63 and 66.  Should look something like:</p>
<pre class="brush: xml; light: true; title: ; notranslate">
&lt;value name=&quot;owner&quot;&gt;www-data&lt;/value&gt;
&lt;value name=&quot;group&quot;&gt;www-data&lt;/value&gt;
&lt;value name=&quot;user&quot;&gt;www-data&lt;/value&gt;
&lt;value name=&quot;group&quot;&gt;www-data&lt;/value&gt;
</pre>
<p>Done!</p>
<h3>NGINX</h3>
<p>This is just as easy as compiling PHP was:</p>
<pre class="brush: bash; title: ; notranslate">
cd ..
sudo wget http://sysoev.ru/nginx/nginx-0.6.35.tar.gz
sudo tar zxvf nginx-0.6.35.tar.gz
sudo rm -f nginx-0.6.35.tar.gz
cd nginx-0.6.35
sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module
sudo make &amp;&amp; sudo make install
</pre>
<p>Let&#8217;s set up some links:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo ln -s /usr/local/nginx/conf /etc/nginx
</pre>
<p>Now the next step is optional, but this is what I&#8217;ve been using so far.  Let&#8217;s open up <strong>/etc/nginx/nginx.conf</strong> in our editor and go to town.  Salt and pepper to taste:</p>
<pre class="brush: jscript; title: ; notranslate">
user  www-data;
worker_processes  6;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  10 10;

    gzip  on;
    gzip_comp_level 1;
    gzip_proxied any;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        log_format main '$remote_addr - $remote_user [$time_local] '
                  '&quot;$request&quot; $status  $body_bytes_sent &quot;$http_referer&quot; '
                  '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';

        access_log  /var/log/nginx_access.log  main;

        error_log  /var/log/nginx_error.log debug;

        include /usr/local/nginx/sites-enabled/*;
}
</pre>
<p>We&#8217;re also going to have to set up some FastCGI parameters so that PHP doesn&#8217;t choke and you&#8217;ll avoid random 503 errors from nginx.  Open up <strong>/etc/nginx/fastcgi_params</strong> and add the following:</p>
<pre class="brush: bash; title: ; notranslate">
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
</pre>
<p>Finally, let&#8217;s create a SystemV style init script and store it in <strong>/etc/init.d/nginx</strong>.  This script was lifted from <a href="http://articles.slicehost.com/2007/10/17/ubuntu-lts-adding-an-nginx-init-script">here</a>.</p>
<pre class="brush: bash; title: ; notranslate">
#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case &quot;$1&quot; in
  start)
        echo -n &quot;Starting $DESC: &quot;
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
                --exec $DAEMON -- $DAEMON_OPTS
        echo &quot;$NAME.&quot;
        ;;
  stop)
        echo -n &quot;Stopping $DESC: &quot;
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
                --exec $DAEMON
        echo &quot;$NAME.&quot;
        ;;
  restart|force-reload)
        echo -n &quot;Restarting $DESC: &quot;
        start-stop-daemon --stop --quiet --pidfile
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo &quot;$NAME.&quot;
        ;;
  reload)
      echo -n &quot;Reloading $DESC configuration: &quot;
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid
          --exec $DAEMON
      echo &quot;$NAME.&quot;
      ;;
  *)
        N=/etc/init.d/$NAME
        echo &quot;Usage: $N {start|stop|restart|force-reload}&quot; &gt;&amp;2
        exit 1
        ;;
esac

exit 0
</pre>
<p>Don&#8217;t forget to set executable permissions on it!</p>
<h3>Set Up Your Site</h3>
<p>This part you&#8217;ll have to tool yourself, but here&#8217;s a rough guide.</p>
<p>First we&#8217;ll have to create a directory to store our site(s) conf files:</p>
<pre class="brush: bash; light: true; title: ; notranslate">
sudo mkdir /usr/local/nginx/sites-enabled
sudo ln -s /usr/local/nginx/sites-enabled /etc/sites
</pre>
<p>And now let&#8217;s add a conf file for our default site at <strong>/etc/sites/default.conf</strong>.  The contents:</p>
<pre class="brush: jscript; title: ; notranslate">
server {
        listen *:80;

        location / {
                root   /var/www/default/pub;
                index index.php;

                # if file exists return it right away
                if (-f $request_filename) {
                        break;
                }

                # otherwise rewrite the fucker
                if (!-e $request_filename) {
                        rewrite ^(.+)$ /index.php$1 last;
                        break;
                }

        }

        # if the request starts with our frontcontroller, pass it on to fastcgi
        location ~ ^/index.php
        {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME /var/www/default/pub$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
        }
}</pre>
<p>The conf file above is for front controller style sites, which include wordpress, cake, etc.  It also takes care of serving up static content without going through FastCGI.  Note:  You will need to change <strong>/var/www/default</strong> to wherever the files for your site reside.</p>
<h3>Start It Up</h3>
<p>Now all we have to do to get it rocking:</p>
<pre class="brush: bash; title: ; notranslate">
sudo php-fm start
sudo /etc/init.d/nginx start
</pre>
<p>You should be able to test your site out and see it working.  Any questions, feel free to ask.</p>
<h3>Reference</h3>
<ul>
<li><a href="http://nginx.net/">Nginx</a></li>
<li><a href="http://www.joeandmotorboat.com/2008/02/28/apache-vs-nginx-web-server-performance-deathmatch/">Nginx vs Apache Death Match</a></li>
<li><a href="http://blog.webfaction.com/a-little-holiday-present">10,000 req/s with Nginx</a></li>
<li><a href="http://bethesignal.org/blog/2009/04/06/replacing-apache-with-nginx-for-static-file-serving/">Replacing Apache With Nginx For Static File Serving</a></li>
<li><a href="http://php-fpm.anight.org/">PHP-FPM</a></li>
<li><a href="http://www.softwareprojects.com/resources/programming/t-how-to-install-php-fpm-spawn-fcgi-replacement-1602.html">How to Install PHP-FPM</a></li>
<li><a href="http://www.igorclark.net/2008/07/14/php-fpm-a-smoother-php-fastcgi-process-manager.html">PHP-FPM, A Smoother PHP FastCGI Process Manager</a>
</li>
</ul>
<p>&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://interfacelab.com/nginx-php-fpm-apc-awesome/feed/</wfw:commentRss>
		<slash:comments>180</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>

