From 70ca5f642d437f339a6895555b6d521334dac1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 16 Aug 2020 20:59:16 +0200 Subject: [PATCH 1/3] Drop custom exceptions, use built-in `UnexpectedValueException` instead Refs https://github.com/graphp/graph/pull/193 and https://github.com/graphp/graph/pull/194 --- composer.json | 2 +- src/GraphViz.php | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 4d8aea6..e288a3c 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ }, "require": { "php": ">=5.3.0", - "graphp/graph": "dev-master#81eef65 as 1.0.0" + "graphp/graph": "dev-master#a9d8cfc as 1.0.0" }, "require-dev": { "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35" diff --git a/src/GraphViz.php b/src/GraphViz.php index 9d6d8a0..8458089 100644 --- a/src/GraphViz.php +++ b/src/GraphViz.php @@ -5,7 +5,6 @@ use Graphp\Graph\Edge; use Graphp\Graph\EdgeDirected; use Graphp\Graph\Entity; -use Graphp\Graph\Exception\UnexpectedValueException; use Graphp\Graph\Graph; use Graphp\Graph\Vertex; @@ -187,7 +186,7 @@ public function createImageHtml(Graph $graph) * * @param Graph $graph graph to display * @return string filename - * @throws UnexpectedValueException on error + * @throws \UnexpectedValueException on error * @uses GraphViz::createScript() */ public function createImageFile(Graph $graph) @@ -197,12 +196,12 @@ public function createImageFile(Graph $graph) $tmp = tempnam(sys_get_temp_dir(), 'graphviz'); if ($tmp === false) { - throw new UnexpectedValueException('Unable to get temporary file name for graphviz script'); + throw new \UnexpectedValueException('Unable to get temporary file name for graphviz script'); } $ret = file_put_contents($tmp, $script, LOCK_EX); if ($ret === false) { - throw new UnexpectedValueException('Unable to write graphviz script to temporary file'); + throw new \UnexpectedValueException('Unable to write graphviz script to temporary file'); } $ret = 0; @@ -210,7 +209,7 @@ public function createImageFile(Graph $graph) $executable = $this->getExecutable(); system(escapeshellarg($executable) . ' -T ' . escapeshellarg($this->format) . ' ' . escapeshellarg($tmp) . ' -o ' . escapeshellarg($tmp . '.' . $this->format), $ret); if ($ret !== 0) { - throw new UnexpectedValueException('Unable to invoke "' . $executable .'" to create image file (code ' . $ret . ')'); + throw new \UnexpectedValueException('Unable to invoke "' . $executable .'" to create image file (code ' . $ret . ')'); } unlink($tmp); From cbfe6fb6315f4bd0f9d3657a810936c08f827307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 1 Jan 2022 11:20:39 +0100 Subject: [PATCH 2/3] Use plain arrays instead of `Edges` and `Vertices` containers Refs https://github.com/graphp/graph/pull/195 --- composer.json | 2 +- src/GraphViz.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index e288a3c..6eab546 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ }, "require": { "php": ">=5.3.0", - "graphp/graph": "dev-master#a9d8cfc as 1.0.0" + "graphp/graph": "dev-master#214de45 as 1.0.0" }, "require-dev": { "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35" diff --git a/src/GraphViz.php b/src/GraphViz.php index 8458089..05ad038 100644 --- a/src/GraphViz.php +++ b/src/GraphViz.php @@ -305,7 +305,7 @@ public function createScript(Graph $graph) $vid = $vids[\spl_object_hash($vertex)]; $layout = $this->getLayoutVertex($vertex, $vid); - if ($layout || $vertex->getEdges()->isEmpty()) { + if ($layout || !$vertex->getEdges()) { $script .= $this->formatIndent . $this->escape($vid); if ($layout) { $script .= ' ' . $this->escapeAttributes($layout); @@ -319,7 +319,7 @@ public function createScript(Graph $graph) // add all edges as directed edges foreach ($graph->getEdges() as $currentEdge) { - $both = $currentEdge->getVertices()->getVector(); + $both = $currentEdge->getVertices(); $currentStartVertex = $both[0]; $currentTargetVertex = $both[1]; From 6f27b25ea952f30232d74b27d888dbd850f16e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 2 Jan 2022 13:22:55 +0100 Subject: [PATCH 3/3] Clean up usage of edge API --- examples/01-simple.php | 1 - src/GraphViz.php | 33 +++++++++++++-------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/examples/01-simple.php b/examples/01-simple.php index 67d1919..3664e5e 100644 --- a/examples/01-simple.php +++ b/examples/01-simple.php @@ -13,7 +13,6 @@ $red->setAttribute('graphviz.color', 'red'); $edge = $graph->createEdgeDirected($blue, $red); -$edge = $blue->createEdgeTo($red); $edge->setAttribute('graphviz.color', 'grey'); $graphviz = new Graphp\GraphViz\GraphViz(); diff --git a/src/GraphViz.php b/src/GraphViz.php index 05ad038..fcddeb0 100644 --- a/src/GraphViz.php +++ b/src/GraphViz.php @@ -228,10 +228,10 @@ public function createImageFile(Graph $graph) */ public function createScript(Graph $graph) { - $directed = false; + $hasDirectedEdges = false; foreach ($graph->getEdges() as $edge) { if ($edge instanceof EdgeDirected) { - $directed = true; + $hasDirectedEdges = true; break; } } @@ -246,17 +246,11 @@ public function createScript(Graph $graph) $name = $this->escape($name) . ' '; } - $script = ($directed ? 'di':'') . 'graph ' . $name . '{' . self::EOL; + $script = ($hasDirectedEdges ? 'di':'') . 'graph ' . $name . '{' . self::EOL; // add global attributes - $globals = array( - 'graph' => 'graphviz.graph.', - 'node' => 'graphviz.node.', - 'edge' => 'graphviz.edge.', - ); - - foreach ($globals as $key => $prefix) { - if ($layout = $this->getAttributesPrefixed($graph, $prefix)) { + foreach (array('graph', 'node', 'edge') as $key) { + if ($layout = $this->getAttributesPrefixed($graph, 'graphviz.' . $key . '.')) { $script .= $this->formatIndent . $key . ' ' . $this->escapeAttributes($layout) . self::EOL; } } @@ -315,20 +309,19 @@ public function createScript(Graph $graph) } } - $edgeop = $directed ? ' -> ' : ' -- '; + $edgeop = $hasDirectedEdges ? ' -> ' : ' -- '; // add all edges as directed edges - foreach ($graph->getEdges() as $currentEdge) { - $both = $currentEdge->getVertices(); - $currentStartVertex = $both[0]; - $currentTargetVertex = $both[1]; + foreach ($graph->getEdges() as $edge) { + $vertices = $edge->getVertices(); + assert($vertices[0] instanceof Vertex && $vertices[1] instanceof Vertex); - $script .= $this->formatIndent . $this->escape($vids[\spl_object_hash($currentStartVertex)]) . $edgeop . $this->escape($vids[\spl_object_hash($currentTargetVertex)]); + $script .= $this->formatIndent . $this->escape($vids[\spl_object_hash($vertices[0])]) . $edgeop . $this->escape($vids[\spl_object_hash($vertices[1])]); - $layout = $this->getLayoutEdge($currentEdge); + $layout = $this->getLayoutEdge($edge); - // this edge is not a loop and also points to the opposite direction => this is actually an undirected edge - if ($directed && $currentStartVertex !== $currentTargetVertex && $currentEdge->isConnection($currentTargetVertex, $currentStartVertex)) { + // omit arrow head if this is an undirected edge in a mixed graph + if ($hasDirectedEdges && !$edge instanceof EdgeDirected) { $layout['dir'] = 'none'; } if ($layout) {