diff --git a/templates/bake/config/skeleton-anonymous.twig b/templates/bake/config/skeleton-anonymous.twig index 4b90bf40..1d19b868 100644 --- a/templates/bake/config/skeleton-anonymous.twig +++ b/templates/bake/config/skeleton-anonymous.twig @@ -37,39 +37,11 @@ return new class extends BaseMigration */ public function change(): void { -{% for table in tables %} - $table = $this->table('{{ table }}'); -{% if tableMethod != 'drop' %} -{% if columnMethod == 'removeColumn' %} -{% for column, config in columns['fields'] %} - $table->{{ columnMethod }}('{{ column }}'); -{% endfor %} -{% for column, config in columns['indexes'] %} - $table->{{ indexMethod }}([{{ - Migration.stringifyList(config['columns']) | raw - }}]); -{% endfor %} -{% else %} -{% for column, config in columns['fields'] %} - $table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{ - Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw - }}]); -{% endfor %} -{% for column, config in columns['indexes'] %} - $table->{{ indexMethod }}([{{ - Migration.stringifyList(config['columns'], {'indent': 3}) | raw }} - ], [{{ - Migration.stringifyList(config['options'], {'indent': 3}) | raw - }}]); -{% endfor %} -{% if tableMethod == 'create' and columns['primaryKey'] is not empty %} - $table->addPrimaryKey([{{ - Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw - }}]); -{% endif %} -{% endif %} -{% endif %} - $table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %}; -{% endfor %} +{{ element('Migrations.change-method-body', { + columnMethod: columnMethod, + indexMethod: indexMethod, + tableMethod: tableMethod, + wantedOptions: wantedOptions, +}) }} } }; diff --git a/templates/bake/config/skeleton.twig b/templates/bake/config/skeleton.twig index fae2eb0d..745aeb8d 100644 --- a/templates/bake/config/skeleton.twig +++ b/templates/bake/config/skeleton.twig @@ -38,39 +38,11 @@ class {{ name }} extends BaseMigration */ public function change(): void { -{% for table in tables %} - $table = $this->table('{{ table }}'); - {%~ if tableMethod != 'drop' %} - {%~ if columnMethod == 'removeColumn' %} - {%~ for column, config in columns['fields'] %} - $table->{{ columnMethod }}('{{ column }}'); - {%~ endfor %} - {%~ for column, config in columns['indexes'] %} - $table->{{ indexMethod }}([{{ - Migration.stringifyList(config['columns']) | raw - }}]); - {%~ endfor %} - {%~ else %} - {%~ for column, config in columns['fields'] %} - $table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{ - Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw - }}]); - {%~ endfor %} - {%~ for column, config in columns['indexes'] %} - $table->{{ indexMethod }}([{{ - Migration.stringifyList(config['columns'], {'indent': 3}) | raw }} - ], [{{ - Migration.stringifyList(config['options'], {'indent': 3}) | raw - }}]); - {%~ endfor %} - {%~ if tableMethod == 'create' and columns['primaryKey'] is not empty %} - $table->addPrimaryKey([{{ - Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw - }}]); - {%~ endif %} - {%~ endif %} -{% endif %} - $table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %}; -{% endfor %} +{{ element('Migrations.change-method-body', { + columnMethod: columnMethod, + indexMethod: indexMethod, + tableMethod: tableMethod, + wantedOptions: wantedOptions, +}) }} } } diff --git a/templates/bake/element/change-method-body.twig b/templates/bake/element/change-method-body.twig new file mode 100644 index 00000000..64815f01 --- /dev/null +++ b/templates/bake/element/change-method-body.twig @@ -0,0 +1,49 @@ +{# +/** + * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) + * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) + * + * Licensed under The MIT License + * For full copyright and license information, please see the LICENSE.txt + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) + * @link https://cakephp.org CakePHP(tm) Project + * @since 5.0.0 + * @license https://www.opensource.org/licenses/mit-license.php MIT License + */ +#} +{% for table in tables %} + $table = $this->table('{{ table }}'); + {%~ if tableMethod != 'drop' %} + {%~ if columnMethod == 'removeColumn' %} + {%~ for column, config in columns['fields'] %} + $table->{{ columnMethod }}('{{ column }}'); + {%~ endfor %} + {%~ for column, config in columns['indexes'] %} + $table->{{ indexMethod }}([{{ + Migration.stringifyList(config['columns']) | raw + }}]); + {%~ endfor %} + {%~ else %} + {%~ for column, config in columns['fields'] %} + $table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{ + Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw + }}]); + {%~ endfor %} + {%~ for column, config in columns['indexes'] %} + $table->{{ indexMethod }}([{{ + Migration.stringifyList(config['columns'], {'indent': 3}) | raw }} + ], [{{ + Migration.stringifyList(config['options'], {'indent': 3}) | raw + }}]); + {%~ endfor %} + {%~ if tableMethod == 'create' and columns['primaryKey'] is not empty %} + $table->addPrimaryKey([{{ + Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw + }}]); + {%~ endif %} + {%~ endif %} +{% endif %} + $table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %}; +{%- endfor -%} diff --git a/tests/comparisons/Migration/testNoContents.php b/tests/comparisons/Migration/testNoContents.php index d2a6289a..19c2524b 100644 --- a/tests/comparisons/Migration/testNoContents.php +++ b/tests/comparisons/Migration/testNoContents.php @@ -15,5 +15,6 @@ class NoContents extends BaseMigration */ public function change(): void { + } }