/**
  * @param DependencyContext    $dependencyContext
  * @param OutputInterface      $output
  * @param OutputFormatterInput $outputFormatterInput
  */
 public function finish(DependencyContext $dependencyContext, OutputInterface $output, OutputFormatterInput $outputFormatterInput)
 {
     $layerViolations = $this->calculateViolations($dependencyContext->getViolations());
     $layersDependOnLayers = $this->calculateLayerDependencies($dependencyContext->getAstMap(), $dependencyContext->getDependencyResult(), $dependencyContext->getClassNameLayerResolver());
     $graph = new \Fhaculty\Graph\Graph();
     /** @var $vertices Vertex[] */
     $vertices = [];
     // create a vertices
     foreach ($layersDependOnLayers as $layer => $layersDependOn) {
         if (!isset($vertices[$layer])) {
             $vertices[$layer] = $graph->createVertex($layer);
         }
         foreach ($layersDependOn as $layerDependOn => $layerDependOnCount) {
             if (!isset($vertices[$layerDependOn])) {
                 $vertices[$layerDependOn] = $graph->createVertex($layerDependOn);
             }
         }
     }
     // createEdges
     foreach ($layersDependOnLayers as $layer => $layersDependOn) {
         foreach ($layersDependOn as $layerDependOn => $layerDependOnCount) {
             $vertices[$layer]->createEdgeTo($vertices[$layerDependOn]);
             if (isset($layerViolations[$layer], $layerViolations[$layer][$layerDependOn])) {
                 $edge = $vertices[$layer]->getEdgesTo($vertices[$layerDependOn])->getEdgeFirst();
                 $edge->setAttribute('graphviz.label', $layerViolations[$layer][$layerDependOn]);
                 $edge->setAttribute('graphviz.color', 'red');
             }
         }
     }
     if ($outputFormatterInput->getOption(static::$argument_display)) {
         (new \Graphp\GraphViz\GraphViz())->display($graph);
     }
     if ($dumpImagePath = $outputFormatterInput->getOption(static::$argument_dump_image)) {
         $imagePath = (new \Graphp\GraphViz\GraphViz())->createImageFile($graph);
         rename($imagePath, $dumpImagePath);
         $output->writeln('<info>Image dumped to ' . realpath($dumpImagePath) . '</info>');
     }
     if ($dumpDotPath = $outputFormatterInput->getOption(static::$argument_dump_dot)) {
         file_put_contents($dumpDotPath, (new \Graphp\GraphViz\GraphViz())->createScript($graph));
         $output->writeln('<info>Script dumped to ' . realpath($dumpDotPath) . '</info>');
     }
     if ($dumpHtmlPath = $outputFormatterInput->getOption(static::$argument_dump_html)) {
         file_put_contents($dumpHtmlPath, (new \Graphp\GraphViz\GraphViz())->createImageHtml($graph));
         $output->writeln('<info>HTML dumped to ' . realpath($dumpHtmlPath) . '</info>');
     }
 }
Пример #2
0
<?php

use Fhaculty\Graph\Algorithm\ConnectedComponents;
require __DIR__ . '/../vendor/autoload.php';
// initialize empty graph and an UML builder
$graph = new Fhaculty\Graph\Graph();
$builder = new Fhaculty\Graph\Uml\ClassDiagramBuilder($graph);
$all = get_loaded_extensions(false);
// adding all extensions will result in a huge graph, so just pick 2 random ones
shuffle($all);
$all = array_slice($all, 0, 2);
// $all = array('PDO');
foreach ($all as $one) {
    if (!$graph->hasVertex($one)) {
        $builder->createVertexExtension($one);
    }
}
// display graph as svg image
$graphviz = new Fhaculty\Graph\GraphViz($graph);
$graphviz->setFormat('svg');
$graphviz->display();