New Website – Phase 1

If you’ve visited my site before you’ll see it’s gotten quote an overhaul. Though it’s still got a lot of dark elements it’s significantly brighter than before and hopefully feels a bit more approachable.

Still to come are a new starting page, better support for IE (it currently works but leaves some to be desired) and full integration with the support section.

If you experience any issues or simply want to let me know what you think of the new design I’m always happy to hear your feedback, whether positive or not.

What CoffeeScript needs to do to win me over

These are basically just notes to myself for when I inevitably give CoffeeScript another try a couple months from now, in a nutshell it’s just way too inconsistent and lacking in it’s current state.

  • Make consistent use of @ variables (issue #1207)
    • currently their very purpose differs based on the context you’re in 
  • Provide class pointers
    • If you want to access class variables or methods from inside the class there is currently no class pointer that always guarantees you access to the class instance, you can use ‘this’ but it’s not garanteed to hold the instance of the class (for example jQuery events override this in their callbacks).
  • Provide ONE consistent way of calling functions
    • CoffeeScript allows you to call functions with arguments like so: “alert ‘test 123′”, which makes for very clean code, but when you’re method chaining there is no way of doing something similar, making for inconsistent use of functions.
  • Add compiler config
    • At the moment there  is no way to define what the compiled javascript code should look like, seeing as this is what we’ll be using for debugging I’d say it’s pretty important, you can’t even have it use tabs instead of spaces for indentation. You can work around this by parsing the output of the compiled code, but this is tedious and becomes pretty complex when you’re using the –watch option.

I loved coding in CS at first, but once you really delve into it the limitations of the “language” seriously overshadow those of javascript, which may be ugly, but at least it does the job right (and consistently).

Simple Environment Config (PHP Snippet)

Never ended up using it, was intended for tiny scripts that needed some simple environmental config.

_config.php

<?php

function _config($for=null)
{

    $path    = dirname(__FILE__) . '/config.xml';
    $sig     = md5($path);

    if (defined($sig))
    {
        $config = unserialize(constant($sig));
    }
        else
    {
        $config  = (object) array();
        $xml     = simplexml_load_file(dirname(__FILE__) . '/config.xml');

        foreach ($xml->environments->children() AS $env)
        {
            $environment              = $env->attributes()->name;
            $config->{$environment}   = (object) array();

            foreach ($env->children() AS $variable)
            {
                $config->{$environment}->{$variable->attributes()->name} = (string) $variable;
            }
        }

        define($sig,serialize($config));
    }

    if ($for==null)
    {
        $for = getenv('ENVIRONMENT');

        if (empty($for))
        {
            $for = 'dev';
        }
    }

    return isset($config->{$for}) ? $config->{$for} : (object) array();

}

config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>

    <environments>
        <environment name="dev">
            <variable name="mysql_host">localhost</variable>
            <variable name="mysql_port">3306</variable>
            <variable name="mysql_user">root</variable>
            <variable name="mysql_password">root</variable>
            <variable name="mysql_database">portal</variable>
        </environment>
    </environments>

</config>

Convert multi-dimensional array to easily parsable string

Flattens array entries so that they can be parsed and/or read easily. I use this in CrossORM to make the permission definitions easy to read and write by users.

function flatten_array($array, $parents = '')
{
	if ( !is_array($array) AND !is_object($array))
	{
		return array($parents . $array);
	}

	$array  = (array) $array;
	$flat 	= array();

	foreach ($array AS $k => $v)
	{
		if (is_numeric($k))
		{
			$k = '';
			$p = $parents;
		} else
		{
			$p = $parents . $k . '.';
			$flat[] = $parents . $k;
		}

		$flat = array_merge($flat,flatten_array($v,$p));
	}

	return $flat;
}

Sample usage:

$array = flatten_array(array(
	"fruits"  	=> array(
		"a" 		=> "orange",
        "b" 		=> "banana",
        "c" 		=> "apple"
    ),
    "numbers" 	=> array (
		1, 2, 3, 4, 5, 6
	),
    "holes"   	=> array (
		"first",
        5 			=> "second",
        "third"
    )
));

echo implode('\n',$array);

Produces:

fruits
fruits.a
fruits.a.orange
fruits.b
fruits.b.banana
fruits.c
fruits.c.apple
numbers
numbers.1
numbers.2
numbers.3
numbers.4
numbers.5
numbers.6
holes
holes.first
holes.second
holes.third