包括通过作曲家与莫扎特合作的碳场

时间:2021-04-18 作者:MikeiLL

科恩·雅各布斯Mozart 是一种防止Wordpress主题/插件中命名空间冲突的工具,该主题/插件利用Composer进行依赖关系管理。类似于PHP Scoper 但被描述为更简单,面向Wordpress。

我对莫扎特的配置如下:


  "extra": {
       "mozart": {
           "dep_namespace": "MZoo\\\\MyPlugin\\\\Dependencies\\\\",
           "dep_directory": "/src/Dependencies/",
           "classmap_directory": "/src/Classes/",
           "classmap_prefix": "MYPREFIX_",
           "packages": [
               "htmlburger/carbon-fields"
           ],
           "delete_vendor_directories": true
       }
   },
所有Carbon Fields文件都以命名空间为前缀,并迁移到src/Dependencies/Carbon_Fields 除了vendor/htmlburger/carbon-fields/config.php 文件,它位于core 包含所有插件类的目录。

我尝试在脚本标记中复制该文件:

"post-update-cmd": [
      "cp vendor/htmlburger/carbon-fields/config.php src/Dependencies/Carbon_Fields/",
然后将其添加到自动加载psr-4 作为file:

"files": ["src/Dependencies/Carbon_Fields/config.php"]
这让莫扎特疯狂:

Call to undefined function trailingslashit() in .../vendor/htmlburger/carbon-fields/core/Carbon_Fields.php:229
我认为解决方案可能是覆盖Carbon Fields的composer自动加载:

    "autoload": {
        "psr-4": {
            "Carbon_Fields\\\\": "core/"
        }
    },
通过莫扎特的override_autoload 配置选项,即;键入程序包名称的自动加载设置字典,以替换原始程序包中的设置\'composer.json autoload 属性"E;

这就是我大脑减压后要做的。

如果有人想插话,请提前谢谢。

更新似乎仍然无法正确执行。

"extra": {
      "mozart": {
          "dep_namespace": "MZoo\\\\MzMboAccess\\\\Dependencies\\\\",
          "dep_directory": "/src/Dependencies/",
          "classmap_directory": "/src/Classes/",
          "classmap_prefix": "MZMBOACCESS_",
          "packages": [
              "htmlburger/carbon-fields"
          ],
          "delete_vendor_directories": false,
          "override_autoload": {
              "htmlburger/carbon-fields": {
                "psr-4": {
                  "Carbon_Fields\\\\": "core/"
                },
                "files": ["./config.php"]
              }
          }
      }
  },
Theoverride_autoload 上面的配置似乎与config.php 文件

我认为我最终需要的是:

src/Dependencies/Carbon_Fields
|__config.php
|__core/
|____All of the CF class files.
我的结局是:

src/Dependencies/Carbon_Fields
|__All of the CF class files.

尝试引用中的整个目录override_autoload 像这样:

"Carbon_Fields\\\\": "./"
这样做的目的是复制所需的目录结构,但破坏了对psr-4自动加载标准的遵守。

1 个回复
SO网友:MikeiLL

因此,莫扎特还不支持字段自动加载,但有一个分支支持自动加载(可能会在某个时候合并到莫扎特大师中)

这是我用来管理碳场的composer文件(还有Eric Mann的Sessionz和WP Session Manager):

    "type": "wordpress-plugin",
    "scripts": {
        "lint": [
          "phpcs --extensions=php src *.php"
        ],
    "fix": ["phpcbf --extensions=php src *.php"],
    "post-install-cmd": [
        "\\"vendor/bin/mozart\\" compose"
    ],
    "post-update-cmd": [
      "\\"vendor/bin/mozart\\" compose"
    ]
    },
  "require": {
        "php": ">=7.1",
        "ericmann/sessionz": "^0.3",
        "ericmann/wp-session-manager": "*",
    "htmlburger/carbon-fields": "*"
  },
  "repositories": [
    {
      "url": "https://github.com/brianhenryie/mozart",
      "type": "git"
    },
    {
      "url": "https://github.com/ericmann/wp-session-manager",
      "type": "git"
    }
  ],
  "require-dev":{
    "coenjacobs/mozart": "dev-rewrite",
        "squizlabs/php_codesniffer": "*",
        "wp-coding-standards/wpcs": "*"
  },
    "autoload": {
        "psr-4": {
            "MZoo\\\\MzMboAccess\\\\": "src"
        }
    },
  "extra": {
    "mozart":{
      "dep_namespace": "MZoo\\\\MzMboAccess\\\\Dependencies\\\\",
      "dep_directory": "/src/Mozart/",
      "packages": [
        "htmlburger/carbon-fields",
        "ericmann/wp-session-manager",
        "ericmann/sessionz"
      ],
      "delete_vendor_directories": false,
      "override_autoload": {
        "htmlburger/carbon-fields": {
          "psr-4": {
            "Carbon_Fields\\\\": "core/"
          },
          "files": [
            "config.php",
            "templates",
            "assets",
            "build"
          ]
        }
      }
    }
  }
然后,与作曲家自动加载一起,我需要莫扎特特定的自动加载:

/**
 * Autoload Classes
 */
$wp_mbo_access_autoload = NS\\PLUGIN_NAME_DIR . \'/vendor/autoload.php\';
if ( file_exists( $wp_mbo_access_autoload ) ) {
    include_once $wp_mbo_access_autoload;
}

// Mozart-managed dependencies
$wp_mbo_access_mozart_autoload = NS\\PLUGIN_NAME_DIR . \'/src/Mozart/autoload.php\';
if ( file_exists( $wp_mbo_access_mozart_autoload ) ) {
    include_once $wp_mbo_access_mozart_autoload;
}

相关推荐

Composer无法将PHP Codesniffer下载到插件

尝试安装时出错squizlabs/php_codesniffer 通过composer打包到我的WordPress插件中。我将飞轮用于本地服务器,composer 1.8.6安装在/usr/local/bin/composer.phar我还尝试在本地插件文件夹中使用composer文件,但仍然出现相同的错误。这是我在插件目录中运行的命令composer require --dev squizlabs/php_codesniffer