|
10 | 10 | use Doctrine\DBAL\Schema\Table;
|
11 | 11 | use Fhaculty\Graph\Edge\Base;
|
12 | 12 | use Fhaculty\Graph\Graph;
|
| 13 | +use Fhaculty\Graph\Vertex; |
13 | 14 | use Graphp\Algorithms\ShortestPath\Dijkstra;
|
14 | 15 |
|
15 | 16 | /**
|
@@ -172,8 +173,15 @@ private function getShortestPathWithoutCache($fromTable, $toTable)
|
172 | 173 | {
|
173 | 174 | $graph = $this->buildSchemaGraph();
|
174 | 175 |
|
175 |
| - $predecessors = MultiDijkstra::findShortestPaths($graph->getVertex($fromTable), $graph->getVertex($toTable)); |
176 |
| - $edges = MultiDijkstra::getCheapestPathFromPredecesArray($graph->getVertex($fromTable), $graph->getVertex($toTable), $predecessors); |
| 176 | + try { |
| 177 | + $predecessors = MultiDijkstra::findShortestPaths($graph->getVertex($fromTable), $graph->getVertex($toTable)); |
| 178 | + $edges = MultiDijkstra::getCheapestPathFromPredecesArray($graph->getVertex($fromTable), $graph->getVertex($toTable), $predecessors); |
| 179 | + } catch (MultiDijkstraAmbiguityException $e) { |
| 180 | + // If there is more than 1 short path, let's display this. |
| 181 | + $paths = MultiDijkstra::getAllPossiblePathsFromPredecesArray($graph->getVertex($fromTable), $graph->getVertex($toTable), $predecessors); |
| 182 | + $msg = $this->getAmbiguityExceptionMessage($paths, $graph->getVertex($fromTable), $graph->getVertex($toTable)); |
| 183 | + throw new ShortestPathAmbiguityException($msg); |
| 184 | + } |
177 | 185 |
|
178 | 186 | $foreignKeys = [];
|
179 | 187 |
|
@@ -262,4 +270,75 @@ private function getSchema() {
|
262 | 270 | return $this->schema;
|
263 | 271 | }
|
264 | 272 |
|
| 273 | + /** |
| 274 | + * Returns the full exception message when an ambiguity arises. |
| 275 | + * |
| 276 | + * @param Base[][] $paths |
| 277 | + * @param Vertex $startVertex |
| 278 | + */ |
| 279 | + private function getAmbiguityExceptionMessage(array $paths, Vertex $startVertex, Vertex $endVertex) { |
| 280 | + $textPaths = []; |
| 281 | + $i = 1; |
| 282 | + foreach ($paths as $path) { |
| 283 | + $textPaths[] = "Path ".$i.": ".$this->getTextualPath($path, $startVertex); |
| 284 | + $i++; |
| 285 | + } |
| 286 | + |
| 287 | + $msg = sprintf("There are many possible shortest paths between table '%s' and table '%s'\n\n", |
| 288 | + $startVertex->getId(), $endVertex->getId()); |
| 289 | + |
| 290 | + $msg .= implode("\n\n", $textPaths); |
| 291 | + |
| 292 | + return $msg; |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * Returns the textual representation of the path. |
| 297 | + * |
| 298 | + * @param Base[] $path |
| 299 | + * @param Vertex $startVertex |
| 300 | + */ |
| 301 | + private function getTextualPath(array $path, Vertex $startVertex) { |
| 302 | + $currentVertex = $startVertex; |
| 303 | + $currentTable = $currentVertex->getId(); |
| 304 | + |
| 305 | + $textPath = $currentTable; |
| 306 | + |
| 307 | + foreach ($path as $edge) { |
| 308 | + /* @var $fk ForeignKeyConstraint */ |
| 309 | + if ($fk = $edge->getAttribute('fk')) { |
| 310 | + if ($fk->getForeignTableName() == $currentTable) { |
| 311 | + $currentTable = $fk->getLocalTable()->getName(); |
| 312 | + $isForward = false; |
| 313 | + } else { |
| 314 | + $currentTable = $fk->getForeignTableName(); |
| 315 | + $isForward = true; |
| 316 | + } |
| 317 | + |
| 318 | + $columns = implode(',', $fk->getLocalColumns()); |
| 319 | + |
| 320 | + $textPath .= " ".(!$isForward?"<":""); |
| 321 | + $textPath .= "--(".$columns.")--"; |
| 322 | + $textPath .= ($isForward?">":"")." "; |
| 323 | + $textPath .= $currentTable; |
| 324 | + } elseif ($junctionTable = $edge->getAttribute('junction')) { |
| 325 | + /* @var $junctionTable Table */ |
| 326 | + $junctionFks = array_values($junctionTable->getForeignKeys()); |
| 327 | + // We need to order the 2 FKs. The first one is the one that has a common point with the current table. |
| 328 | + $fk = $junctionFks[0]; |
| 329 | + if ($fk->getForeignTableName() == $currentTable) { |
| 330 | + $currentTable = $junctionFks[1]->getForeignTableName(); |
| 331 | + } else { |
| 332 | + $currentTable = $fk->getForeignTableName(); |
| 333 | + } |
| 334 | + $textPath .= " <=(".$junctionTable->getName().")=> ".$currentTable; |
| 335 | + } else { |
| 336 | + // @codeCoverageIgnoreStart |
| 337 | + throw new SchemaAnalyzerException('Unexpected edge. We should have a fk or a junction attribute.'); |
| 338 | + // @codeCoverageIgnoreEnd |
| 339 | + } |
| 340 | + } |
| 341 | + |
| 342 | + return $textPath; |
| 343 | + } |
265 | 344 | }
|
0 commit comments