Phinx - пользовательский шаблон миграции, передающий параметры в шаблон - PullRequest
0 голосов
/ 08 ноября 2019

Я создал файл-заглушку / шаблон, который я хотел бы использовать для создания миграций

<?php

use Phinx\Migration\AbstractMigration;

class DummyTableMigration extends AbstractMigration
{
    public function up()
    {
        // Create the table
        $table = $this->table('table_name');

        $table->addColumn('column_name', 'string', ['limit' => 255])
              ->create();
    }

    public function down()
    {
        $this->table('table_name')->drop()->save();
    }
}

Этот код я использую для создания миграций с помощью компонента Symfony Console. Я передаю опцию -t, поскольку хочу сгенерировать миграцию с использованием созданного мной пользовательского шаблона, но не уверен, как я могу заменить DummyTableMigration именем класса, которое я хочу использовать. Нужно ли передавать его как дополнительный параметр в пределах ArrayInput?

$phinx = new PhinxApplication();
    $input = new ArrayInput([
        'command' => 'create',
        'name' => $input->getArgument('name'),
        '-c' => './config/phinx.php',
        '-t' => '../../Console/stubs/migrations/customTemplateMigration.stub'),
    ]);

    return $phinx->find('create')->run($input, $output);

1 Ответ

0 голосов
/ 11 ноября 2019

Проверьте шаблон по умолчанию, он использует переменные стиля PHP для имени класса и различные другие аспекты:

<?php
$namespaceDefinition
use $useClassName;

class $className extends $baseClassName
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    addCustomColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Any other destructive changes will result in an error when trying to
     * rollback the migration.
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {

    }
}

https://github.com/cakephp/phinx/blob/v0.11.0/src/Phinx/Migration/Migration.template.php.dist

Все доступные переменные можно найти вCreate код команды: https://github.com/cakephp/phinx/blob/v0.11.0/src/Phinx/Console/Command/Create.php#L281-L288

...