Added diagrams to dev/viewmodel

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@67427 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-12-04 03:49:40 +00:00
parent 681c05d088
commit 850989e158
3 changed files with 68 additions and 16 deletions

View File

@ -5,6 +5,16 @@
* Access at dev/viewmodel * Access at dev/viewmodel
*/ */
class ModelViewer extends Controller { class ModelViewer extends Controller {
static $url_handlers = array(
'$Module!' => 'handleModule',
);
protected $module = null;
function handleModule($request) {
return new ModelViewer_Module($request->param('Module'));
}
function init() { function init() {
parent::init(); parent::init();
if(!Permission::check("ADMIN")) Security::permissionFailure(); if(!Permission::check("ADMIN")) Security::permissionFailure();
@ -37,15 +47,16 @@ class ModelViewer extends Controller {
$modules[$model->Module]->push($model); $modules[$model->Module]->push($model);
} }
ksort($modules); ksort($modules);
unset($modules['userforms']);
if(isset($this->urlParams['Action']) && isset($modules[$this->urlParams['Action']])) { if($this->module) {
$module = $this->urlParams['Action']; $modules = array($this->module => $modules[$this->module]);
$modules = array($module => $modules[$module]);
} }
$output = new DataObjectSet(); $output = new DataObjectSet();
foreach($modules as $moduleName => $models) { foreach($modules as $moduleName => $models) {
$output->push(new ArrayData(array( $output->push(new ArrayData(array(
'Link' => 'dev/viewcode/' . $moduleName,
'Name' => $moduleName, 'Name' => $moduleName,
'Models' => $models, 'Models' => $models,
))); )));
@ -53,7 +64,28 @@ class ModelViewer extends Controller {
return $output; return $output;
} }
}
class ModelViewer_Module extends ModelViewer {
static $url_handlers = array(
'graph' => 'graph',
);
/**
* ModelViewer can be optionally constructed to restrict its output to a specific module
*/
function __construct($module = null) {
$this->module = $module;
}
function graph() {
SSViewer::set_source_file_comments(false);
$dotContent = $this->renderWith("ModelViewer_dotsrc");
$CLI_dotContent = escapeshellarg($dotContent);
header("Content-type: image/png");
echo `neato -Tpng:gd &> /dev/stdout`;
}
} }
/** /**
@ -87,13 +119,17 @@ class ModelViewer_Model extends ViewableData {
} }
function Fields() { function Fields() {
$db = singleton($this->className)->db();
$output = new DataObjectSet(); $output = new DataObjectSet();
$output->push(new ModelViewer_Field('ID', 'PrimaryKey'));
$output->push(new ModelViewer_Field('Created', 'Datetime')); $output->push(new ModelViewer_Field($this,'ID', 'PrimaryKey'));
$output->push(new ModelViewer_Field('LastEdited', 'Datetime')); if(!$this->ParentModel) {
foreach($db as $k => $v) { $output->push(new ModelViewer_Field($this,'Created', 'Datetime'));
$output->push(new ModelViewer_Field($k, $v)); $output->push(new ModelViewer_Field($this,'LastEdited', 'Datetime'));
}
$db = singleton($this->className)->uninherited('db',true);
if($db) foreach($db as $k => $v) {
$output->push(new ModelViewer_Field($this, $k, $v));
} }
return $output; return $output;
} }
@ -102,9 +138,9 @@ class ModelViewer_Model extends ViewableData {
$output = new DataObjectSet(); $output = new DataObjectSet();
foreach(array('has_one','has_many','many_many') as $relType) { foreach(array('has_one','has_many','many_many') as $relType) {
$items = singleton($this->className)->$relType(); $items = singleton($this->className)->uninherited($relType,true);
foreach($items as $k => $v) { if($items) foreach($items as $k => $v) {
$output->push(new ModelViewer_Relation($k, $v, $relType)); $output->push(new ModelViewer_Relation($this, $k, $v, $relType));
} }
} }
return $output; return $output;
@ -112,18 +148,20 @@ class ModelViewer_Model extends ViewableData {
} }
class ModelViewer_Field extends ViewableData { class ModelViewer_Field extends ViewableData {
public $Name, $Type; public $Model, $Name, $Type;
function __construct($name, $type) { function __construct($model, $name, $type) {
$this->Model = $model;
$this->Name = $name; $this->Name = $name;
$this->Type = $type; $this->Type = $type;
} }
} }
class ModelViewer_Relation extends ViewableData { class ModelViewer_Relation extends ViewableData {
public $Name, $RelationType, $RelatedClass; public $Model, $Name, $RelationType, $RelatedClass;
function __construct($name, $relatedClass, $relationType) { function __construct($model, $name, $relatedClass, $relationType) {
$this->Model = $model;
$this->Name = $name; $this->Name = $name;
$this->RelatedClass = $relatedClass; $this->RelatedClass = $relatedClass;
$this->RelationType = $relationType; $this->RelationType = $relationType;

View File

@ -9,6 +9,8 @@
<% control Modules %> <% control Modules %>
<h1>Module $Name</h1> <h1>Module $Name</h1>
<img src="$Link/graph" />
<% control Models %> <% control Models %>
<h2>$Name <% if ParentModel %> (subclass of $ParentModel)<% end_if %></h2> <h2>$Name <% if ParentModel %> (subclass of $ParentModel)<% end_if %></h2>
<h4>Fields</h4> <h4>Fields</h4>

View File

@ -0,0 +1,12 @@
digraph g {
edge[len=3];
<% control Modules %>
<% control Models %>
$Name [shape=record,label="{$Name|Field1\\nField2}"];
<% control Relations %>
$Model.Name -> $RelatedClass [label="$Name\\n$RelationType"];
<% end_control %>
<% end_control %>
<% end_control %>
}