Allow calling custom processors directly on EnvConfigurator

Add magic __call method to EnvConfigurator to allow calling custom
processors as methods instead of using the custom method.
This commit is contained in:
Jérôme Vasseur 2021-04-12 11:09:21 +02:00
parent 8451a9fbef
commit 1d008f76da
2 changed files with 13 additions and 0 deletions

View File

@ -31,6 +31,18 @@ class EnvConfigurator
return '%env('.implode(':', $this->stack).')%'; return '%env('.implode(':', $this->stack).')%';
} }
/**
* @return $this
*/
public function __call(string $name, array $arguments): self
{
$processor = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $name));
$this->custom($processor, ...$arguments);
return $this;
}
/** /**
* @return $this * @return $this
*/ */

View File

@ -31,5 +31,6 @@ final class EnvConfiguratorTest extends TestCase
yield ['%env(string:FOO)%', (new EnvConfigurator('FOO'))->string()]; yield ['%env(string:FOO)%', (new EnvConfigurator('FOO'))->string()];
yield ['%env(key:path:url:FOO)%', (new EnvConfigurator('FOO'))->url()->key('path')]; yield ['%env(key:path:url:FOO)%', (new EnvConfigurator('FOO'))->url()->key('path')];
yield ['%env(default:fallback:bar:arg1:FOO)%', (new EnvConfigurator('FOO'))->custom('bar', 'arg1')->default('fallback')]; yield ['%env(default:fallback:bar:arg1:FOO)%', (new EnvConfigurator('FOO'))->custom('bar', 'arg1')->default('fallback')];
yield ['%env(my_processor:my_argument:FOO)%', (new EnvConfigurator('FOO'))->myProcessor('my_argument')];
} }
} }