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]);
}
}
}
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:
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).