2008-12-04 23:38:32 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Gives you a nice way of viewing your data model.
|
2009-07-10 02:37:04 +02:00
|
|
|
* Access at dev/viewmodel.
|
|
|
|
*
|
|
|
|
* Requirements: http://graphviz.org/
|
2009-03-22 23:59:14 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tools
|
2008-12-04 23:38:32 +01:00
|
|
|
*/
|
|
|
|
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() {
|
|
|
|
parent::init();
|
|
|
|
if(!Permission::check("ADMIN")) Security::permissionFailure();
|
2009-07-10 02:37:04 +02:00
|
|
|
|
|
|
|
// check for graphviz dependencies
|
|
|
|
$returnCode = 0;
|
|
|
|
$output = array();
|
2009-07-10 05:21:48 +02:00
|
|
|
exec("which neato", $output, $returnCode);
|
2009-07-10 02:37:04 +02:00
|
|
|
if($returnCode != 0) {
|
|
|
|
user_error(
|
2009-07-10 05:21:48 +02:00
|
|
|
'You don\'t seem to have the GraphViz library (http://graphviz.org/) and the "neato" command-line utility available',
|
2009-07-10 02:37:04 +02:00
|
|
|
E_USER_ERROR
|
|
|
|
);
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model classes
|
|
|
|
*/
|
|
|
|
function Models() {
|
|
|
|
$classes = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($classes);
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
foreach($classes as $class) {
|
|
|
|
$output->push(new ModelViewer_Model($class));
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model classes, grouped by Module
|
|
|
|
*/
|
|
|
|
function Modules() {
|
|
|
|
$classes = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($classes);
|
|
|
|
|
|
|
|
$modules = array();
|
|
|
|
foreach($classes as $class) {
|
|
|
|
$model = new ModelViewer_Model($class);
|
|
|
|
if(!isset($modules[$model->Module])) $modules[$model->Module] = new DataObjectSet();
|
|
|
|
$modules[$model->Module]->push($model);
|
|
|
|
}
|
|
|
|
ksort($modules);
|
|
|
|
unset($modules['userforms']);
|
|
|
|
|
|
|
|
if($this->module) {
|
|
|
|
$modules = array($this->module => $modules[$this->module]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
foreach($modules as $moduleName => $models) {
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'Link' => 'dev/viewmodel/' . $moduleName,
|
|
|
|
'Name' => $moduleName,
|
|
|
|
'Models' => $models,
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2009-07-10 02:37:04 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function graph() {
|
|
|
|
SSViewer::set_source_file_comments(false);
|
|
|
|
$dotContent = $this->renderWith("ModelViewer_dotsrc");
|
|
|
|
$CLI_dotContent = escapeshellarg($dotContent);
|
2009-07-10 05:21:48 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$output= `echo $CLI_dotContent | neato -Tpng:gd &> /dev/stdout`;
|
|
|
|
if(substr($output,1,3) == 'PNG') header("Content-type: image/png");
|
|
|
|
else header("Content-type: text/plain");
|
|
|
|
echo $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a single model in the model viewer
|
|
|
|
*/
|
|
|
|
class ModelViewer_Model extends ViewableData {
|
|
|
|
protected $className;
|
|
|
|
|
|
|
|
function __construct($className) {
|
|
|
|
$this->className = $className;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getModule() {
|
|
|
|
global $_CLASS_MANIFEST;
|
|
|
|
$className = $this->className;
|
|
|
|
if(($pos = strpos($className,'_')) !== false) $className = substr($className,0,$pos);
|
|
|
|
if(isset($_CLASS_MANIFEST[$className])) {
|
|
|
|
if(preg_match('/^'.str_replace('/','\/',preg_quote(BASE_PATH)).'\/([^\/]+)\//', $_CLASS_MANIFEST[$className], $matches)) {
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getName() {
|
|
|
|
return $this->className;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParentModel() {
|
|
|
|
$parentClass = get_parent_class($this->className);
|
|
|
|
if($parentClass != "DataObject") return $parentClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Fields() {
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
$output->push(new ModelViewer_Field($this,'ID', 'PrimaryKey'));
|
|
|
|
if(!$this->ParentModel) {
|
|
|
|
$output->push(new ModelViewer_Field($this,'Created', 'Datetime'));
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Relations() {
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
|
|
|
|
foreach(array('has_one','has_many','many_many') as $relType) {
|
|
|
|
$items = singleton($this->className)->uninherited($relType,true);
|
|
|
|
if($items) foreach($items as $k => $v) {
|
|
|
|
$output->push(new ModelViewer_Relation($this, $k, $v, $relType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ModelViewer_Field extends ViewableData {
|
|
|
|
public $Model, $Name, $Type;
|
|
|
|
|
|
|
|
function __construct($model, $name, $type) {
|
|
|
|
$this->Model = $model;
|
|
|
|
$this->Name = $name;
|
|
|
|
$this->Type = $type;
|
2009-07-10 02:37:04 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ModelViewer_Relation extends ViewableData {
|
|
|
|
public $Model, $Name, $RelationType, $RelatedClass;
|
|
|
|
|
|
|
|
function __construct($model, $name, $relatedClass, $relationType) {
|
|
|
|
$this->Model = $model;
|
|
|
|
$this->Name = $name;
|
|
|
|
$this->RelatedClass = $relatedClass;
|
|
|
|
$this->RelationType = $relationType;
|
2009-07-10 02:37:04 +02:00
|
|
|
|
|
|
|
parent::__construct();
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|