Delicious LinkedIn Facebook Twitter RSS Feed

My PHP Development Environment

I was having problems with XAMPP, I made the huge mistake of installing WebMatrix to try it out, it used my port 80, installed a bunch of crap and wouldn't let me uninstall anything, sure, I can uninstall WebMatrix but not all the programs Web Installer installed.

So I decided to move my development server from my development PC, to my notebook, I set up a virtual machine with Virtual Box, and installed Windows Server 2003 (I was going to install 2008 but I couldn't find the CD, 2003 is good enough though!), I gave it one core of my i3 processor and 512 RAM, and to my surprise, it ran very smoothly, I installed IIS6, .NET 4, IIRF, PHP using FastCGI, MySQL, xDebug and Mongo, also I installed the updates. IIS might not be liked by many, but it's a pretty efficient little webserver! (Note: That benchmark uses IIS 7!)

After I installed everything I set up the network, I gave the virtual server a static ip "192.168.0.110", and using my router's port forwarding I forwarded the ports 80 (for HTTP) and 21 (for FTP), I also forwarded two other ports, 3389 for Remote Desktop and 2082 for PhpMyAdmin (I didn't forward the MySQL nor Mongo ports, they will be used by scripts using localhost).

Then, I set up a no-ip account and using my router's configuration page I synchronized my public dynamic ip with a no-ip domain, so I can use a static ip for my development sever, and remote desktop connections.

Finally I added a Network Location on My Pc, so I could go to W:\www to access IIS's public folder, and I just develop using Sublime Text 2 on that folder!

When I'm not on my LAN, I use Sublime Text 2's FTPSync awesome package to automatically Sync my folder with my FTP! So I can develop at work, then go home, and keep working on the same exact files.

And I can use Remote Desktop to manage my little server everywhere, at LAN or over the internet using my static no-ip.

It's way easier than anything I've tried before, and it's quite nice! I really enjoy my new setup, and the best part is I can mount the VM on other machine, and migrating my server is pretty easy.

Fun!

Converting C# Anonymous Functions to SQL Where Clauses using Expression Trees

C# Anonymous functions are awesome, they allow you to abstract your code in a beautiful and clean synax.
But trying to integrate this with reflection and generics might get a little bit hard, I had a problem a few days ago... Generate a SQL WHERE clause based on a function, which takes an object of ta generic ype T, and returns a boolean (f(x: T): Boolean).

The function itself will be pretty simple most of the time, the concrete example of the method I was trying to accomplish is the following: db.Update<MyType>(myObj, (o => o.Id == 1))

So basically, that will update the conveniently named table MyType, with myObj data, WHERE Id = 1

So how take an anonymouse function, parse it, and return a string? I knew it was possible, because LINQ does this for SQL, so there must be a way. The first thing I did was search on StackOverflow, which lead me to this good article on Expression Trees.

After learning a bit about that, I knew I had to use Expression Trees, but I still didn't quite understand how, I wanted to use a function on my method, not a Expression Tree, and there is no way to get an Expression Tree from a function, so I asked on StackOverflow, the answer was pretty simple, just use a function on your method call, and in your Update method use a ExpressionTree as argument type.

public bool Update<T>(T obj, Expression<Func<T, bool>> predicate)

That was I can call my function like this Update<MyType>(obj, (t => t.Id == 1))

That was a huge step, now I could work on the Expression Tree itself, I ended up with a quite nice function to get a string based on a Expression Tree, it assumes the body it's just a list of conditions, for example A && (B || C), which is what you'll most likely have to cope with.

Here is the code I used to get a string from my expression, also note the GetValue method is a helper method, used to get the value of a MemberExpression (Thanks again, StackOverflow!)


plog

Plog stands for Pragmatic Blog, it's a name derived from PPLOG, one of my old projects, it's no longer mantained, but the idea was a simplistic blog, PPLOG is just one big perl file, you drop on your webserver, and it just works.

Plog, on the other hand, is oriented at developers, and runs on the desktop, it generate a bunch of static HTML files which you can then upload to any webserver, no server configuration, no hassle, just plain HTML files.

Plog is still on development, it's on .Net, but it can also be run on Linux using Mono. The idea is to set up a configuration file, conveniently named config.json, and just run the plog command on the console to generate static HTML files based on your configuration and layout.

The workflow could be something like this

  • Create a folder named Blog
  • Create a folder named Entries inside Blog
  • Create a Markdown file in the Entries folder, with the name "yyyy-mm-dd Entry Title.md", for example "2012-10-02 My First Entry.md"
  • Open a terminal / console and navigate to the Blog folder
  • Run the plog command
That will generate HTML files in /Blog/Deploy, based on your configuration and layout. Whenever you add or modify a file, just run the plog command again to re-generate all the files.

More information about the usage will be added on future posts, but once you have set up your environment, all you'll have to do is create a  .md file, and run the plog command, if you use git you can use github to host your blog, and update your blog using your favourite editor and the terminal.

Plog will also be able to upload the generated file using FTP if specified in the configuration.

Pochitto Development

For the last few weeks I've been working on a Tiny Javascript MV* Application Framework, I had several reasons to create my own framework, improving my Javascript skills, learning more about Javascript App Frameworks, and MV* design patterns were the most relevant.

The name of the framework can still change, but for now is called Pochitto, it's dependant on jQuery, and it's < 2kb compressed and gzipped.

Pochitto is inspired mostly by YUI App Framework, it's focused on speed of development, it won't get on your way, it will just get the job done.

You can see a working example of a TODO List here, although TODO Lists might not be the best way to show a framework's potential, it's the most common.

Pochitto let's you use any template library you want, or none at all, it can also send automatic RESTful requests to an URL of your choice to sync your data in memory with your database. So all you have to worry about is adding, modifying and viewing elements in your collection, and pochitto can optionally persist those changes with ajax in the background.

All in all it's a nice little library, with all the basics an App Framework should have and some nice features, easy to pick up and easy to use.

I'll be blogging more about Pochitto soon, but for now, I'll work on the documentation.

Removing empty HTML tags from a string with Javascript

Removing empty HTML tags from a string is a rather common task when you let the user modify the HTML with a WYSIWYG editor, giving the user so much freedom is normally because he or she is the administrator, normally you don't want to just let them input HTML, as the potential for XSS and other kind of bad intentioned inputs are possible.

Nevertheless, if you just need to clean the string, this is a nice Javascript function which clean all empty HTML tags.

 String.prototype.htmlTrim = function () {  
   return this.replace(/<(\w+)>\s*\n*\t*(&nbsp;)*\s*\n*\t*<\/\1>/, '');  
 };  

The usage is just

 var cleanString = "<p></p><p>My String</p>".htmlTrim();  

That will return "<p>My String</p>". Here's an example of the Regex.