如何扩展关系 Config October CMS

How to extend relation Config October CMS

如何扩展控制器以添加配置关系字段。

现在我发现我可以像这样添加一个新文件

 myController::extend(function($controller){

$controller->relationConfig = '~/plugins/path/languages/config_relation.yaml';
        }); 

在这种情况下,该方法删除我的配置文件已经存在并添加一个新配置文件,因此它会触发错误,因为其他关系行为已经不存在。

最近对此进行了讨论和记录here

myController::extend(function($controller) {

    // Implement the relation controller if it doesn't exist already
    if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
        $controller->implement[] = 'Backend.Behaviors.RelationController';
    }

    // Implement the relationConfig property with our custom config if it doesn't exist already
    $myConfigPath = '~/plugins/path/languages/config_relation.yaml';
    if (!isset($controller->relationConfig)) {
        $controller->addDynamicProperty('relationConfig', $myConfigPath);
    }
    else {
        // Ensure that we have an instantiated config object to work with
        $config = $controller->makeConfig($controller->relationConfig);

        // Instantiate our custom config object to work with
        $myConfig = $controller->makeConfig($myConfigPath);

        // Merge the above two
        $controller->relationConfig = (object) array_merge((array) $config, (array) $myConfig);
    }
}

以下函数是 new,目前在 develop 分支中:

public function mergeConfig($configA, $configB)
{
    $configA = $this->makeConfig($configA);
    $configB = $this->makeConfig($configB);
    return (object) array_merge((array) $configA, (array) $configB);
}

所以以后 develop 分支合并到 master 后,您可以使用以下代码合并配置:

UsersController::extend(function($controller) {

    // Implement behavior if not already implemented
    if (!$controller->isClassExtendedWith('Backend.Behaviors.RelationController')) {
        $controller->implement[] = 'Backend.Behaviors.RelationController';
    }

    // Define property if not already defined
    if (!isset($controller->relationConfig)) {
        $controller->addDynamicProperty('relationConfig');
    }

    // Splice in configuration safely
    $myConfigPath = '$/myvendor/myplugin/controllers/users/config_relation.yaml';

    $controller->relationConfig = $controller->mergeConfig(
        $controller->relationConfig,
        $myConfigPath
    );

}