Skip to content

Commit 89a2ebc

Browse files
Allow access to plugin config for placeholders
Fixes issue: #304
1 parent 76fc984 commit 89a2ebc

2 files changed

Lines changed: 78 additions & 6 deletions

File tree

src/Runner/Action/Cli/Command/Placeholder/Config.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Config extends Foundation
2626
*
2727
* @var array<string, string>
2828
*/
29-
private $valueToMethod = [
29+
private array $valueToMethods = [
3030
'bootstrap' => 'getBootstrap',
3131
'git-directory' => 'getGitDirectory',
3232
'php-path' => 'getPhpPath',
@@ -53,16 +53,50 @@ public function replacement(array $options): string
5353
*/
5454
private function getConfigValueFor(string $value): string
5555
{
56+
// check if the value should be a custom config value
5657
if (str_starts_with($value, 'custom>>')) {
57-
$key = substr($value, 8);
58-
$custom = $this->config->getCustomSettings();
59-
return $custom[$key] ?? '';
58+
return $this->findCustomConfigValue($value);
6059
}
61-
if (!isset($this->valueToMethod[$value])) {
60+
// check if the value should be a plugin config value
61+
if (str_starts_with($value, 'plugin>>')) {
62+
return $this->findPluginConfigValue($value);
63+
}
64+
// if we don't know what method to call, return an empty string
65+
if (!isset($this->valueToMethods[$value])) {
6266
return '';
6367
}
6468

65-
$method = $this->valueToMethod[$value];
69+
$method = $this->valueToMethods[$value];
6670
return $this->config->$method();
6771
}
72+
73+
/**
74+
* Returns a custom configuration value
75+
*
76+
* @param string $value
77+
* @return string
78+
*/
79+
private function findCustomConfigValue(string $value): string
80+
{
81+
$key = substr($value, 8);
82+
$custom = $this->config->getCustomSettings();
83+
return $custom[$key] ?? '';
84+
}
85+
86+
/**
87+
* Returns a option value for a plugin configuration
88+
*
89+
* @param string $value
90+
* @return string
91+
*/
92+
private function findPluginConfigValue(string $value): string
93+
{
94+
[$pluginClass, $key] = explode('.', substr($value, 8));
95+
foreach ($this->config->getPlugins() as $plugin) {
96+
if ($plugin->getPlugin() === $pluginClass) {
97+
return $plugin->getOptions()->get($key);
98+
}
99+
}
100+
return '';
101+
}
68102
}

tests/unit/Runner/Action/Cli/Command/Placeholder/ConfigTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace CaptainHook\App\Runner\Action\Cli\Command\Placeholder;
1313

14+
use CaptainHook\App\Config\Plugin;
1415
use CaptainHook\App\Console\IO\Mockery as IOMockery;
1516
use CaptainHook\App\Config\Mockery as ConfigMockery;
1617
use CaptainHook\App\Mockery as AppMockery;
18+
use CaptainHook\App\Plugin\DummyPlugin;
1719
use PHPUnit\Framework\TestCase;
1820

1921
class ConfigTest extends TestCase
@@ -61,6 +63,42 @@ public function testCustomConfigValueNotFound(): void
6163
$this->assertEquals('', $replace);
6264
}
6365

66+
public function testPluginConfigValue(): void
67+
{
68+
$io = $this->createIOMock();
69+
$repo = $this->createRepositoryMock();
70+
$config = $this->createConfigMock();
71+
$config->expects($this->once())->method('getPlugins')->willReturn(
72+
[
73+
new Plugin(DummyPlugin::class, ['foo' => 'bar']),
74+
new Plugin(DummyPlugin::class, ['fiz' => 'baz']),
75+
]
76+
);
77+
78+
$placeholder = new Config($io, $config, $repo);
79+
$replace = $placeholder->replacement(['value-of' => 'plugin>>' . DummyPlugin::class . '.foo']);
80+
81+
$this->assertEquals('bar', $replace);
82+
}
83+
84+
public function testPluginConfigValueNotFound(): void
85+
{
86+
$io = $this->createIOMock();
87+
$repo = $this->createRepositoryMock();
88+
$config = $this->createConfigMock();
89+
$config->expects($this->once())->method('getPlugins')->willReturn(
90+
[
91+
new Plugin(DummyPlugin::class, ['foo' => 'bar']),
92+
new Plugin(DummyPlugin::class, ['fiz' => 'baz']),
93+
]
94+
);
95+
96+
$placeholder = new Config($io, $config, $repo);
97+
$replace = $placeholder->replacement(['value-of' => 'plugin>>fiz.baz']);
98+
99+
$this->assertEquals('', $replace);
100+
}
101+
64102
public function testNoValueOf(): void
65103
{
66104
$io = $this->createIOMock();

0 commit comments

Comments
 (0)