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.

  »

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.

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.

  »

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.

  »

Nuff Said.

Posted by John Kleijn • Monday, April 13. 2009 • Category: PHP


function extraStatics() {
    return $this->extraDBFields();
}

/**
* @deprecated 2.3 Use extraStatics()
*/

function extraDBFields() {
    return array();
}
 

(This gem was taken from SilverStripe)

"Don't blame us, blame PHP"

Posted by John Kleijn • Monday, April 13. 2009 • Category: PHP

At SilverStripe, they've gone through great lengths to rape the OO model (seriously, stay clear). But according to SS, their way is the right way, and PHP has it ALL WRONG. The fact that SilverStripe's codebase looks like my kitchen when the gf is out of town, is as defensive as an 80 year old Amish women in a coma, and about as structurally sound as house of cards, is ALL PHP's fault! Obviously is has nothing to with the way they are trying to use static properties. Right.


/**
* ...
*
* Note: please ensure that the static variable that you are overloading is explicitly defined on the class that
* you are extending.  For example, we have added static $has_one = array() to the Member definition,
* so that we can add has_one relationships to Member with decorators.
*
* If you forget to do this, db/build won't create the new relation.
* Don't blame us, blame PHP! ;-)
*/

 

Adding a winky does not make it funny or right, sorry. Actually, double fail for smilies in code. Triple fail for writing the biggest piece of crap in PHP history (and that's saying a lot, MediaWiki anyone?)

SOAP server in 30 seconds

Posted by John Kleijn • Friday, January 2. 2009 • Category: PHP

I sometimes get the feeling that some PHP developers think SOAP is hard to use. When in most cases, nothing could be further from the truth.

  »
Tagged

Unserialize vs Include, with APC or php://memory

Posted by John Kleijn • Sunday, December 7. 2008 • Category: PHP

As a follow up on my previous post, I decided it would be interesting to see what effect using the php://memory wrapper or APC would have on the results.

  »
Tagged

Unserialize vs Include

Posted by John Kleijn • Sunday, December 7. 2008 • Category: PHP

Cutting straight to chase, the results:

r448191@goliath:~/bench$ php bench.php 10

Total include: 0.000389575958252

Total unserialize: 0.000274181365967

r448191@goliath:~/bench$ php bench.php 100

Total include: 0.0033860206604

Total unserialize: 0.00247645378113

r448191@goliath:~/bench$ php bench.php 1000

Total include: 0.0342044830322

Total unserialize: 0.0263719558716

r448191@goliath:~/bench$ php bench.php 10000

Total include: 0.32866358757

Total unserialize: 0.25150680542

r448191@goliath:~/bench$ php bench.php 100000

Total include: 3.23683238029

Total unserialize: 2.56990027428

  »
Tagged

Zend Framework 1.7: Zend_Db_Table still sucks

Posted by John Kleijn • Sunday, November 30. 2008 • Category: Zend Framework

Zend Framework is now at version 1.7, adding better internationalization support, and supporting the Dijit Editor Widget. Ok, better internationalization support can only be good (since it was already more than decent), but the Editor Dijit should have been in 1.6. I gave the Dojo support in 1.6 a test drive when it was first released, only to find that it was mostly useless. Hopefully that has changed, I haven't tried it yet.

  »

Antiquities and such