Skip to content

Inject Configuration Object Into Functor #9

Description

@jimdelois

Currently, when mapping, the mapping functor cannot read other values from the container. It may be desirable to do so, however. Consider the following example:

EMAIL_FROM_ADDR=admin@example.com
EMAIL_FROM_NAME="Organization Administrator"

We may want our call to $config->get('EMAIL_FROM_ADD'); to return something like 'Organization Administrator <admin@example.com>'. This behavior is not currently possible.

By injecting the configuration object, itself, in to the MapperFactory, the factory can install it as a property in the Mapper, which can then be passed to the functor (defined as optional):

$config->map('EMAIL_FROM_ADD')
	->using(function($val, Configuration $config) {
		return sprintf('%s <%s>', $config->get('EMAIL_FROM_NAME'), $val);
	});

This behavior could further be improved by creating a new method create on the Configuration object which would behave nearly identically to the map method, but skipping the existence check (rather, inverting it). This would avoid the confusing "re-assignment" of the environment variable, above.

E.g.,

// Install a new config value in the Configuration object, generated from other values
$config->create('EMAIL_ADDRESS_RFC2822')->using( /* ... etc ... */ );

$config->get('EMAIL_ADDRESS_RFC2822'); // "Organization Administrator <admin@example.com>"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions