Permalink
Browse files

Package update for Request #12913 (return PEAR_Error or boolean)

git-svn-id: http://svn.php.net/repository/pear/packages/Image_GraphViz/trunk@252323 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 07bc088 commit 72ccbe87e50349fec21e0037294169884e261c1d Philippe Jausions committed Feb 5, 2008
Showing with 161 additions and 49 deletions.
  1. +84 −40 GraphViz.php
  2. +40 −9 package.xml
  3. +37 −0 tests/req_12913.phpt
View
@@ -1,4 +1,5 @@
<?php
+
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
@@ -120,21 +121,21 @@ class Image_GraphViz
/**
* Path to GraphViz/dot command
*
- * @var string
+ * @var string
*/
var $dotCommand = 'dot';
/**
* Path to GraphViz/neato command
*
- * @var string
+ * @var string
*/
var $neatoCommand = 'neato';
/**
* Representation of the graph
*
- * @var array
+ * @var array
*/
var $graph = array('edgesFrom' => array(),
'nodes' => array(),
@@ -146,54 +147,67 @@ class Image_GraphViz
);
/**
+ * Whether to return PEAR_Error instance on failures instead of FALSE
+ *
+ * @var boolean
+ * @access protected
+ */
+ var $_returnFalseOnError = true;
+
+ /**
* 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.
- * Note: You MUST pass a boolean, and not just
- * an expression that evaluates to TRUE or
- * FALSE (i.e. NULL, empty string, 0 will NOT
- * work)
- * @param array $attributes Attributes of the graph
- * @param string $name Name of the Graph
- * @param boolean $strict Whether to collapse multiple edges between
- * same nodes
+ * @param boolean $directed Directed (TRUE) or undirected (FALSE) graph.
+ * Note: You MUST pass a boolean, and not just
+ * an expression that evaluates to TRUE or
+ * FALSE (i.e. NULL, empty string, 0 will NOT
+ * work)
+ * @param array $attributes Attributes of the graph
+ * @param string $name Name of the Graph
+ * @param boolean $strict Whether to collapse multiple edges between
+ * same nodes
+ * @param boolean $returnError Set to TRUE to return PEAR_Error instances
+ * on failures instead of FALSE
*
* @access public
*/
function Image_GraphViz($directed = true, $attributes = array(),
- $name = 'G', $strict = true)
+ $name = 'G', $strict = true, $returnError = false)
{
$this->setDirected($directed);
$this->setAttributes($attributes);
$this->graph['name'] = $name;
$this->graph['strict'] = (boolean)$strict;
+
+ $this->_returnFalseOnError = !$returnError;
}
/**
* Outputs image of the graph in a given format
*
* This methods send HTTP headers
*
- * @param string $format Format of the output image. This may be one of
- * the formats supported by GraphViz.
+ * @param string $format Format of the output image. This may be one
+ * of the formats supported by GraphViz.
* @param string $command "dot" or "neato"
*
- * @return boolean TRUE on success, FALSE otherwise
+ * @return boolean TRUE on success, FALSE or PEAR_Error otherwise
* @access public
*/
function image($format = 'svg', $command = null)
{
$file = $this->saveParsedGraph();
- if (!$file) {
- return false;
+ if (!$file || PEAR::isError($file)) {
+ return $file;
}
$outputfile = $file . '.' . $format;
- $rendered = $this->renderDotFile($file, $outputfile, $format, $command);
+ $rendered = $this->renderDotFile($file, $outputfile, $format,
+ $command);
if ($rendered !== true) {
return $rendered;
}
@@ -267,19 +281,20 @@ function image($format = 'svg', $command = null)
/**
* Returns image (data) of the graph in a given format.
*
- * @param string $format Format of the output image. This may be one of
- * the formats supported by GraphViz.
+ * @param string $format Format of the output image. This may be one
+ * of the formats supported by GraphViz.
* @param string $command "dot" or "neato"
*
- * @return string The image (data) created by GraphViz or FALSE on error
+ * @return string The image (data) created by GraphViz, FALSE or PEAR_Error
+ * on error
* @access public
* @since Method available since Release 1.1.0
*/
function fetch($format = 'svg', $command = null)
{
$file = $this->saveParsedGraph();
- if (!$file) {
- return false;
+ if (!$file || PEAR::isError($file)) {
+ return $file;
}
$outputfile = $file . '.' . $format;
@@ -295,7 +310,11 @@ function fetch($format = 'svg', $command = null)
$fp = fopen($outputfile, 'rb');
if (!$fp) {
- return false;
+ if ($this->_returnFalseOnError) {
+ return false;
+ }
+ $error = PEAR::raiseError('Could not read rendered file');
+ return $error;
}
$data = fread($fp, filesize($outputfile));
@@ -314,14 +333,19 @@ function fetch($format = 'svg', $command = null)
* of the formats supported by GraphViz.
* @param string $command "dot" or "neato"
*
- * @return boolean TRUE if the file was saved, FALSE otherwise.
+ * @return boolean TRUE if the file was saved, FALSE or PEAR_Error
+ * otherwise.
* @access public
*/
function renderDotFile($dotfile, $outputfile, $format = 'svg',
$command = null)
{
if (!file_exists($dotfile)) {
- return false;
+ if ($this->_returnFalseOnError) {
+ return false;
+ }
+ $error = PEAR::raiseError('Could not find dot file');
+ return $error;
}
$oldmtime = file_exists($outputfile) ? filemtime($outputfile) : 0;
@@ -333,19 +357,27 @@ function renderDotFile($dotfile, $outputfile, $format = 'svg',
default:
$command = $this->graph['directed'] ? 'dot' : 'neato';
}
+ $command_orig = $command;
- $command = $this->binPath . (($command == 'dot')
- ? $this->dotCommand
- : $this->neatoCommand);
+ $command = $this->binPath.(($command == 'dot') ? $this->dotCommand
+ : $this->neatoCommand);
$command .= ' -T'.escapeshellarg($format)
.' -o'.escapeshellarg($outputfile)
- .' '.escapeshellarg($dotfile);
- @`$command`;
+ .' '.escapeshellarg($dotfile)
+ .' 2>&1';
+ exec($command, $msg, $return_val);
clearstatcache();
- return (file_exists($outputfile)
- && filemtime($outputfile) > $oldmtime);
+ if (file_exists($outputfile) && filemtime($outputfile) > $oldmtime
+ && $return_val == 0) {
+ return true;
+ } elseif ($this->_returnFalseOnError) {
+ return false;
+ }
+ $error = PEAR::raiseError($command_orig.' command failed: '
+ .implode("\n", $msg));
+ return $error;
}
/**
@@ -657,9 +689,13 @@ function load($file)
/**
* Save graph to file in Image_GraphViz format
*
+ * This saves the serialized version of the instance, not the
+ * rendered graph.
+ *
* @param string $file File to save the graph to.
*
- * @return mixed File the graph was saved to, FALSE on failure.
+ * @return string File the graph was saved to, FALSE or PEAR_Error on
+ * failure.
* @access public
*/
function save($file = '')
@@ -677,7 +713,11 @@ function save($file = '')
return $file;
}
- return false;
+ if ($this->_returnFalseOnError) {
+ return false;
+ }
+ $error = PEAR::raiseError('Could not save serialized graph instance');
+ return $error;
}
/**
@@ -798,8 +838,8 @@ function parse()
*
* @param string $file File to write the GraphViz markup to.
*
- * @return mixed File to which the GraphViz markup was written, FALSE on
- * failure.
+ * @return string File to which the GraphViz markup was written, FALSE or
+ * or PEAR_Error on failure.
* @access public
*/
function saveParsedGraph($file = '')
@@ -819,7 +859,11 @@ function saveParsedGraph($file = '')
}
}
- return false;
+ if ($this->_returnFalseOnError) {
+ return false;
+ }
+ $error = PEAR::raiseError('Could not save graph');
+ return $error;
}
}
@@ -830,4 +874,4 @@ function saveParsedGraph($file = '')
* c-hanging-comment-ender-p: nil
* End:
*/
-?>
+?>
View
@@ -26,22 +26,19 @@ http://pear.php.net/dtd/package-2.0.xsd">
<email>sb@sebastian-bergmann.de</email>
<active>no</active>
</lead>
- <date>2007-11-27</date>
- <time>16:45:30</time>
+ <date>2008-02-05</date>
+ <time>14:31:14</time>
<version>
- <release>1.3.0RC2</release>
- <api>1.3.0RC2</api>
+ <release>1.3.0RC3</release>
+ <api>1.3.0RC3</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.php.net/license">PHP License</license>
- <notes>
- - Fixes memory usage (Req #8380)
- - Added MIME types
- - Fixes a filesystem cache issue
- - Added .phpt unit tests
+ <notes>- PEAR Coding Standard fixes:
+- Support for PEAR_Error (Reques #12913)
</notes>
<contents>
<dir name="/">
@@ -62,6 +59,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file name="test17.phpt" role="test" />
<file name="test19.phpt" role="test" />
<file name="test20.phpt" role="test" />
+ <file name="req_12913.phpt" role="test" />
</dir> <!-- /tests -->
</dir> <!-- / -->
</contents>
@@ -78,6 +76,39 @@ http://pear.php.net/dtd/package-2.0.xsd">
<phprelease />
<changelog>
<release>
+ <date>2008-02-05</date>
+ <version>
+ <release>1.3.0RC3</release>
+ <api>1.3.0RC3</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>- PEAR Coding Standard fixes:
+- Support for PEAR_Error (Reques #12913)
+ </notes>
+ </release>
+ <release>
+ <date>2007-11-27</date>
+ <version>
+ <release>1.3.0RC2</release>
+ <api>1.3.0RC2</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <license uri="http://www.php.net/license">PHP License</license>
+ <notes>
+- Fixes memory usage (Req #8380)
+- Added MIME types
+- Fixes a filesystem cache issue
+- Added .phpt unit tests
+ </notes>
+ </release>
+ <release>
<date>2007-11-19</date>
<version>
<release>1.3.0RC1</release>
View
@@ -0,0 +1,37 @@
+--TEST--
+Request #12913: PEAR_Error on failure
+--FILE--
+<?php
+
+/**
+ * Request 12913: "PEAR_Error on failure"
+ *
+ * @category Image
+ * @package Image_GraphViz
+ * @author Philippe Jausions <jausions@php.net>
+ * @link http://pear.php.net/bugs/bug.php?id=12913
+ */
+require_once 'Image/GraphViz.php';
+
+$graph = new Image_GraphViz(true, array(), 'G', true, true);
+
+$graph->addNode('Node1', array('label' => 'Node1'), 'cluster_1');
+
+$result = $graph->image('unavailable_format');
+if (PEAR::isError($result)) {
+ echo "PEAR_Error\n";
+}
+
+$graph = new Image_GraphViz(true, array(), 'G', true, false);
+
+$graph->addNode('Node1', array('label' => 'Node1'), 'cluster_1');
+
+$result = $graph->image('unavailable_format');
+if ($result === false) {
+ echo "Boolean\n";
+}
+
+?>
+--EXPECT--
+PEAR_Error
+Boolean

0 comments on commit 72ccbe8

Please sign in to comment.