Skip to content

Commit

Permalink
Added CodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Dec 5, 2008
1 parent aad373c commit 567b2e4
Show file tree
Hide file tree
Showing 23 changed files with 670 additions and 38 deletions.
14 changes: 14 additions & 0 deletions apps/blog/models/Class.class.php
@@ -0,0 +1,14 @@
<?php
/**
* !Source Default
* !Table classes
*/
class Class extends Model {
/** !Type integer */
public $id;

/** !Type string */
public $className;

}
?>
29 changes: 29 additions & 0 deletions apps/blog/models/fdas.class.php
@@ -0,0 +1,29 @@
<?php
/**
* !Source Default
* !Table classes
*/
class fdas extends Model {
/** !Type integer */
public $id;

/** !Type text */
public $name;

/** !Type integer */
public $parentId;

/** !Type integer */
public $packageId;

/** !Type text */
public $docComment;

/** !Type text */
public $file;

/** !Type integer */
public $lastModified;

}
?>
5 changes: 2 additions & 3 deletions bootstrap.php
Expand Up @@ -13,9 +13,8 @@
require_once('./recess-config.php');

Library::import('recess.diagnostics.Diagnostics');
set_error_handler('Diagnostics::handleError', E_ALL);
set_exception_handler('Diagnostics::handleException');

set_error_handler(array('Diagnostics','handleError'));
set_exception_handler(array('Diagnostics','handleException'));
Library::import('recess.framework.Coordinator');
Library::import('recess.http.Environment');
Coordinator::main(Environment::getRawRequest(), Config::$policy, Config::$applications, Config::getRoutes(), Config::$plugins);
Expand Down
8 changes: 4 additions & 4 deletions recess-config.php
Expand Up @@ -20,10 +20,10 @@
= array( // 'name' => array('sqlite:' . $_ENV['dir.documentRoot'] . 'recess/sqlite/default.db')
// 'name' => array('mysql:host=localhost;dbname=recess', 'username', 'password')
// 'sqlite2' => 'sqlite:' . $_ENV['dir.documentRoot'] . 'recess/sqlite/sqlite2.db',
// 'recess' => array(
// 'mysql:host=localhost;dbname=recess',
// 'recess',
// 'recess'),
'recess' => array(
'mysql:host=localhost;dbname=recess',
'recess',
'recess'),
);

// Paths to the recess and apps directories
Expand Down
Expand Up @@ -173,14 +173,116 @@ public function app($appClass) {
$this->app = $application;
}

/** !Route GET, model/gen */
public function createModel() {
/** !Route GET, app/$app/model/gen */
public function createModel($app) {
$this->sources = DbSources::getSources();
$this->tables = DbSources::getDefaultSource()->getTables();
$this->app = $app;
}

/** !Route POST, app/$app/model/gen */
public function generateModel($app) {
// TODO: Clean up this pile of crap
$values = $this->request->post;

$modelName = $values['modelName'];
$tableExists = $values['tableExists'] == 'yes' ? true : false;
if(!$tableExists) {
$dataSource = $values['existingDataSource'];
$createTable = false;
$tableName = $values['existingTableName'];
} else {
$dataSource = $values['dataSource'];
$createTable = $values['createTable'] == 'Yes' ? true : false;
$tableName = $values['tableName'];
}
$propertyNames = $values['fields'];
$types = $values['types'];
$requireds = $values['nullables'];
$defaultValues = $values['defaultValues'];

Library::import('recess.sources.db.orm.Model', true);
// Forcing b/c ModelDescriptor is in Model

$modelDescriptor = new ModelDescriptor($modelName, false);
$modelDescriptor->setSource($dataSource);
$modelDescriptor->setTable($tableName, false);

foreach($propertyNames as $i => $name) {
if($name == "") continue;
$property = new ModelProperty();
$property->name = $name;
$property->type = $types[$i];
$property->required = $requireds[$i];
$modelDescriptor->properties[] = $property;
}

Library::import('recess.sources.db.orm.ModelGen');
$this->modelCode = ModelGen::toCode($modelDescriptor, $_ENV['dir.temp'] . 'Model.class.php');

$app = new $app;
if(strpos($app->modelsPrefix,'recess.apps.') !== false) {
$base = $_ENV['dir.lib'];
} else {
$base = $_ENV['dir.apps'];
}
$path = $base . str_replace(Library::dotSeparator,Library::pathSeparator,$app->modelsPrefix);
$path .= $modelName . '.class.php';
$this->path = $path;

$this->modelWasSaved = false;
$this->codeGenMessage = '';
try {
if(file_exists($this->path)) {
if(file_get_contents($this->path) == $this->modelCode) {
$this->modelWasSaved = true;
} else {
$this->codeGenMessage = 'File already exists!';
}
} else {
file_put_contents($this->path, $this->modelCode);
$this->modelWasSaved = true;
}
} catch(Exception $e) {
$this->codeGenMessage = 'File could not be saved. Is models directory writeable?';
$this->modelWasSaved = false;
}

$this->modelName = $modelName;

$this->tableGenAttempted = $createTable;
$this->tableWasCreated = false;

return $this->ok('newModelComplete');
}

/** !Route GET, model/gen/analyzeModelName/$modelName */
public function analyzeModelName($modelName) {
Library::import('recess.lang.Inflector');
$this->tableName = Inflector::toPlural(Inflector::toUnderscores($modelName));
$this->isValid = preg_match('/^[a-zA-Z][_a-zA-z0-9]*$/', $modelName) == 1;
}

/** !Route GET, model/gen/getTables/$sourceName */
public function getTables($sourceName) {
$this->tables = DbSources::getSource($sourceName)->getTables();
}

/** !Route GET, model/gen/getTableProps/$sourceName/$tableName */
public function getTableProps($sourceName, $tableName) {
$source = DbSources::getSource($sourceName);
if($source == null) {
return $this->redirect($this->urlToMethod('home'));
} else {
$this->source = $source;
}
$this->sourceName = $sourceName;
$this->table = $tableName;
$this->columns = $this->source->getTableDefinition($tableName)->getColumns();
}

/** !Route GET, controller/gen */
public function createController() {

}

private function getApplication($appClass) {
Expand Down
Expand Up @@ -48,7 +48,6 @@ public function showTable($sourceName, $tableName) {
$this->sourceName = $sourceName;
$this->table = $tableName;
$this->columns = $this->source->getTableDefinition($tableName)->getColumns();

}

/** !Route GET, source/$sourceName/table/$tableName/drop */
Expand Down
2 changes: 1 addition & 1 deletion recess/lib/recess/apps/tools/views/apps/app.php
Expand Up @@ -17,7 +17,7 @@
)
); ?>"><?php echo get_class($app); ?></a></p>
<div class="span-6">
<h2 class="bottom">Models (<a href="<?php echo $controller->urlToMethod('createModel'); ?>">new</a>)</h2>
<h2 class="bottom">Models (<a href="<?php echo $controller->urlToMethod('createModel',get_class($app)); ?>">new</a>)</h2>
<p>Location: <a href="<?php echo $codeController->urlToMethod('packageInfo', substr($app->modelsPrefix,0,-1)); ?>"><?php echo $app->modelsPrefix; ?></a></p>
<?php
function printClassesInNamespace($namespace, $codeController) {
Expand Down
59 changes: 38 additions & 21 deletions recess/lib/recess/apps/tools/views/apps/createModel.php
Expand Up @@ -6,7 +6,7 @@
?>
<h1>New <strong>Model</strong> Helper</h1>
<p>The purpose of this helper is to help speed the process of creating Recess! Models. Please note <span class="highlight">this form is <strong>not</strong> child proof</span>!</p>
<form class="modelForm">
<form class="modelForm" method="POST" action="<?php echo $controller->urlToMethod('generateModel',$app); ?>">
<h2>Step 1) Name Your Model</h2>
<label for="modelName">Model Class Name:</label> <input id="modelName" type="text" name="modelName" />
<p>The name of your model must be a <span class="highlight">valid PHP class name</span>.</p>
Expand All @@ -31,25 +31,39 @@
</tr>
<tr id="tableNameRow">
<td><label for="tableName">Table Name:</label></td>
<td><input type="text" name="tableName" size="15" /></td>
<td><input id="tableName" type="text" name="tableName" size="15" /></td>
</tr>
</table>
</div>
<div class="span-6 last">
<h3><input type="radio" name="tableExists" value="no" /> Table already exists.</h3>
<table style="display:none">
<tr>
<td><label for="dataSource">Data Source:</label></td>
<td><select name="dataSource">
<option value="Default">Default</option>
<td><label for="existingDataSource">Data Source:</label></td>
<td><select id="existingDataSource" name="existingDataSource">
<option value="Default" selected>Default</option>
<?php
foreach($sources as $sourceName => $source):
if($sourceName != "Default"):
?>
<option value="<?php echo $sourceName; ?>"><?php echo $sourceName; ?></option>
<?php
endif;
endforeach;
?>
</select></td>
</tr>
<tr>
<td><label for="tableName">Table:</label></td>
<td><select name="tableName">
<option value=""></option>
<option value="Yes">classes</option>
<option value="No">packages</option>
<td><label for="existingTableName">Table:</label></td>
<td><select id="existingTableName" name="existingTableName">
<option value="" selected></option>
<?php
foreach($tables as $table):
?>
<option value="<?php echo $table; ?>"><?php echo $table; ?></option>
<?php
endforeach;
?>
</select></td>
</tr>
</table>
Expand All @@ -69,8 +83,8 @@
</thead>
<tbody id="propertiesForm">
<tr>
<td><input type="text" name="field[]" class="fieldName" /></td>
<td><select name="type[]">
<td><input type="text" name="fields[]" class="fieldName" /></td>
<td><select name="types[]" class="type">
<option value="string">String</option>
<option value="text">Text</option>

Expand All @@ -86,8 +100,8 @@
<option value="blob">Blob</option>
<option value="boolean">Boolean</option>
</select></td>
<td><input type="checkbox" name="nullable[]" value="1" checked="checked" /></td>
<td><input type="text" name="defaultValue[]" /></td>
<td><input class="nullable" type="checkbox" name="nullables[]" value="1" checked="checked" /></td>
<td><input class="defaultValue" type="text" name="defaultValues[]" /></td>
<td><input class="removeField" type="button" value="X"></input></td>
</tr>
</tbody>
Expand All @@ -108,12 +122,12 @@
</thead>
<tbody id="relationsForm">
<tr>
<td style="vertical-align:top"><input class="relationName" type="text" name="relation[]" /></td>
<td style="vertical-align:top"><select name="relationType[]">
<td style="vertical-align:top"><input class="relationName" type="text" name="relationNames[]" /></td>
<td style="vertical-align:top"><select name="relationTypes[]">
<option value="hasMany">Has Many</option>
<option value="belongsTo">Belongs To</option>
</select></td>
<td style="vertical-align:top"><select name="relationType[]">
<td style="vertical-align:top"><select name="relationTypes[]">
<option value="Books">Books</option>
<option value="Post">Post</option>
<option value="MyModel">MyModel</option>
Expand All @@ -124,14 +138,14 @@
<tr>
<td>Foreign Key:</td>
<td>
<input type="text" name="foreignKey[]" value="" />
<input type="text" name="foreignKeys[]" value="" />
</td>
</tr>
<tr>
<td>Through:</td>
<td>
<input type="checkbox" name="through[]" value="1" />
<select name="throughClass[]">
<input type="checkbox" name="throughs[]" value="1" />
<select name="throughClasses[]">
<option value="Posts">Posts</option>
<option value="Comments">Comments</option>
</select>
Expand All @@ -140,7 +154,7 @@
<tr>
<td>On Delete:</td>
<td>
<select name="onDelete[]">
<select name="onDeletes[]">
<option value="Cascade">Cascade</option>
<option value="Delete">Delete</option>
<option value="Nullify">Nullify</option>
Expand All @@ -154,6 +168,9 @@
</tbody>
</table>
<input type="button" class="addRelation" value="Add a Relationship" />
<hr />
<h2>Step 5) Go!</h2>
<input id="generateModel" type="submit" value="Generate Model" />
</form>
<table style="visibility:hidden;">
<tbody id="propertyTemplate">
Expand Down
37 changes: 37 additions & 0 deletions recess/lib/recess/apps/tools/views/apps/newModelComplete.php
@@ -0,0 +1,37 @@
<?php
$title = 'New Application Instructions';
$selectedNav = 'apps';
include_once($viewsDir . 'common/header.php');
?>
<h1>Introducing <strong><?php echo $modelName; ?></strong> Model...</h1>

<h3 class="bottom">Code Gen <?php if($modelWasSaved){ echo '<span class="added">Done</a>'; } else { echo '<span class="highlight">Almost Done</span>'; } ?></h3>
<?php if($modelWasSaved): ?>
<p>The <strong><?php echo $modelName; ?></strong> model was <span class="added">successfully saved</span> to <span class="added"><?php echo $path; ?></span>. If you'd like to take a peak at the generated code, expand the source below.</p>
<?php else:?>
<p><strong><?php echo $modelName; ?></strong> could not be saved. <?php echo $codeGenMessage; ?></p>
<p>To finish adding your model please <span class="highlight">save the code below</span> to the file <span class="highlight"><strong><?php echo $path; ?></strong></span>.</p>
<?php endif; ?>
<pre name="code" class="php<?php if($modelWasSaved) echo ':collapse'; ?>">
<?php echo str_replace('<','&lt;',$modelCode); ?>
</pre>

<h3>Table Gen
<?php
if(!$tableGenAttempted) {
echo ' - Skipped';
} else {
if($tableWasCreated) {
echo '<span class="added">Done</a>';
} else {
echo '<span class="highlight">Almost Done</span>';
}
}
?></h3>


<h2><span class="highlight">Next Steps</span></h2>

<?php
include_once($viewsDir . 'common/footer.php');
?>

0 comments on commit 567b2e4

Please sign in to comment.