/** * Brings the table into an order which respects dependencies between them. * * @param Table[] $tables * * @return Table[] */ public function sortTablesByDependencies(array $tables) { $nodes = array(); $dependencyGraph = new DependencyGraph(); // create nodes for each table foreach ($tables as $table) { $node = new DependencyNode($table, $table->getName()); $nodes[$table->getName()] = $node; $dependencyGraph->addNode($node); } foreach ($tables as $table) { foreach ($table->getForeignKeys() as $foreignKey) { // TODO foreign keys pointing to own table not supported yet if ($foreignKey->getForeignTableName() == $table->getName()) { continue; } $dependencyGraph->addDependency($nodes[$table->getName()], $nodes[$foreignKey->getForeignTableName()]); } } // TODO why the filter? return array_filter($dependencyGraph->resolve()); }
/** * Tests whether a circular dependency is detected for a->b b->a c->d. * * @expectedException Digilist\DependencyGraph\CircularDependencyException * @test */ public function testCircularDependencyException3() { $graph = new DependencyGraph(); $nodeA = new DependencyNode('A'); $nodeB = new DependencyNode('B'); $nodeC = new DependencyNode('C'); $nodeD = new DependencyNode('D'); $graph->addDependency($nodeA, $nodeB); $graph->addDependency($nodeB, $nodeA); $graph->addDependency($nodeC, $nodeD); $graph->resolve(); }