xenogenesi::blog
memento
3d adt android apache2 app apt aria2 build bullet cflags chromium codeigniter debian demoscene dependencies dpkg driver emulator freeglut gcc gfx git glut htaccess javascript json kernel linux make metalink minimal mysql opengl php python raspbian realtime rpi specs template toolchain update-alternatives video wifi wordpress

Php dependecies resolver

based on this article: Dependency resolving algorithm and the python snippet below:

def dep_resolve(node, resolved, unresolved):
   unresolved.append(node)
   for edge in node.edges:
      if edge not in resolved:
         if edge in unresolved:
            raise Exception('Circular reference detected: %s -> %s' % (node.name, edge.name))
         dep_resolve(edge, resolved, unresolved)
   resolved.append(node)
   unresolved.remove(node)

a Php implementation to use with CodeIgniter to enqueue scripts like WordPress:

class DepsNode {
    public $name;
    public $edges = array();

    function __construct($name) {
        $this->name = $name;
    }

    function addEdge($edge) {
        array_push($this->edges, $edge);
    }
}

class JsDepNode extends DepsNode {
    public $src;
    public $deps;
    public $version;
    function __construct($name, $src, $deps = array(), $version = '') {
        parent::__construct($name);
        $this->src = $src;
        $this->deps = $deps;
        $this->version = $version;
    }

}

class Jsdeps {
    public $scripts = array();
    public $resolved = array();

    public function enqueue($name, $src, $deps = array(), $version = '') {
        $script = new JsDepNode($name, $src, $deps, $version);
        foreach($deps as $dep) {
            $script->addEdge($dep);
        }
        $this->scripts[$name] = $script;
    }

    public function render() {
        $unresolved = array();
        foreach($this->scripts as $script) {
            $this->dep_resolve($script, $this->resolved, $unresolved);
        }
        foreach ($this->resolved as $script_name) {
            $script = $this->scripts[$script_name];
            $ver = $script->version !== '' ? '?'.$script->version : '';
            echo "<script src=\"{$script->src}{$ver}\"></script>\n";
        }
    }

    /*
    public function test() {
            $a = new JsDepNodex("a");
            $this->scripts["a"] = $a;
            $b = new JsDepNodex("b");
            $this->scripts["b"] = $b;
            $c = new JsDepNodex("c");
            $this->scripts["c"] = $c;
            $d = new JsDepNodex("d");
            $this->scripts["d"] = $d;
            $e = new JsDepNodex("e");
            $this->scripts["e"] = $e;


            $a->addEdge($b->name);    # a depends on b
            $a->addEdge($d->name);    # a depends on d
            $b->addEdge($c->name);    # b depends on c
            $b->addEdge($e->name);    # b depends on e
            $c->addEdge($d->name);    # c depends on d
            $c->addEdge($e->name);

            //$d->addEdge($b->name);

            $unresolved = array();
            $this->dep_resolve($a, $this->resolved, $unresolved);
            foreach($this->resolved as $node) {
                echo "- $node\n";
            }
    }*/


    public function dep_resolve($node, &$resolved, &$unresolved) {
        if(in_array($node->name, $resolved))
            return;
        array_push($unresolved, $node->name);
        foreach($node->edges as $edge) {
            if(!in_array($edge, $resolved)) {
                if(in_array($edge, $unresolved)) {
                    log_message('error', "Jsdeps: circular reference: {$node->name} -> {$edge}");
                    return;
                }
                $this->dep_resolve($this->scripts[$edge], $resolved, $unresolved);
            }
        }
        array_push($resolved, $node->name);
        $k = array_search($node->name,$unresolved);
        if($k!==false) {
            unset($unresolved[$k]);
        }
    }
}

Php and Javascript template engines

I thought would be nice to have a template engine with the same syntax for both Php and Javascript, I’m thinking about a website which could work on Php only then scale with Javascript, below a list of something that already exist:

  1. jquery-tmpl: jquery-tmp is deprecated from jquery and abandoned.
  2. Smarty: smarty have a nice syntax and is powerful but too much for my needs, I didn’t checked the status and compatibility of jsmart.
  3. mustache: so far the most interesting, pre-compiled templates in Javascript, cached on Php, same syntax for both languages, there’s also handlebars.js which is compatible to some degree.
  4. twig: interesting but the compatibility of twig.js is still partial, a subset.
  5. underscore: would have been interesting because is a dependency of Backbone.js, sadly the syntax is different, would require a different template for each language.

So, mustache seems to be most interesting, I’ll give a try sooner or later, in the while I been tempted (still) to write my own, inspired by something simple like JavaScript template engine in just 20 lines and JavaScript Micro-Templating. The target would be a template engine with the same exact syntax for both Php and Javascript, a generator, script or command, which given a template would create a function for both the languages (eventually any language introducing a output backend concept).

Doing some fast test immediately shown an horrific implementation design, Javascript’s (json) objects are unsorted (arrays are sorted).