Skip to content

Allow to use Plugin Options with the CONFIG placeholder #304

@llaville

Description

@llaville

I've finished my first Capt'n Plugin, and I've solved all my goals.

BTW, to be able to run it, we need a CaptainHook patched version, because I've added some new capabilities.

Third One -- this report

Caution

It's mandatory to run my plugin, but it's NOT a reading confort !

On same way to define a custom config my-custom-index: my-custom-value
https://php.captainhook.info/configure.html#settings

And {$CONFIG|value-of:custom>>my-custom-index} placeholder

I would like to be able to retrieve any Plugin Option if defined with such syntax

{$CONFIG|value-of:plugin>>PluginClassFQN.my-plugin-option-key}

For example:

{$CONFIG|value-of:plugin>>\\Bartlett\\CaptainHookBinPlugin\\BinPlugin.auto-colors-flag}

With

{
    "config": {
        "verbosity": "normal",
        "allow-failure": false,
        "ansi-colors": true,
        "git-directory": ".git",
        "fail-on-first-error": false,
        "bootstrap": "examples/vendor-bin-autoloader.php",
        "plugins": [
            {
                "plugin": "\\Bartlett\\CaptainHookBinPlugin\\BinPlugin",
                "options": {
                    "config-directory": "{$ENV|value-of:XDG_CONFIG_HOME|default:~/.config}",
                    "binary-directory": "{$ENV|value-of:VENDOR_BIN_DIR|default:vendor/bin}",
                    "ansi-colors-flag": "--ansi",
                    "auto-colors-flag": "--colors=auto",
                    "always-colors-flag": "--colors=always",
                    "never-colors-flag": "--colors=never"
                }
            }
        ],
        "includes": [
            "examples/pre-push.mago.json"
        ]
    }
}

My Patch suggestion

diff --git a/src/Runner/Action/Cli/Command/Placeholder/Config.php b/src/Runner/Action/Cli/Command/Placeholder/Config.php
index acb5f44..dce152b 100644
--- a/src/Runner/Action/Cli/Command/Placeholder/Config.php
+++ b/src/Runner/Action/Cli/Command/Placeholder/Config.php
@@ -58,6 +58,15 @@ class Config extends Foundation
             $custom = $this->config->getCustomSettings();
             return $custom[$key] ?? '';
         }
+        if (str_starts_with($value, 'plugin>>')) {
+            list ($pluginClass, $key) = explode('.', substr($value, 8));
+            foreach ($this->config->getPlugins() as $plugin) {
+                if ($plugin->getPlugin() === $pluginClass) {
+                    return $plugin->getOptions()->get($key);
+                }
+            };
+            return '';
+        }
         if (!isset($this->valueToMethod[$value])) {
             return '';
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions