Skip to content

Instantly share code, notes, and snippets.

@grom358 grom358/dep.php
Created Dec 9, 2014

Embed
What would you like to do?
<?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
You can’t perform that action at this time.