graphviz.php.tmp

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· TMP 代码 · 共 573 行 · 第 1/2 页

TMP
573
字号
            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())    {        if (is_array($edge)) {            $from = key($edge);            $to   = $edge[$from];            $id   = $from . '_' . $to;            if (!isset($this->graph['edges'][$id])) {                $this->graph['edges'][$id] = $edge;            } else {                $this->graph['edges'][$id] = array_merge(                  $this->graph['edges'][$id],                  $edge                );            }            if (is_array($attributes)) {                if (!isset($this->graph['edgeAttributes'][$id])) {                    $this->graph['edgeAttributes'][$id] = $attributes;                } else {                    $this->graph['edgeAttributes'][$id] = array_merge(                      $this->graph['edgeAttributes'][$id],                      $attributes                    );                }            }        }    }    /**     * 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];              $id   = $from . '_' . $to;            if (isset($this->graph['edges'][$id])) {                unset($this->graph['edges'][$id]);            }            if (isset($this->graph['edgeAttributes'][$id])) {                unset($this->graph['edgeAttributes'][$id]);            }        }    }    /**     * 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'],              $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)    {        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 ($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 = '')    {        $serializedGraph = serialize($this->graph);        if (empty($file)) {            $file = System::mktemp('graph_');        }        if ($fp = @fopen($file, 'w')) {            @fputs($fp, $serializedGraph);            @fclose($fp);            return $file;        }        return FALSE;    }    /**     * 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 {            $parsedGraph = "digraph G {\n";        }        if (isset($this->graph['attributes'])) {            foreach ($this->graph['attributes'] as $key => $value) {                $attributeList[] = $key . '="' . $value . '"';            }            if (!empty($attributeList)) {                $parsedGraph .= 'graph [ '.implode(',', $attributeList) . " ];\n";            }        }        if (isset($this->graph['nodes'])) {            foreach($this->graph['nodes'] as $group => $nodes) {                if ($group != 'default') {                    $parsedGraph .= sprintf(                      "subgraph \"cluster_%s\" {\nlabel=\"%s\";\n",                      $group,                      isset($this->graph['clusters'][$group]) ? $this->graph['clusters'][$group]['title'] : ''                    );                    if (isset($this->graph['clusters'][$group]['attributes'])) {                        unset($attributeList);                        foreach ($this->graph['clusters'][$group]['attributes'] as $key => $value) {                            $attributeList[] = $key . '="' . $value . '"';                        }                        if (!empty($attributeList)) {                            $parsedGraph .= implode(',', $attributeList) . ";\n";                        }                    }                }                foreach($nodes as $node => $attributes) {                    unset($attributeList);                    foreach($attributes as $key => $value) {                        $attributeList[] = $key . '="' . $value . '"';                    }                    if (!empty($attributeList)) {                        $parsedGraph .= sprintf(                          "\"%s\" [ %s ];\n",                          addslashes(stripslashes($node)),                          implode(',', $attributeList)                        );                    }                }                if ($group != 'default') {                  $parsedGraph .= "}\n";                }            }        }        if (isset($this->graph['edges'])) {            foreach($this->graph['edges'] as $label => $node) {                unset($attributeList);                $from = key($node);                $to   = $node[$from];                foreach($this->graph['edgeAttributes'][$label] as $key => $value) {                    $attributeList[] = $key . '="' . $value . '"';                }                $parsedGraph .= sprintf(                  '"%s" -> "%s"',                  addslashes(stripslashes($from)),                  addslashes(stripslashes($to))                );                                if (!empty($attributeList)) {                    $parsedGraph .= sprintf(                      ' [ %s ]',                      implode(',', $attributeList)                    );                }                $parsedGraph .= ";\n";            }        }        return $parsedGraph . "}\n";    }    /**     * 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)) {            if (empty($file)) {                $file = System::mktemp('graph_');            }            if ($fp = @fopen($file, 'w')) {                @fputs($fp, $parsedGraph);                @fclose($fp);                return $file;            }        }        return FALSE;    }}/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?