Skip to content

Commit

Permalink
Various Corrections to Baked Files
Browse files Browse the repository at this point in the history
  • Loading branch information
beporter authored and justinyost committed Jun 20, 2016
1 parent 1eb60b3 commit e14e6b4
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 67 deletions.
55 changes: 7 additions & 48 deletions config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,17 @@
use Cake\Event\EventManager;

/**
* Install our own BakeHelper.
* Default to the LoadsysTheme and install our own BakeHelper.
*/
EventManager::instance()->on('Bake.initialize', function (Event $event) {
$view = $event->subject;

// Load our overridden BakeHelper class.
// Use the LoadsysTheme if none was explicitly named.
if (empty($view->theme())) {
$view->theme('LoadsysTheme');
}

// Swap in our overridden BakeHelper class.
$view->helpers()->unload('Bake');
$view->loadHelper('LoadsysTheme.Bake');
});

/**
* Change viewVars globally.
*/
// EventManager::instance()->on('Bake.beforeRender', function (Event $event) {
// $view = $event->subject;
//
// // Use $rows for the main data variable in indexes
// if ($view->get('pluralName')) {
// $view->set('pluralName', 'rows');
// }
// if ($view->get('pluralVar')) {
// $view->set('pluralVar', 'rows');
// }
//
// // Use $theOne for the main data variable in view/edit
// if ($view->get('singularName')) {
// $view->set('singularName', 'theOne');
// }
// if ($view->get('singularVar')) {
// $view->set('singularVar', 'theOne');
// }
// });

//
/**
* Example of injecting viewVars for a specific generated file:
*/
// EventManager::instance()->on(
// 'Bake.beforeRender.Controller.controller',
// function (Event $event) {
// $view = $event->subject();
// if ($view->viewVars['name'] == 'Users') {
// // add the login and logout actions to the Users controller
// $view->viewVars['actions'] = [
// 'login',
// 'logout',
// 'index',
// 'view',
// 'add',
// 'edit',
// 'delete'
// ];
// }
// }
// );
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/edit.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $compact = ["'" . $singularName . "'"];
*/
public function edit($id = null) {
$<%= $singularName %> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Bake->stringifyList($belongsToMany, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($belongsToMany, ['indent' => false]) %>],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$<%= $singularName %> = $this-><%= $currentModelName %>->patchEntity($<%= $singularName %>, $this->request->data);
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<% if ($belongsTo):
$belongsTo = array_diff($belongsTo, $ignoreAssociations); %>
$this->paginate = [
'contain' => [<%= $this->Bake->stringifyList($belongsTo, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($belongsTo, ['indent' => false]) %>],
];
<% endif; %>
$this->set('<%= $pluralName %>', $this->paginate($this-><%= $currentModelName %>));
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/Controller/view.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $allAssociations = array_merge(
*/
public function view($id = null) {
$<%= $singularName%> = $this-><%= $currentModelName %>->get($id, [
'contain' => [<%= $this->Bake->stringifyList($allAssociations, ['indent' => false]) %>]
'contain' => [<%= $this->Bake->stringifyList($allAssociations, ['indent' => false]) %>],
]);
$this->set('<%= $singularName %>', $<%= $singularName %>);
$this->set('_serialize', ['<%= $singularName %>']);
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/Element/add-foreign-keys-from-create.ctp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<% foreach ($constraints as $table => $tableConstraints):
echo $this->element('Migrations.add-foreign-keys', ['constraints' => $tableConstraints, 'table' => $table]);
echo $this->element('LoadsysTheme.add-foreign-keys', ['constraints' => $tableConstraints, 'table' => $table]);
endforeach; %>
8 changes: 5 additions & 3 deletions src/Template/Bake/Element/create-tables.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ $specialPk = (count($primaryKeys) > 1 || $primaryKeys[0]['name'] !== 'id' || $pr
%>
<% if ($specialPk): %>

$this->table('<%= $tableArgForArray %>', ['id' => false, 'primary_key' => ['<%= implode("', '", \Cake\Utility\Hash::extract($primaryKeys, '{n}.name')) %>']])
$this->table('<%= $tableArgForArray %>', ['id' => false, 'primary_key' => ['<%= implode("', '", \Cake\Utility\Hash::extract($primaryKeys, '{n}.name')) %>'], ['comment' => '']])
<% else: %>

$this->table('<%= $tableArgForArray %>')
$this->table('<%= $tableArgForArray %>', ['comment' => ''])
<% endif; %>
<% if ($specialPk || !$autoId):
foreach ($primaryKeys as $primaryKey) :
Expand All @@ -30,6 +30,8 @@ foreach ($this->Migration->columns($tableArgForMethods) as $column => $config):
%>
->addColumn('<%= $column %>', '<%= $config['columnType'] %>', [<%
$columnOptions = $this->Migration->getColumnOption($config['options']);
$columnOptions['comment'] = '';
//@TODO: init proper integrity checks matching MigrationsTest from skeleton. (Example: int id must be signed=false)
if ($config['columnType'] === 'boolean' && isset($columnOptions['default']) && $this->Migration->value($columnOptions['default']) !== 'null'):
$columnOptions['default'] = (bool)$columnOptions['default'];
endif;
Expand Down Expand Up @@ -76,5 +78,5 @@ endforeach; %>
->create();
<% endforeach; %>
<% if (!empty($constraints)): %>
<% echo $this->element('Migrations.add-foreign-keys-from-create', ['constraints' => $constraints]); %>
<% echo $this->element('LoadsysTheme.add-foreign-keys-from-create', ['constraints' => $constraints]); %>
<% endif; %>
12 changes: 6 additions & 6 deletions src/Template/Bake/config/diff.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class <%= $name %> extends AbstractMigration {
<%- endif; %>
<%- endforeach; %>
<%- if (!empty($tables['add'])): %>
<%- echo $this->element('Migrations.create-tables', ['tables' => $tables['add'], 'autoId' => $autoId, 'useSchema' => true]) %>
<%- echo $this->element('LoadsysTheme.create-tables', ['tables' => $tables['add'], 'autoId' => $autoId, 'useSchema' => true]) %>
<%- endif; %>
<%- foreach ($data as $tableName => $tableDiff): %>
<%- if (!empty($tableDiff['columns']['add'])):
Expand All @@ -113,14 +113,14 @@ class <%= $name %> extends AbstractMigration {

<%= $statement %>
<%- endif; %>
<%- echo $this->element('Migrations.add-columns', ['columns' => $tableDiff['columns']['add']]) %>
<%- echo $this->element('LoadsysTheme.add-columns', ['columns' => $tableDiff['columns']['add']]) %>
<%- endif; %>
<%- if (!empty($tableDiff['indexes']['add'])):
$statement = $this->Migration->tableStatement($tableName);
if (!empty($statement)): %>
<%= $statement %>
<%- endif; %>
<%- echo $this->element('Migrations.add-indexes', ['indexes' => $tableDiff['indexes']['add']]) %>
<%- echo $this->element('LoadsysTheme.add-indexes', ['indexes' => $tableDiff['indexes']['add']]) %>
<%- endif;
if (isset($this->Migration->tableStatements[$tableName])): %>
->update();
Expand Down Expand Up @@ -170,7 +170,7 @@ class <%= $name %> extends AbstractMigration {
<%- endforeach; %>
<%- endif; %>
<%- if (!empty($tables['remove'])): %>
<%- echo $this->element('Migrations.create-tables', ['tables' => $tables['remove'], 'autoId' => $autoId, 'useSchema' => true]) %>
<%- echo $this->element('LoadsysTheme.create-tables', ['tables' => $tables['remove'], 'autoId' => $autoId, 'useSchema' => true]) %>
<%- endif; %>
<%- foreach ($data as $tableName => $tableDiff): %>
<%- if (!empty($tableDiff['indexes']['add'])): %>
Expand All @@ -190,7 +190,7 @@ class <%= $name %> extends AbstractMigration {
<%= $this->Migration->tableStatement($tableName, true) %>
<%- endif; %>
<%- if (!empty($tableDiff['columns']['remove'])): %>
<%- echo $this->element('Migrations.add-columns', ['columns' => $tableDiff['columns']['remove']]) %>
<%- echo $this->element('LoadsysTheme.add-columns', ['columns' => $tableDiff['columns']['remove']]) %>
<%- endif; %>
<%- if (!empty($tableDiff['columns']['changed'])):
$oldTableDef = $dumpSchema[$tableName];
Expand All @@ -213,7 +213,7 @@ class <%= $name %> extends AbstractMigration {
<%- endforeach; %>
<%- endif; %>
<%- if (!empty($tableDiff['indexes']['remove'])): %>
<%- echo $this->element('Migrations.add-indexes', ['indexes' => $tableDiff['indexes']['remove']]) %>
<%- echo $this->element('LoadsysTheme.add-indexes', ['indexes' => $tableDiff['indexes']['remove']]) %>
<%- endif;
if (isset($this->Migration->tableStatements[$tableName])): %>
->update();
Expand Down
4 changes: 2 additions & 2 deletions src/Template/Bake/config/skeleton.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class <%= $name %> extends AbstractMigration {
*/
public function change() {
<% foreach ($tables as $table): %>
$table = $this->table('<%= $table%>');
$table = $this->table('<%= $table%>', ['comment' => '']);
<% if ($tableMethod !== 'drop') : %>
<% if ($columnMethod === 'removeColumn'): %>
<% foreach ($columns['fields'] as $column => $config): %>
Expand All @@ -61,14 +61,14 @@ class <%= $name %> extends AbstractMigration {
$table-><%= $columnMethod %>('<%= $column %>', '<%= $config['columnType'] %>', [<%
$columnOptions = $config['options'];
$columnOptions = array_intersect_key($columnOptions, $wantedOptions);
$columnOptions['comment'] = '';
echo $this->Bake->stringifyList($columnOptions, ['indent' => 3]);
%>]);
<% endforeach; %>
<% foreach ($columns['indexes'] as $column => $config): %>
$table-><%= $indexMethod %>([<%=
$this->Bake->stringifyList($config['columns'], ['indent' => 3])
%>], [<%
$options = [];
echo $this->Bake->stringifyList($config['options'], ['indent' => 3]);
%>]);
<% endforeach; %>
Expand Down
2 changes: 1 addition & 1 deletion src/Template/Bake/config/snapshot.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class <%= $name %> extends AbstractMigration {
* @return void
*/
public function up() {
<%- echo $this->element('Migrations.create-tables', ['tables' => $tables, 'autoId' => $autoId, 'useSchema' => false]) %>
<%- echo $this->element('LoadsysTheme.create-tables', ['tables' => $tables, 'autoId' => $autoId, 'useSchema' => false]) %>
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/View/Helper/BakeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class BakeHelper extends BaseBakeHelper {
/**
* Returns an array converted into a formatted multiline string
*
* This version uses tabs by default, and includes a trailing comma on
* multi-line arrays by default.
* This version uses tabs by default, and includes a trailing comma (on
* multi-line arrays only) by default.
*
* @param array $list array of items to be stringified
* @param array $options options to use
Expand All @@ -25,7 +25,12 @@ public function stringifyList(array $list, array $options = []) {
$options += [
'indent' => 2,
'tab' => "\t",
'trailingComma' => true,
];

// MUST be layered in a second pass to pick up the layered [indent]
// value from the first pass!
$options += [
'trailingComma' => ($options['indent'] !== false),
];

return parent::stringifyList($list, $options);
Expand Down

0 comments on commit e14e6b4

Please sign in to comment.