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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.