Skip to content

Instantly share code, notes, and snippets.

@grom358
Created December 9, 2014 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grom358/96a4344f4c8a9a5738ba to your computer and use it in GitHub Desktop.
Save grom358/96a4344f4c8a9a5738ba to your computer and use it in GitHub Desktop.
<?php
class Dep {
protected $name;
protected $dependencies = [];
public function __construct($name) {
$this->name = $name;
}
public function add($dep) {
$this->dependencies[] = $dep;
return $this;
}
public function dependsOn($dep) {
return in_array($dep, $this->dependencies);
}
public function getName() {
return $this->name;
}
public static function compareTo($a, $b) {
if ($b->dependsOn($a)) {
return -1;
}
if ($a->dependsOn($b)) {
return 1;
}
return strnatcmp($a->name, $b->name);
}
}
$strongarm = new Dep('strongarm');
$ctools = new Dep('ctools');
$custom_module = new Dep('custom_module');
$strongarm->add($ctools);
$custom_module->add($strongarm);
$deps = [$custom_module, $strongarm, $ctools];
usort($deps, 'Dep::compareTo');
print_r(array_map(function(Dep $dep) { return $dep->getName(); }, $deps));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment