Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add renderDotFile() method. Use escapeshellarg(). Use new header and …

…bump copyright year. Add myself back as a maintainer. Fix CS and WS issues. Prepare release.

git-svn-id: http://svn.php.net/repository/pear/packages/Image_GraphViz/trunk@212826 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit 70385713393d33eb3eb0aee3a03dcfff2a8d5478 1 parent 03694d5
Sebastian Bergmann authored
Showing with 243 additions and 159 deletions.
  1. +216 −149 GraphViz.php
  2. +27 −10 package.xml
View
365 GraphViz.php
@@ -1,20 +1,30 @@
<?php
-//
-// +---------------------------------------------------------------------------+
-// | PEAR :: Image :: GraphViz |
-// +---------------------------------------------------------------------------+
-// | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> and |
-// | Dr. Volker Göbbels <vmg@arachnion.de>. |
-// +---------------------------------------------------------------------------+
-// | This source file is subject to version 3.00 of the PHP License, |
-// | that is available at http://www.php.net/license/3_0.txt. |
-// | If you did not receive a copy of the PHP license and are unable to |
-// | obtain it through the world-wide-web, please send a note to |
-// | license@php.net so we can mail you a copy immediately. |
-// +---------------------------------------------------------------------------+
-//
-// $Id$
-//
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * Image_GraphViz
+ *
+ * Copyright (c) 2001-2006, Dr. Volker Göbbels <vmg@arachnion.de> and
+ * Sebastian Bergmann <sb@sebastian-bergmann.de>. All rights reserved.
+ *
+ * LICENSE: This source file is subject to version 3.0 of the PHP license
+ * that is available through the world-wide-web at the following URI:
+ * http://www.php.net/license/3_0.txt. If you did not receive a copy of
+ * the PHP License and are unable to obtain it through the web, please
+ * send a note to license@php.net so we can mail you a copy immediately.
+ *
+ * @category Image
+ * @package GraphViz
+ * @author Dr. Volker Göbbels <vmg@arachnion.de>
+ * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @author Karsten Dambekalns <k.dambekalns@fishfarm.de>
+ * @author Michael Lively Jr. <mlively@ft11.net>
+ * @copyright 2001-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
+ * @license http://www.php.net/license/3_0.txt PHP License 3.0
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/Image_GraphViz
+ * @since File available since Release 0.1
+ */
require_once 'System.php';
@@ -77,61 +87,68 @@
* ?>
* </code>
*
+ * @category Image
+ * @package GraphViz
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @author Dr. Volker Göbbels <vmg@arachnion.de>
* @author Karsten Dambekalns <k.dambekalns@fishfarm.de>
- * @copyright Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> and Dr. Volker Göbbels <vmg@arachnion.de>
+ * @author Michael Lively Jr. <mlively@ft11.net>
+ * @copyright Copyright &copy; 2001-2006 Dr. Volker Göbbels <vmg@arachnion.de> and Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
- * @category Image
- * @package Image_GraphViz
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Image_GraphViz
+ * @since Class available since Release 0.1
*/
-class Image_GraphViz {
+class Image_GraphViz
+{
/**
- * Path to GraphViz/dot command
- *
- * @var string
- */
+ * Path to GraphViz/dot command
+ *
+ * @var string
+ */
var $dotCommand = 'dot';
/**
- * Path to GraphViz/neato command
- *
- * @var string
- */
+ * Path to GraphViz/neato command
+ *
+ * @var string
+ */
var $neatoCommand = 'neato';
/**
- * Representation of the graph
- *
- * @var array
- */
+ * Representation of the graph
+ *
+ * @var array
+ */
var $graph;
/**
- * Constructor.
- *
- * Setting the name of the Graph is useful for including multiple image maps on
- * one page. If not set, the graph will be named 'G'.
- *
- * @param boolean $directed Directed (TRUE) or undirected (FALSE) graph.
- * @param array $attributes Attributes of the graph
- * @param string $name Name of the Graph
- * @access public
- */
- function Image_GraphViz($directed = TRUE, $attributes = array(), $name = NULL) {
+ * Constructor.
+ *
+ * Setting the name of the Graph is useful for including multiple image maps on
+ * one page. If not set, the graph will be named 'G'.
+ *
+ * @param boolean $directed Directed (TRUE) or undirected (FALSE) graph.
+ * @param array $attributes Attributes of the graph
+ * @param string $name Name of the Graph
+ * @access public
+ */
+ function Image_GraphViz($directed = TRUE, $attributes = array(), $name = NULL)
+ {
$this->setDirected($directed);
$this->setAttributes($attributes);
$this->graph['name'] = $name;
}
/**
- * Output image of the graph in a given format.
- *
- * @param string Format of the output image.
- * This may be one of the formats supported by GraphViz.
- * @access public
- */
- function image($format = 'svg') {
+ * Output image of the graph in a given format.
+ *
+ * @param string Format of the output image.
+ * This may be one of the formats supported by GraphViz.
+ * @access public
+ */
+ function image($format = 'svg')
+ {
if ($data = $this->fetch($format)) {
$sendContentLengthHeader = TRUE;
@@ -172,19 +189,20 @@ function image($format = 'svg') {
}
/**
- * Return image (data) of the graph in a given format.
- *
- * @param string Format of the output image.
- * This may be one of the formats supported by GraphViz.
- * @return string The image (data) created by GraphViz.
- * @access public
- * @since 1.1.0
- */
- function fetch($format = 'svg') {
+ * Return image (data) of the graph in a given format.
+ *
+ * @param string Format of the output image.
+ * This may be one of the formats supported by GraphViz.
+ * @return string The image (data) created by GraphViz.
+ * @access public
+ * @since Method available since Release 1.1.0
+ */
+ function fetch($format = 'svg')
+ {
if ($file = $this->saveParsedGraph()) {
$outputfile = $file . '.' . $format;
$command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand;
- $command .= " -T$format -o$outputfile $file";
+ $command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($dotfile);
@`$command`;
@unlink($file);
@@ -204,55 +222,88 @@ function fetch($format = 'svg') {
}
/**
- * Add a cluster to the graph.
- *
- * @param string ID.
- * @param array Title.
- * @param array Attributes of the cluster.
- * @access public
- */
- function addCluster($id, $title, $attributes = array()) {
+ * Render a given dot file into another format.
+ *
+ * @param string The absolute path of the dot file to use.
+ * @param string The absolute path of the file to save to.
+ * @param string Format of the output image.
+ * This may be one of the formats supported by GraphViz.
+ * @return bool True if the file was saved, false otherwise.
+ * @access public
+ */
+ function renderDotFile($dotfile, $outputfile, $format = 'svg')
+ {
+ if (file_exists($dotfile)) {
+ $oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
+
+ $command = $this->graph['directed'] ? $this->dotCommand : $this->neatoCommand;
+ $command .= ' -T' . escapeshellarg($format) . ' -o' . escapeshellarg($outputfile) . ' ' . escapeshellarg($dotfile);
+
+ @`$command`;
+ @unlink($file);
+
+ if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime) {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+ }
+
+ /**
+ * Add a cluster to the graph.
+ *
+ * @param string ID.
+ * @param array Title.
+ * @param array Attributes of the cluster.
+ * @access public
+ */
+ function addCluster($id, $title, $attributes = array())
+ {
$this->graph['clusters'][$id]['title'] = $title;
$this->graph['clusters'][$id]['attributes'] = $attributes;
}
/**
- * Add a note to the graph.
- *
- * @param string Name of the node.
- * @param array Attributes of the node.
- * @param string Group of the node.
- * @access public
- */
- function addNode($name, $attributes = array(), $group = 'default') {
+ * Add a note to the graph.
+ *
+ * @param string Name of the node.
+ * @param array Attributes of the node.
+ * @param string Group of the node.
+ * @access public
+ */
+ function addNode($name, $attributes = array(), $group = 'default')
+ {
$this->graph['nodes'][$group][$name] = $attributes;
}
/**
- * Remove a node from the graph.
- *
- * @param Name of the node to be removed.
- * @access public
- */
- function removeNode($name, $group = 'default') {
+ * Remove a node from the graph.
+ *
+ * @param Name of the node to be removed.
+ * @access public
+ */
+ function removeNode($name, $group = 'default')
+ {
if (isset($this->graph['nodes'][$group][$name])) {
unset($this->graph['nodes'][$group][$name]);
}
}
/**
- * Add an edge to the graph.
- *
- * Caveat! This cannot handle multiple identical edges. If you use non-numeric
- * IDs for the nodes, this will not do (too much) harm. For numeric IDs the
- * array_merge() that is used will change the keys when merging arrays, leading
- * to new nodes appearing in the graph.
- *
- * @param array Start and End node of the edge.
- * @param array Attributes of the edge.
- * @access public
- */
- function addEdge($edge, $attributes = array()) {
+ * Add an edge to the graph.
+ *
+ * Caveat! This cannot handle multiple identical edges. If you use non-numeric
+ * IDs for the nodes, this will not do (too much) harm. For numeric IDs the
+ * array_merge() that is used will change the keys when merging arrays, leading
+ * to new nodes appearing in the graph.
+ *
+ * @param array Start and End node of the edge.
+ * @param array Attributes of the edge.
+ * @access public
+ */
+ function addEdge($edge, $attributes = array())
+ {
if (is_array($edge)) {
$from = key($edge);
$to = $edge[$from];
@@ -281,12 +332,13 @@ function addEdge($edge, $attributes = array()) {
}
/**
- * Remove an edge from the graph.
- *
- * @param array Start and End node of the edge to be removed.
- * @access public
- */
- function removeEdge($edge) {
+ * Remove an edge from the graph.
+ *
+ * @param array Start and End node of the edge to be removed.
+ * @access public
+ */
+ function removeEdge($edge)
+ {
if (is_array($edge)) {
$from = key($edge);
$to = $edge[$from];
@@ -303,12 +355,13 @@ function removeEdge($edge) {
}
/**
- * Add attributes to the graph.
- *
- * @param array Attributes to be added to the graph.
- * @access public
- */
- function addAttributes($attributes) {
+ * Add attributes to the graph.
+ *
+ * @param array Attributes to be added to the graph.
+ * @access public
+ */
+ function addAttributes($attributes)
+ {
if (is_array($attributes)) {
$this->graph['attributes'] = array_merge(
$this->graph['attributes'],
@@ -318,57 +371,61 @@ function addAttributes($attributes) {
}
/**
- * Set attributes of the graph.
- *
- * @param array Attributes to be set for the graph.
- * @access public
- */
- function setAttributes($attributes) {
+ * Set attributes of the graph.
+ *
+ * @param array Attributes to be set for the graph.
+ * @access public
+ */
+ function setAttributes($attributes)
+ {
if (is_array($attributes)) {
$this->graph['attributes'] = $attributes;
}
}
/**
- * Set directed/undirected flag for the graph.
- *
- * @param boolean Directed (TRUE) or undirected (FALSE) graph.
- * @access public
- */
- function setDirected($directed) {
+ * Set directed/undirected flag for the graph.
+ *
+ * @param boolean Directed (TRUE) or undirected (FALSE) graph.
+ * @access public
+ */
+ function setDirected($directed)
+ {
if (is_bool($directed)) {
$this->graph['directed'] = $directed;
}
}
/**
- * Load graph from file.
- *
- * @param string File to load graph from.
- * @access public
- */
- function load($file) {
- if ($serialized_graph = implode('', @file($file))) {
- $this->graph = unserialize($serialized_graph);
+ * Load graph from file.
+ *
+ * @param string File to load graph from.
+ * @access public
+ */
+ function load($file)
+ {
+ if ($serializedGraph = implode('', @file($file))) {
+ $this->graph = unserialize($serializedGraph);
}
}
/**
- * Save graph to file.
- *
- * @param string File to save the graph to.
- * @return mixed File the graph was saved to, FALSE on failure.
- * @access public
- */
- function save($file = '') {
- $serialized_graph = serialize($this->graph);
+ * Save graph to file.
+ *
+ * @param string File to save the graph to.
+ * @return mixed File the graph was saved to, FALSE on failure.
+ * @access public
+ */
+ function save($file = '')
+ {
+ $serializedGraph = serialize($this->graph);
if (empty($file)) {
$file = System::mktemp('graph_');
}
if ($fp = @fopen($file, 'w')) {
- @fputs($fp, $serialized_graph);
+ @fputs($fp, $serializedGraph);
@fclose($fp);
return $file;
@@ -378,12 +435,13 @@ function save($file = '') {
}
/**
- * Parse the graph into GraphViz markup.
- *
- * @return string GraphViz markup
- * @access public
- */
- function parse() {
+ * Parse the graph into GraphViz markup.
+ *
+ * @return string GraphViz markup
+ * @access public
+ */
+ function parse()
+ {
if (isset($this->graph['name']) && is_string($this->graph['name'])) {
$parsedGraph = "digraph " . $this->graph['name'] . " {\n";
} else {
@@ -477,14 +535,15 @@ function parse() {
}
/**
- * Save GraphViz markup to file.
- *
- * @param string File to write the GraphViz markup to.
- * @return mixed File to which the GraphViz markup was
- * written, FALSE on failure.
- * @access public
- */
- function saveParsedGraph($file = '') {
+ * Save GraphViz markup to file.
+ *
+ * @param string File to write the GraphViz markup to.
+ * @return mixed File to which the GraphViz markup was
+ * written, FALSE on failure.
+ * @access public
+ */
+ function saveParsedGraph($file = '')
+ {
$parsedGraph = $this->parse();
if (!empty($parsedGraph)) {
@@ -503,4 +562,12 @@ function saveParsedGraph($file = '') {
return FALSE;
}
}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * c-hanging-comment-ender-p: nil
+ * End:
+ */
?>
View
37 package.xml
@@ -10,31 +10,48 @@
<name>Dr. Volker G&ouml;bbels</name>
<email>vmg@arachnion.de</email>
</maintainer>
+ <maintainer>
+ <user>sebastian</user>
+ <name>Sebastian Bergmann</name>
+ <email>sb@sebastian-bergmann.de</email>
+ </maintainer>
</maintainers>
<license>PHP License</license>
<release>
- <version>1.1.0</version>
- <date>2005-01-19</date>
+ <version>1.2.0</version>
+ <date>2006-05-11</date>
<state>stable</state>
<notes>
<![CDATA[
-+ Added fetch() method that returns the generated image.
-
-+ Added support for named graphs.
-
-+ Added support for cluster attributes.
-
-* Fixed a bug with graph attributes.
++ Added renderDotFile() method that renders an existing DOT file.
]]>
</notes>
<filelist>
<dir name="/" baseinstalldir="Image">
- <file role="php" name="GraphViz.php"/>
+ <file name="GraphViz.php">
+ <replace type="package-info" from="@package_version@" to="version"/>
+ </file>
</dir>
</filelist>
</release>
<changelog>
<release>
+ <version>1.1.0</version>
+ <date>2005-01-19</date>
+ <state>stable</state>
+ <notes>
+<![CDATA[
++ Added fetch() method that returns the generated image.
+
++ Added support for named graphs.
+
++ Added support for cluster attributes.
+
+* Fixed a bug with graph attributes.
+]]>
+ </notes>
+ </release>
+ <release>
<version>1.0.3</version>
<date>2004-02-25</date>
<state>stable</state>
Please sign in to comment.
Something went wrong with that request. Please try again.