Drupal配置文件批量清除UUID和默认哈希

yunke 提交于 周五, 07/16/2021 - 10:07

如果你需要从现有站点制作一个Drupal发行版,那么为了避免发行版实例间潜在的冲突,往往需要将配置文件中的uuid和默认哈希(_core)删除,这里提供两种方式:

命令行方式删除:

find /path/to/PROFILE_NAME/config/install/ -type f -exec sed -i -e '/^uuid: /d' {} \;

find /path/to/PROFILE_NAME/config/install/ -type f -exec sed -i -e '/_core:/,+1d' {} \;

对于Mac OSX用户请使用以下命令:

find /path/to/PROFILE_NAME/config/install/ -type f -exec sed -i '' '/^uuid: /d' {} \;

find /path/to/PROFILE_NAME/config/install/ -type f -exec sed -i '' '/_core:/{N;d;}' {} \;

程序方式删除:

如果你不方便使用命令行,可使用以下程序(在drupal环境中执行):

<?php

/**
 *  by:yunke
 *  email:phpworld@qq.com
 */

namespace Drupal\yunke_help\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Serialization\Yaml;

/**
 * 自定义控制器
 */
class Test extends ControllerBase {

  protected $fileList = [];

  protected function doScan($path) {
    foreach (new \FilesystemIterator($path) as $fileInfo) {
      $path = realpath($fileInfo->getRealPath());
      $path = str_replace('\\', '/', $path);
      $subPath = rtrim($path, '/');
      if ($fileInfo->isDir()) {
        $this->doScan($subPath);
      }
      else {
        if ($fileInfo->getExtension() == 'yml') {
          $this->fileList[] = $subPath;
        }
      }
    }
  }

  public function delete() {
    $path = 'sync/';//导出的配置文件目录
    $this->doScan($path);
    foreach ($this->fileList as $configFile) {
      $data = Yaml::decode(file_get_contents($configFile));
      unset($data['uuid'], $data['_core']);
      file_put_contents($configFile, Yaml::encode($data));
    }
    echo "已处理完成:";
    print_r($this->fileList);
    die;
  }

}

注意事项:

1、如果你的发行版是从配置安装,那么不要清除system.site.yml中的uuid,但可以修改,此处的UUID有特殊含义,它代表实例id

2、制作发行版时,清除UUID或默认配置哈希不是必须的,有时候甚至不可以清除,视情况而定

 

添加新评论

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
请输入以上问题的答案,换一个问题请刷新,不区分大小写