Back from Calgary

Posted by John Kleijn • Tuesday, March 22. 2011 • Category: Personal

Last week I was in Calgary (Canada) for a few days to meet some of the people I have been doing business with remotely for a few years now. Despite my throat infection and horrible cough, I had a great time.

Many thanks to Mark Lall and his wife, Christopher Staley and David Nagy :-)

ExtJS RecordFormPanel

Posted by John Kleijn • Wednesday, December 29. 2010 • Category: JavaScript

Say you have a an overview component, like a grid, and you need to edit that. The best approach would be to load the record into the form and invoke BasicForm's "loadRecord". This means you'll be using the Store for persistence though, and BasicForm.submit() no longer does the trick. Especially when creating new records, directly inserting that into the store a grid uses is extra useful. It means the grid wont have to reload it's data from the server and immediately shows the inserted record.

The solution is creating a form type that loads records and uses a store for persistence. This is relatively simple and seems like this functionality should be available in the Ext library. In any case, below is my implementation.

  »

Topological sorting of JavaScript files

Posted by John Kleijn • Wednesday, December 29. 2010 • Category: PHP

Recently I'm doing a lot of ExtJS work. In complex JS app developement you'll quickly have a LOT of files to link to your HTML document. You'll want to combine and compress these files for production and link them sperate and uncompressed for testing.

The simplest solution is just listing the files in a configuration file. But managing this list is a PITA. We're developers, not clerks. So I decided this list should be auto-generated and properly sorted. Scanning the filesystem for javascript files is trivial, and I quickly decided the sort order should be determined by examining the contents, looking for class names. All files contain classes, with a JSDoc declaration of @class app.module.ClassName, so it's not hard to establish which file depends on which.

The real issue is sorting. Before long I came to the conclusion that there's no out of the box solution for the type of sorting I needed, all sort functions use QuickSort, even the ones using UDFs. On WikiPedia, I found that what I needed "resolving dependencies into an ordered list" could be accomplished using a different algorithm called "Topological sorting".

WikiPedia describes one implementation in pseudo code:

L ← Empty list that will contain the sorted elements

S ← Set of all nodes with no incoming edges

while S is non-empty do

remove a node n from S

insert n into L

for each node m with an edge e from n to m do

remove edge e from the graph

if m has no other incoming edges then

insert m into S

if graph has edges then

output error message (graph has at least one cycle)

else

output message (proposed topologically sorted order: L)

I decided to create a PHP implementation of this algorithm.

  »

Where's all the good HipHop?

Posted by John Kleijn • Friday, November 26. 2010 • Category: Personal

Working late, yet again, I often have a news/current events program running on one of my 3 monitors. Today I had to listen to some elitist guys praise the last Kanye West album as "one of the best Hip Hop albums ever made".

Please. Kanye West is of Puff Daddy/Diddy whatever caliber: he sucks. I'd even go as far as saying that kind of stuff is not even Hip Hop.

A few months ago (better late than never) though I discovered a new combination that is stepping up to represent the real hardcore Hip Hop, Hip Hop as it's meant to be: La Coka Nostra. From the Soul Assassins network, you can expect little less than excellence. And La Coka delivers.

La Coka, of course, isn't perfect. It's lyricists consist of Slaine, Ill Bill and Everlast. Slaine's an excellent lyricist, though he seems a bit off sometimes. Everlast at times seems out of time: the style that suited him well in the House of Pain seems out of place at times. But other times he shows the type of skill that validate his position as an all time classic. And then there's Ill Bill. Well, I have no comment on Ill Bill, that guy is just plain awesome. In the above clip you'll also see B-Real, Cypress Hill front man and Soul Assassins catalyst, it would be ridiculous to review his skills.

On a serious note:

Someone on that program explicitly praised the accessibility of Kanye West's work. That's not the right reason to praise an album. I don't like classical music, if I praise someone for making classical more accessible all I'm really saying is "this didn't suck as much". Or is Beethoven praised for the accessibility of classical music that his familiar tunes provide?

Where's all the good Hip Hop?

Story: Doctrine 2 lead up

Posted by John Kleijn • Friday, July 23. 2010 • Category: PHP

I had been waiting for PHP 5.3 for a long time, and looking at the progress for Zend Framework 2.0, I got very frustrated. But it made me keep a closer eye on the ZF Wiki, and when I encountered a proposal for Zend_Db_Mapper, I recognized many of the PoEAA patterns I had abandoned for reasons of practicality, but longed for so much. But the work was far from done, so I emailed the proposer, Benjamin Eberlei, on 19 December last year, offering my assistance. He responded..

Hello John,

I stopped to work on Zend Entity (and Db Mapper) when we realized that

it actually would end up being sort of a clone of the Doctrine 2 project,

so we joined forces and we will see some pretty neat ZF and Doctrine 1 /2

integration very soon.

you are always welcome to contribute to doctrine 2, its quite advanced

already.

greetings,

Benjamin

I was very disappointed. Zend Framework has a certain robustness, a defensiveness, that I've come to expect (although around this same time ZF started to disappoint in this regard). Doctrine, to me, was superior in functionality to Zend_Db_Table, but it definitely lacked defensiveness. It was messy. And "you can't build a palace on a pile of shit", the lesson I extracted from my experience with the SilverStripe CMS.

I responded to Benjamin expressing my disappointment and lack of faith in the quality of Doctrine. And even though he responded with assurances that Doctrine 2 was very different from Doctrine 1, I remained skeptical and disappointed.

Some months passed, and eventually I did have another look at D2, which had reached the last Alpha stage. I didn't really look at the docs, I assumed that it would be better than D1, but remained skeptical. Regardless, a bleak hope of "a better Doctrine" started to glimmer, and I started to investigate. I quickly came across this presentation by Jonathan Wage. He had me at "completely rewritten", but going on to "kill the magic", it really caught my attention. The "magic" had annoyed me greatly, as it is very inflexible, a source of unclear execution flow, and perhaps the primary source of the lack of defensiveness in D1. Going on to replace the magic with proper OO principles, I was sold. This Doctrine was indeed better, fundamentally better. There was more to the presentation, but it was just icing on the cake. The realization that it was still in alpha, tempered my enthusiasm enough to let it be.

Then I came across a project, that had the need for composite objects in a hierarchal structure, that needed to be persistent. Given the possible nesting level of these structures, I needed something that doesn't require many round trips to the database. Enter Nested Set (aka "Modified Preorder Tree Traversal"). Doctrine 1 does support that, but as it turned out, the combination of inheritance using "column aggregation" and nested set, was asking too much. This was mostly due to the poor inheritance mapping in D1. In desperation (and remembering a promise of "better support for inheritance"), I looked at the latest release of Doctrine, which turned out to be 2.0 BETA1. I was pleased the project had finally reached beta status, and started looking at the docs. I became more and more convinced that it was doable to rip out the guts of the application and replace it with D2.

This was a daunting task though, and I did underestimate it. I ended up namespacing the whole application, creating my own Nested Set implementation for use with D2, and overall just making mayor changes to the application. It put mayor stress on the deadline and myself, especially halfway through. But, the end result seemed worth it, and I am truly impressed with Doctrine 2.

Another thing I am anticipation of is Symfony 2. I never cared much for S1, but I had a look at S2 and it looks pretty good. It relies pretty heavily on DI, another prerequisite for effective Domain Driven Development. Regrettably it won't reach beta until next year. Fabien Potencier apparently coined the expression "kill the magic", and seems as impressed with D2 as I am:

And man, Doctrine 2.0 is gorgeous. Doctrine 2.0 is one of best things that's happened to PHP in a long time.

Why you can't (or shouldn't) unserialize exceptions

Posted by John Kleijn • Friday, May 7. 2010 • Category: TDD

Sorry for not posting much, I'm insanely busy, but I can squeeze in a quick post. I already lost lots of time on the topic of this post, so I might as well take 10 minutes more and explain.

I develop using TDD, using PHPUnit. I (usually) use Zend Framework in combination with Doctrine. To be a 100% sure tests are completely isolated, PHPUnit has a nice feature called "Process Isolation", which means as much as that every test is run in its own environment. To get the result of the test into the main environment, PHPUnit serializes and unserializes the "test result" object. Which is fine, I couldn't think of another way to do it.

But, this test result object may include failures, in which case the exception(s) are attached. Which is a problem. A few lines of code is worth a thousand words, so here you go:

jkleijn@goliath:~$ php -a

Interactive shell

php > class Argument { function __wakeup() { throw new LogicException; }}

php > class Subject { function fail($argument){ throw new InvalidArgumentException; }}

php > try { $s = new Subject; $s->fail(new Argument); } catch(Exception $e) { unserialize(serialize($e)); }

PHP Warning: Uncaught exception 'LogicException' in php shell code:1

Stack trace:

#0 [internal function]: Argument->__wakeup()

#1 php shell code(1): unserialize('O:24:"InvalidAr...')

#2 {main}

thrown in php shell code on line 1

php >

As you can see, unserializing the exception triggers the __wakeup() method in the argument that was passed to the failing method. The problem with that is that an Exception object in PHP includes a full backtrace, which, amongst other things, includes arguments to function or method calls. No issue if the arguments are scalar values or a simple array, but an object may not like being serialized, per se.

Such is the case with Doctrine_Record and Doctrine_Collection objects. They can be unserialized, but require that a database connection has been made first, or they will throw: Uncaught exception 'Doctrine_Connection_Exception' with message 'There is no open connection'. Which obviously is not a precondition one would want to have for running test cases. It kinda defeats having test isolation in the first place.

The good news is that this is only an issue if a test fails, so as long as your test passes, you're fine. The bad news is that in order to find out WHY your test failed, you have to hack a file dump of the serialized test result, to find the exception message and origin. Which is a nuisance at best.

Google not indexing my blog

Posted by John Kleijn • Monday, March 29. 2010 • Category: Personal

If you are reading this, you probably kept my old feed in your RSS reader. Using Google Webmaster Tools, I got reminded that people actually subscribed to that. Right now, you lucky (ha!) few are the only ones reading my blog, the greater Internet is being deprived of the privilege because Google is not indexing it. For example search "john kleijn closures", you'll get Eli White's blog because Serendipity created a trackback for it, instead of my own post about closures. Search simply "John Kleijn" and you'll get my company website, some other links such as my PHPFreaks.com profile, and also some other guy called John Kleijn twice my age (okay, 1.5 times -- I'm getting old).

  »

NetBeans is TDD unfriendly!

Posted by John Kleijn • Monday, March 29. 2010 • Category: TDD

I am getting soo tired of Zend Studio for Eclipse. On large projects, or rather projects with large libs (e.g. Zend Framework and Doctrine) code suggest and "building" is just too slow. It is since about a month impossible to blame hardware imitations either.

  »

Notes about closures in PHP

Posted by John Kleijn • Saturday, March 27. 2010 • Category: PHP

When PHP 5.3 came out, I was ecstatic. Namespaces, finally! Actually some parts of the implementation were a bit disappointing, but we'll leave that for another time. In that same enthousiasm, I jumped on closures like a hungry dog on a steak. Only to find the steak to be an old shoe.

  »

Virtual Proxies revisited

Posted by John Kleijn • Thursday, March 25. 2010 • Category: PHP

Of all the well known design patterns related to ORM, the Proxy pattern (or more specifically, Virtual Proxy) is perhaps the most under-appreciated (with Unit of Work Controller and Value List Handler coming in second). This may be because it's not strictly a data source pattern, and you won't find in PoEAA chapter or Core J2EE Patterns. It is actually in the GoF, which doesn't contain any data source patterns. For me personally, Virtual Proxy will always be directly associated with data loading, as that is what I first used it for in 2006, although since then I have used for other occasions where object initialization was abnormally expensive. It is a pretty versatile pattern.

In a nutshell, a Virtual Proxy is a "lazy loading" pattern that defers initialization of an object until it needed. The proxy does not contain the actual resource, but "knows how to get it". It does this by extending a subjects class while delegating to an instance of that same class, the subject. Basically this is what a Decorator does. But instead of overriding methods to add behaviour to a decorated object, it overrides them to trigger initialization of the subject (loading from the database in the case of an ORM), before delegating to the subject. Like with a decorator you'll have to override every method so that it is delegated to the subject. This makes manually writing proxies a pain.

  »

Started on a new framework

Posted by John Kleijn • Wednesday, March 24. 2010 • Category: PHP

Yet again.

Actually, using bits and pieces from previous works of art and a lot of red bars that turned green eventually, it is actually already a usable framework. Which allows me to focus on the more interesting stuff that are going to set this framework apart. Starting with the data layer.

  »

Bad poetry

Posted by John Kleijn • Wednesday, March 24. 2010 • Category: Personal

I tried my hand at poetry. It's deep, philosophical, it is the best poetry ever. Actually it is probably as good as my skills on the guitar.

  »
Tagged

Simple htpasswd for Hudson CI using PHP

Posted by John Kleijn • Wednesday, March 24. 2010 • Category: CI

My development server hosts Trac instances, SVN repos, and private testing/staging websites/applications. All of these use one htpasswd file with users and passwords in it.

But the build server, using Hudson, doesn't natively support that. Here's how you can have Hudson check a standard htpasswd file, and have project based security so one client or subcontractor wont mess with the builds of a project they are not involved in.

  »

Learning to play the guitar

Posted by John Kleijn • Wednesday, March 24. 2010 • Category: Personal

That's right, I decided to join Jimmy Hendrix and.. Some other guys who were pretty good? As soon as I can play anything with more than an E and A minor, maybe I can play some Staind or System of a Down at your wedding (or funeral, same game). But seriously, I'll record something sometime so the Internet can have a little fun at my expense.

(The Stratocaster was "borrowed" from http://technabob.com/ btw).

Tagged

Mandatory Hello World

Posted by John Kleijn • Wednesday, March 24. 2010 • Category: Personal

Hello World,

After I stopped blogging at johnkleijn.nl about a year ago, I recently decided I wanted to start again. While johnkleijn.nl was purely about Web Development, this time I want to get other thoughts off my mind as well. I'm also going to try to write a little more often than once a month, it's not like I don't have a ton of incredibly interesting things to say, even if no one really cares about half of it.

When I write something that makes you want to hire me for PHP or JS development, go over to my company website. But this is also a personal blog, with opinions, bad and poor humor and stuff. When I write something that makes you want to pull my head through your monitor by my (fairly large) ears, troll about it in the comments.

I'll import some of the old posts, not sure if it's worth the trouble. If you visited my old blog and want a specific old post back, comment below.

I may also become active on Twitter, Facebook and LinkedIn, who knows. Before long, you could find yourself reading more John Kleijn than you can stomach.

John Kleijn.

Antiquities and such