Monday 27 July 2009

Switch

Hi Readers,

Today I found a bit unusual usage of switch. What if you have different N options and you have to invoke M common commands and N different commands? How you can save some extra typing job for yourself? (Or how to avoid code repetition.)

switch (var) {
case n1:
...
case nN:
common1();
...
commonM();
case n1:
special1();
break;
...
case nN:
specialN();
break;
}

Got it? The trick is using the same case terms multiple times. If you like it check the DRY approach. And of course don't forget about KISS.

Good day,
Peter

Friday 24 July 2009

Drupal Flash interaction example

Hi Readers,

I had to make a presentation about a theoretical workaround describes an approach Drupal and Flash can work together. Basically there are two concepts. One Flash for all - means one big flash object is the whole site, and the other one, lots of flash objects in html. In the first concept the flash object handles everything. In the Drupal era it requires the Services and AMFPHP module. I would say it is rather a communicaton heavy way. On the other side the lots-of-flash way needs large numbers of interactions between the underground html layer. Lets see this approach in the slides:

Sunday 19 July 2009

Flex/AIR PHP evaluator

Hi Readers,

OS X hasn't got [readline] for PHP. That is why you can't use [php -a] in command line for testing PHP codes. Sad. If you want one, you can get a similar from: http://www.fischerlaender.net/php/phpa-norl by Stefan Fischerländer and David Phillips. It's an easy replacement, however, I wanted to use one that can keep my command history. If you use macports, you maybe win but I don't want to use that. (I heard it is kinda evil sometimes.) Recently I have found a bestof documentation of Flex at http://bit.ly/1auMV8. (Have I mentioned it is free? :) Oh yeah.) So I made a really simple AIR php -a replacement.



Concept is stupid easy. An AIR app sends a POST request for a PHP script lies on my localhost. This script evaluate the text I want to check and sends back. See? K, check the code:
The AIR part:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[
public function eval():void {
var url:String = "http://localhost/flex_eval.php";
var ur:URLRequest = new URLRequest(url);
var ul:URLLoader = new URLLoader(ur);
var uv:URLVariables = new URLVariables();

uv.phpsnippet = php_snippet.text;
ur.data = uv;

ur.method = URLRequestMethod.POST;

ul.addEventListener(Event.COMPLETE, evalComplete);
ul.addEventListener(IOErrorEvent.IO_ERROR, ioError);

ul.load(ur);
}

public function ioError(event:IOErrorEvent):void {
trace(event);
php_result.text = "Missing PHP file from:\n" +
"http://localhost/flex_eval.php\n\n" +
"Create one with the following code:\n\n" +
"<?php\n" +
"echo 'Flex PHP Snippet results ('.date('H:i.s').')'.\"\\n\\n\";\n" +
"eval($_POST['phpsnippet']);";
}

public function evalComplete(event:Event):void {
trace(event);
php_result.htmlText = event.target.data;
}
]]>
</mx:Script>

<mx:TextArea left="10" right="279" top="10" bottom="40" id="php_snippet" backgroundColor="#2E2E2E" fontFamily="Courier New" color="#FFFFFF" fontWeight="normal" fontSize="12"/>
<mx:TextArea width="261" right="10" top="10" bottom="10" id="php_result"/>
<mx:Button label="Eval" bottom="10" left="10" right="279" click="eval();"/>
</mx:WindowedApplication>

And the PHP part:
<?php
echo 'Flex PHP Snippet results ('.date('H:i.s').')'."\n\n";
eval($_POST['phpsnippet']);

You can compile the mxml stuff in Flex Builder 3.0 (and I guess also with the free Flex sdk). And here you are a quick downloadable version:
http://bison.hu/public/PHPSnippet.air

Guddai readers,
Peter