baumrock/rockmigrations

安装: 443

依赖关系: 0

建议者: 0

安全: 0

星标: 33

关注者: 3

分支: 10

开放问题: 1

类型:processwire-module


README


在此查看视频



模块描述

RockMigrations 提供了一个简单的 API,可以执行在 PW 后端通过代码可以做的所有事情。这意味着您可以通过添加所有必要的字段和模板(不是通过点击,而是通过编写简单的脚本来执行这些任务)来简单地全面控制您的网站或应用。

该模块还包含几个助手,使得实现完全自动化的 CI/CD 流程变得极其简单。

Wiki

查看WIKI以获取快速入门和文档

Pro-Fields 和 3rd-Party-Fields

尽管 RockMigrations 有效地处理了大多数数据库架构更改,但仍需指出,并非所有 ProcessWire pro-fields 或 3rd-party-fields 都可能得到支持。在不支持的字段的情况下,用户可以选择赞助添加支持,或者选择使用传统方法手动迁移,模拟没有 RockMigrations 的场景。这种方法确保了 ProcessWire CMS 生态系统内不同字段类型的通用性。

另外,不要忘记,即使您正在使用 RockMigrations,您仍然可以使用 PW API 做到您需要的或想要的所有事情。这意味着您可以使用 RockMigrations 来支持所有内容(这可能占任何人需要的99%),而对于剩余的1%或更少的内容,您可以使用纯 PW API,或者如果可能的话,手动进行更改。

我在哪里可以找到所有这些字段和模板属性?

  1. 您可以通过编辑您的字段或模板并从中复制代码来获取(我建议只复制您需要的设置,以便使迁移文件更易于阅读):img

  2. 将鼠标悬停在您想要设置的设置字段的最右侧的箭头上:img

魔法

RockMigrations 不仅帮助您进行迁移和部署,还添加了许多助手,使得使用 ProcessWire 进行开发变得更加有趣。

请参阅 WIKI 以了解 MagicPages!

代码片段

另一个帮助您开始使用迁移语法的选项是使用提供的 VSCode 代码片段。我强烈建议在您的配置中启用 syncSnippets 选项。

// site/config.php
$config->rockmigrations = [
  "syncSnippets" => true,
];

监视文件、路径或模块

RockMigrations 可以监视文件、路径和模块的更改。它将检测监视列表中任何文件的更改,并在有任何更改时触发迁移运行。

从版本 1.0.0(2022年8月29日)开始,如果某个文件发生变化,RockMigrations 不会运行所有迁移,而只会迁移单个更改的文件。这使得迁移运行速度更快!

当从 CLI 运行时,它仍将运行每个迁移文件,以确保一切按预期工作,并且没有错过任何更改。

有时即使文件未发生变化,也必须进行迁移。RockMatrix 就是这样的一个例子,其中模块文件会触发所有 Matrix-Blocks 的迁移。在这种情况下,您可以使用 force 选项将该文件添加到监视列表。

// inside RockMatrix::init
$rm->watch($this, true, ['force'=>true]);

监视模块

您可以轻松监视任何 ProcessWire 模块的变化,并在文件更改时触发 migrate() 方法。

// module needs to be autoload!
public function init() {
  $rm = $this->wire->modules->get('RockMigrations');
  if($rm) $rm->watch($this);
}
public function migrate() {
  bd('Migrating MyModule...');
}

监视文件

您可以监视单个文件或整个路径。

$rm->watch(__FILE__, false);
$rm->watch(__DIR__."/foo");

请注意,如果文件不应该进行迁移,但只监视变化,则需要将第二个参数定义为 FALSE。如果将其设置为 TRUE,则文件将包含并执行,就像它是迁移脚本一样(请参见下面的示例)。

运行迁移

RockMigrations 将在监视的文件更改时自动运行迁移。如果您想手动触发迁移(例如在部署后),可以使用 migrate.php 文件。

php site/modules/RockMigrations/migrate.php

有时您想对一个文件进行工作,并希望它监视变化,但您不希望始终触发迁移。例如,当在标记或 LESS 上工作。在这种情况下,您可以禁用迁移的自动运行。

$config->noMigrate = true;

这可以防止运行迁移,但文件仍然会监视变化,您仍然可以从 CLI 触发迁移。

在 migrationsDone 后挂钩

如果您想在所有其他迁移运行后应用迁移,可以在 migrationsDone 后挂钩。

$wire->addHookAfter("RockMigrations::migrationsDone", function(HookEvent $event) {
  /** @var RockMigrations $rm */
  $rm = $event->object;
  $rm->removeFieldFromTemplate('title', 'field-profilephoto');
  $rm->removeFieldFromTemplate('title', 'field-pressphoto');
});

按需获取文件

您可以指示 RockMigrations 从远程服务器按需下载文件。这使得在远程系统(例如在实时服务器上)创建内容,将数据库中的数据拉取到您的本地机器成为可能。一旦打开页面,RockMigrations 将从您的远程服务器获取缺少的文件。

// without authentication
$config->filesOnDemand = 'https://example.com';

// with http basic authentication
$config->filesOnDemand = 'https://user:password@example.com';

YAML

$rm->watch("/your/file.yaml");
fields:
  foo:
    type: text
    label: My foo field

PHP

$rm->watch("/your/file.php");
<?php namespace ProcessWire;
$rm->createField('foo', 'text');

自动监视

RockMigrations 会自动监视 /site/migrate.phpYourModule.migrate.php 等文件。

处理 YAML 文件

RockMigrations 附带了 Spyc 库以读取/写入 YAML 文件。

// get YAML instance
$rm->yaml();

// get array from YAML file
$rm->yaml('/path/to/file.yaml');

// save data to file
$rm->yaml('/path/to/file.yaml', ['foo'=>'bar']);

处理字段集

处理字段集很麻烦,因为它们需要有一个开标签和一个闭标签。这使得从迁移的角度处理它变得复杂,但 RockMigrations 有一个漂亮的小助手方法,可以在运行时包装其他字段。

// syntax
$rm->wrapFields($form, $fields, $fieldset);

// usage
$wire->addHookAfter("ProcessPageEdit::buildForm", function($event) {
  $form = $event->return;

  /** @var RockMigrations $rm */
  $rm = $this->wire->modules->get('RockMigrations');
  $rm->wrapFields($form, [
    'title' => [
      // runtime settings for title field
      'columnWidth' => 50,
    ],
    // runtime field example
    [
      'type' => 'markup',
      'label' => 'foo',
      'value' => 'bar',
      'columnWidth' => 50,
    ],
    'other_field_of_this_template',
  ], [
    'label' => 'I am a new fieldset wrapper',
  ]);
})

迁移示例

字段迁移

CKEditor 字段

$rm->migrate([
  'fields' => [
    'yourckefield' => [
      'type' => 'textarea',
      'tags' => 'MyTags',
      'inputfieldClass' => 'InputfieldCKEditor',
      'contentType' => FieldtypeTextarea::contentTypeHTML,
      'rows' => 5,
      'formatTags' => "h2;p;",
      'contentsCss' => "/site/templates/main.css?m=".time(),
      'stylesSet' => "mystyles:/site/templates/mystyles.js",
      'toggles' => [
        InputfieldCKEditor::toggleCleanDIV, // convert <div> to <p>
        InputfieldCKEditor::toggleCleanP, // remove empty paragraphs
        InputfieldCKEditor::toggleCleanNBSP, // remove &nbsp;
      ],
    ],
  ],
]);

图片字段

$rm->migrate([
  'fields' => [
    'yourimagefield' => [
      'type' => 'image',
      'tags' => 'YourTags',
      'maxFiles' => 0,
      'descriptionRows' => 1,
      'extensions' => "jpg jpeg gif png svg",
      'okExtensions' => ['svg'],
      'icon' => 'picture-o',
      'outputFormat' => FieldtypeFile::outputFormatSingle,
      'maxSize' => 3, // max 3 megapixels
    ],
  ],
]);

文件字段

$rm->migrate([
  'fields' => [
    'yourfilefield' => [
      'type' => 'file',
      'tags' => 'YourTags',
      'maxFiles' => 1,
      'descriptionRows' => 0,
      'extensions' => "pdf",
      'icon' => 'file-o',
      'outputFormat' => FieldtypeFile::outputFormatSingle,
    ],
  ],
]);

选项字段

$rm->migrate([
  'fields' => [
    'yourfield' => [
      'type' => 'options',
      'tags' => 'YourTags',
      'label' => 'Options example',
      'options' => [
        1 => 'ONE|This is option one',
        2 => 'TWO',
        3 => 'THREE',
      ],
    ],
  ],
]);

具有多语言标签的选项字段

$rm->createField('demo_field', 'options', [
  'label' => 'Test Field',
  'label1020' => 'Test Feld',
  'type' => 'options',
  'optionsLang' => [
    'default' => [
      1 => 'VERYLOW|Very Low',
      2 => 'LOW|Low',
      3 => 'MIDDLE|Middle',
      4 => 'HIGH|High',
      5 => 'VERYHIGH|Very High',
    ],
    'de' => [
      1 => 'VERYLOW|Sehr niedrig',
      2 => 'LOW|Niedrig',
      3 => 'MIDDLE|Mittel',
      4 => 'HIGH|Hoch',
      5 => 'VERYHIGH|Sehr hoch',
    ],
  ],
]);

请注意,RockMigrations 使用的语法与通过 GUI 填充选项时的语法略有不同。RockMigrations 确保所有选项都使用默认语言的值,并且只设置选项的标签(标题)。

页面引用字段

$rm->migrate([
  'fields' => [
    'yourfield' => [
      'type' => 'page',
      'label' => __('Select a page'),
      'tags' => 'YourModule',
      'derefAsPage' => FieldtypePage::derefAsPageArray,
      'inputfield' => 'InputfieldSelect',
      'findPagesSelector' => 'foo=bar',
      'labelFieldName' => 'title',
    ],
  ],
]);

日期字段

$rm->migrate([
  'fields' => [
    'yourfield' => [
      'type' => 'datetime',
      'label' => __('Enter date'),
      'tags' => 'YourModule',
      'dateInputFormat' => 'j.n.y',
      'datepicker' => InputfieldDatetime::datepickerFocus,
      'defaultToday' => 1,
    ],
  ],
]);

重复矩阵字段

$rm->createRepeaterMatrixField('repeater_matrix_field_name', [
   'label' => 'Field Label',
   'tags' => 'your tags',
   'repeaterAddLabel' => 'Add New Block',
   'matrixItems' => [ // matrix types with their fields
       'type1' => [
           'label' => 'Type1',
           'fields' => [
               'title' => [
                   'label' => 'Custom Title',
                   'description' => 'Custom description',
                   'required' => 1,
               ],
           ]
       ],
       'type2' => [
           'label' => 'Type2',
           'fields' => [
               'text' => [
                  'label' => 'Custom Label',
               ],
               'checkbox',
           ]
       ],
   ]
]);

// remove a matrix type from a matrix field
$rm->removeMatrixItem('repeater_matrix_field_name', 'name_of_type');
// do not forget to also remove the type from the 'matrixItems' array above

星历史

Star History Chart