Skip to content

Commit

Permalink
add Volt support in Scaffold
Browse files Browse the repository at this point in the history
Usage:
    phalcon scaffold TABLE_NAME --template-engine=volt
  • Loading branch information
wenchen committed Apr 28, 2013
1 parent 011a38b commit 5dc34ad
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 11 deletions.
230 changes: 220 additions & 10 deletions scripts/Phalcon/Builder/Scaffold.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private function _getPossiblePlural($className)
if (substr($className, strlen($className) - 1, 1) == 's') {
return $className;
}
return $className;
}

public function build()
Expand Down Expand Up @@ -197,20 +198,37 @@ public function build()
//Build Controller
$this->_makeController($path, $options);

//View layouts
$this->_makeLayouts($path, $options);
if ($options['templateEngine'] == 'volt') {
//View layouts
$this->_makeLayoutsVolt($path, $options);

//View index.phtml
$this->_makeViewIndex($path, $options);
//View index.phtml
$this->_makeViewIndexVolt($path, $options);

//View search.phtml
$this->_makeViewSearch($path, $options);
//View search.phtml
$this->_makeViewSearchVolt($path, $options);

//View new.phtml
$this->_makeViewNew($path, $options);
//View new.phtml
$this->_makeViewNewVolt($path, $options);

//View edit.phtml
$this->_makeViewEdit($path, $options);
//View edit.phtml
$this->_makeViewEditVolt($path, $options);
} else {
//View layouts
$this->_makeLayouts($path, $options);

//View index.phtml
$this->_makeViewIndex($path, $options);

//View search.phtml
$this->_makeViewSearch($path, $options);

//View new.phtml
$this->_makeViewNew($path, $options);

//View edit.phtml
$this->_makeViewEdit($path, $options);
}

return true;
}
Expand Down Expand Up @@ -314,6 +332,45 @@ private function _makeField($attribute, $dataType, $relationField, $selectDefini
return $code;
}

private function _makeFieldVolt($attribute, $dataType, $relationField, $selectDefinition)
{
$code = "\t" . '<tr>' . PHP_EOL .
"\t\t" . '<td align="right">' . PHP_EOL .
"\t\t\t" . '<label for="' . $attribute . '">' . $this->_getPossibleLabel($attribute) . '</label>' . PHP_EOL .
"\t\t" . '</td>' . PHP_EOL .
"\t\t" . '<td align="left">';

if(isset($relationField[$attribute])){
$code .= PHP_EOL . "\t\t\t\t" . '{{ select("' . $attribute . '", ' . $selectDefinition[$attribute]['varName'] .
', "using" :[ "' . $selectDefinition[$attribute]['primaryKey'] . ',' . $selectDefinition[$attribute]['detail'] . '", "useDummy" => true]) }}';
} else {

switch ($dataType) {
case Column::TYPE_CHAR:
$code .= PHP_EOL . "\t\t\t\t" . '{{ text_field("' . $attribute . '") }}';
break;
case Column::TYPE_DECIMAL:
case Column::TYPE_INTEGER:
$code .= PHP_EOL . "\t\t\t" . '{{ text_field("' . $attribute . '", "type" : "numeric") }}';
break;
case Column::TYPE_DATE:
$code .= PHP_EOL . "\t\t\t\t" . '{{ text_field("' . $attribute . '", "type" : "date") }}';
break;
case Column::TYPE_TEXT:
$code .= PHP_EOL . "\t\t\t\t" . '{{ text_field("' . $attribute . '", "type" : "date") }}';
break;
default:
$code .= PHP_EOL . "\t\t\t" . '{{ text_field("' . $attribute . '", "size" : 30) }}';
break;
}
}

$code .= PHP_EOL . "\t\t" . '</td>';
$code .= PHP_EOL . "\t" . '</tr>' . PHP_EOL;

return $code;
}

/**
* Build fields for different actions
*
Expand Down Expand Up @@ -343,6 +400,27 @@ private function _makeFields($path, $options, $action)
return $code;
}

private function _makeFieldsVolt($path, $options, $action)
{

$entity = $options['entity'];
$relationField = $options['relationField'];
$autocompleteFields = $options['autocompleteFields'];
$selectDefinition = $options['selectDefinition'];
$identityField = $options['identityField'];

$code = '';
foreach ($options['dataTypes'] as $attribute => $dataType) {

if (($action == 'new' || $action == 'edit' ) && $attribute == $identityField) {
continue;
}

$code .= $this->_makeFieldVolt($attribute, $dataType, $relationField, $selectDefinition);
}
return $code;
}

/**
* Generate controller using scaffold
*
Expand Down Expand Up @@ -426,6 +504,40 @@ private function _makeLayouts($path, $options)
}
}

private function _makeLayoutsVolt($path, $options)
{

//Make Layouts dir
$dirPathLayouts = $options['viewsDir'] . '/layouts';

//If not exists dir; we make it
if (is_dir($dirPathLayouts) == false) {
mkdir($dirPathLayouts);
}

$fileName = Text::uncamelize($options['name']);
$viewPath = $dirPathLayouts . '/' . $fileName . '.volt';
if (!file_exists($viewPath)) {

//View model layout
$code = '';
if (isset($options['theme'])) {
$code.='{{ stylesheet_link("themes/lightness/style") }}'.PHP_EOL;
$code.='{{ stylesheet_link("themes/base") }}'.PHP_EOL;
}

if (isset($options['theme'])) {
$code .= '<div class="ui-layout" align="center">' . PHP_EOL;
} else {
$code .= '<div align="center">' . PHP_EOL;
}
$code .= "\t" . '{{ content() }}' . PHP_EOL . '</div>';
$code = str_replace("\t", " ", $code);
file_put_contents($viewPath, $code);

}
}

private function makeView($path, $options, $type)
{

Expand Down Expand Up @@ -457,6 +569,37 @@ private function makeView($path, $options, $type)
file_put_contents($viewPath, $code);
}

private function makeViewVolt($path, $options, $type)
{

$dirPath = $options['viewsDir'] . $options['name'];
if (is_dir($dirPath) == false) {
mkdir($dirPath);
}

$viewPath = $dirPath . '/' .$type. '.volt';
if (file_exists($viewPath)) {
return;
}

$templatePath = $options['templatePath'] . '/scaffold/no-forms/views/' .$type. '.volt';
if (!file_exists($templatePath)) {
throw new BuilderException("Template '" . $templatePath . "' does not exist");
}

$code = file_get_contents($templatePath);

$code = str_replace('$plural$', $options['plural'], $code);
$code = str_replace('$captureFields$', self::_makeFieldsVolt($path, $options, $type), $code);

if ($this->isConsole()) {
echo $viewPath, PHP_EOL;
}

$code = str_replace("\t", " ", $code);
file_put_contents($viewPath, $code);
}

/**
* Creates main view
*
Expand All @@ -468,6 +611,11 @@ private function _makeViewIndex($path, $options)
$this->makeView($path, $options, 'index');
}

private function _makeViewIndexVolt($path, $options)
{
$this->makeViewVolt($path, $options, 'index');
}

/**
* Creates the view to create a new item
*
Expand All @@ -479,6 +627,11 @@ private function _makeViewNew($path, $options)
$this->makeView($path, $options, 'new');
}

private function _makeViewNewVolt($path, $options)
{
$this->makeViewVolt($path, $options, 'new');
}

/**
* Make views index.phtml of model by scaffold
*
Expand All @@ -490,6 +643,11 @@ private function _makeViewEdit($path, $options)
$this->makeView($path, $options, 'edit');
}

private function _makeViewEditVolt($path, $options)
{
$this->makeViewVolt($path, $options, 'edit');
}

/**
* Make view search.phtml of model by scaffold
*
Expand Down Expand Up @@ -547,5 +705,57 @@ private function _makeViewSearch($path, $options)
$code = str_replace("\t", " ", $code);
file_put_contents($viewPath, $code);
}

private function _makeViewSearchVolt($path, $options)
{

$dirPath = $options['viewsDir'] . $options['name'];
if (is_dir($dirPath) == false) {
mkdir($dirPath);
}

$viewPath = $dirPath . '/search.volt';
if (file_exists($viewPath)) {
return;
}

$templatePath = $options['templatePath'] . '/scaffold/no-forms/views/search.volt';
if (!file_exists($templatePath)) {
throw new BuilderException("Template '" . $templatePath . "' does not exist");
}

$headerCode = '';
foreach ($options['attributes'] as $attribute) {
$headerCode .= "\t\t\t" . '<th>' . $this->_getPossibleLabel($attribute) . '</th>' . PHP_EOL;
}

$rowCode = '';
$options['allReferences'] = array_merge($options['autocompleteFields'], $options['selectDefinition']);
foreach ($options['dataTypes'] as $fieldName => $dataType) {
$rowCode .= "\t\t\t" . '<td>{{ ';
if (!isset($options['allReferences'][$fieldName])) {
$rowCode .= $options['singular'] . '.' . $fieldName;
} else {
$detailField = ucfirst($options['allReferences'][$fieldName]['detail']);
$rowCode .= $options['singular'] . '.get' . $options['allReferences'][$fieldName]['tableName'] . '().get' . $detailField . '()';
}
$rowCode .= ' }}</td>' . PHP_EOL;
}

$code = file_get_contents($templatePath);

$code = str_replace('$plural$', $options['plural'], $code);
$code = str_replace('$headerColumns$', $headerCode, $code);
$code = str_replace('$rowColumns$', $rowCode, $code);
$code = str_replace('$singularVar$', $options['singular'], $code);
$code = str_replace('$pk$', $options['attributes'][0], $code);

if ($this->isConsole()) {
echo $viewPath, PHP_EOL;
}

$code = str_replace("\t", " ", $code);
file_put_contents($viewPath, $code);
}
}

5 changes: 4 additions & 1 deletion scripts/Phalcon/Commands/Builtin/Scaffold.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Scaffold extends Command implements CommandsInterface
'get-set' => "Attributes will be protected and have setters/getters. [optional]",
'directory=s' => "Base path on which project was created [optional]",
'template-path' => 'Specify a template path [optional]',
'template-engine'=> 'Define the template engine, default php (php, volt). [optional]',
'force' => "Forces to rewrite generated code if they already exists. [optional]",
'trace' => "Shows the trace of the framework in case of exception. [optional]",
);
Expand All @@ -56,14 +57,16 @@ public function run($parameters)
$name = $this->getOption(array('table-name', 1));
$templatePath = $this->getOption(array('template-path'), null, TEMPLATE_PATH);
$schema = $this->getOption('schema');
$templateEngint = $this->getOption(array('template-engine'), null, "php");

$scaffoldBuilder = new scaffoldBuilder(array(
'name' => $name,
'schema' => $schema,
'force' => $this->isReceivedOption('force'),
'genSettersGetters' => $this->isReceivedOption('get-set'),
'directory' => $this->getOption('directory'),
'templatePath' => $templatePath
'templatePath' => $templatePath,
'templateEngine'=> $templateEngint,
));

return $scaffoldBuilder->build();
Expand Down
25 changes: 25 additions & 0 deletions templates/scaffold/no-forms/views/edit.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{{ content() }}

{{ submit_button("Save") }}

<table width="100%">
<tr>
<td align="left">{{ link_to("$plural$", "Go Back") }}</td>
<td align="right">{{ submit_button("Save") }}</td>
<tr>
</table>

<div align="center">
<h1>Edit $plural$</h1>
</div>

<table>
$captureFields$
<tr>
<td>{{ hidden_field("id") }}</td>
<td>{{ submit_button("Search") }}</td>
</tr>
</table>

</form>
22 changes: 22 additions & 0 deletions templates/scaffold/no-forms/views/index.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

{{ content() }}

<div align="right">
{{ link_to("$plural$/new", "Create $plural$") }}
</div>

{{ form("$plural$/search", "method":"post", "autocomplete" : "off") }}

<div align="center">
<h1>Search $plural$</h1>
</div>

<table>
$captureFields$
<tr>
<td></td>
<td>{{ submit_button("Search") }}</td>
</tr>
</table>

</form>

0 comments on commit 5dc34ad

Please sign in to comment.