|
#!/usr/bin/env php |
|
<?php |
|
/** |
|
* Generate a format from the XML file. |
|
* |
|
* <code> |
|
* ./parse.php ERD.xml dot |
|
* ./parse.php ERD.xml dump |
|
* </code> |
|
* |
|
* @author Jachim Coudenys <[email protected]> |
|
* |
|
* @todo Only detect links on the owning side |
|
* @todo Use the tags to create sub diagrams so they are grouped together |
|
*/ |
|
$file = isset($argv[1]) ? $argv[1] : false; |
|
if (!$file || !is_file($file)) { |
|
echo 'Supply a valid file.' . PHP_EOL; |
|
exit(1); |
|
} |
|
$format = isset($argv[2]) ? $argv[2] : 'dump'; |
|
|
|
$nodes = array(); |
|
|
|
$xml = simplexml_load_file($file); |
|
foreach ($xml as $node) { |
|
if (isset($node->section)) { |
|
$title = (string) ($node->title); |
|
$nodes[$title] = array( |
|
'fields' => array(), |
|
'relations' => array(), |
|
); |
|
foreach ($node as $subNode) { |
|
if (isset($subNode->title) |
|
&& $subNode->title == 'Fields' |
|
&& isset($subNode->field_list->field) |
|
) { |
|
foreach ($subNode->field_list->field as $f) { |
|
$description = (string) $f->field_body->paragraph; |
|
$dataType = ''; |
|
$matches = array(); |
|
if (preg_match('#\{([a-zA-Z]+)\}#', $description, $matches)) { |
|
$dataType = $matches[1]; |
|
} |
|
$nodes[$title]['fields'][(string) $f->field_name] = array( |
|
'description' => $description, |
|
'dataType' => $dataType, |
|
); |
|
} |
|
} |
|
if (isset($subNode->title) |
|
&& $subNode->title == 'Relations' |
|
&& isset($subNode->field_list->field) |
|
) { |
|
foreach ($subNode->field_list->field as $f) { |
|
$description = (string) $f->field_body->paragraph; |
|
$type = 'normal'; |
|
if (stripos($description, 'optional') !== false) { |
|
$type = 'optional'; |
|
} |
|
$nodes[$title]['relations'][(string) $f->field_name->reference] = array( |
|
'description' => $description, |
|
'type' => $type, |
|
); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
if ($format == 'dot') { |
|
echo 'digraph G {' . PHP_EOL; |
|
echo ' node [shape=record, fontname=Calibri, fontsize=11]' . PHP_EOL; |
|
//echo ' edge [arrowhead=crow,arrowtail=dot]' . PHP_EOL; |
|
|
|
foreach ($nodes as $node => $attributes) { |
|
//echo ' ' . $node . ' [label="{{' . $node . '}|' . implode('|', array_keys($attributes['fields'])) . '}"]' . PHP_EOL; |
|
$table = '<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">'; |
|
$table .= '<TR><TD BGCOLOR="lightgrey" COLSPAN="2">' . $node . '</TD></TR>'; |
|
foreach ($attributes['fields'] as $field => $fieldAttributes) { |
|
$table .= '<TR>'; |
|
$table .= '<TD ALIGN="left">' . $fieldAttributes['dataType'] . '</TD>'; |
|
$table .= '<TD ALIGN="left">' . $field . '</TD>'; |
|
$table .= '</TR>'; |
|
} |
|
$table .= '</TABLE>'; |
|
echo ' ' . $node . ' [shape=none, margin=0, label=<' . $table . '>]'; |
|
foreach ($attributes['relations'] as $relation => $relationAttributes) { |
|
echo ' ' . $node . ' -> ' . $relation . ' [label=""'; |
|
if ($relationAttributes['type'] == 'optional') { |
|
echo ',style=dashed'; |
|
} |
|
echo ']' . PHP_EOL; |
|
} |
|
} |
|
|
|
echo '}' . PHP_EOL; |
|
|
|
} else { // dump is default |
|
print_r($nodes); |
|
|
|
} |
|
|