Concatenate strings in config #203

Closed
mnapoli opened this Issue Nov 12, 2014 · 4 comments

Comments

Projects
None yet
2 participants
@mnapoli
Member

mnapoli commented Nov 12, 2014

In config files we often need to concatenate strings, but by referencing other container entries (i.e. lazy concatenation).

For example:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\link('path.application') . '/tmp',
    'log.file' => DI\link('path.tmp') . '/app.log',
];

Of course this example doesn't work because DI\link(...) is not a string (it's a reference to a container entry). The only solution for now would be to use a closure which is overly verbose:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\factory(function (ContainerInterface $c) {
        return $c->get('path.application') . '/tmp';
    }),
    'log.file' => DI\factory(function (ContainerInterface $c) {
        return $c->get('path.tmp') . '/app.log';
    }),
];

Solutions

String concatenation definition

We could add a new string definition which would concatenate all parameters (lazily):

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\string(DI\link('path.application'), '/tmp'),
    'log.file' => DI\string(DI\link('path.tmp'), '/app.log'),
];

While it does look verbose, a PHP 5.6 example looks better:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => string(link('path.application'), '/tmp'),
    'log.file' => string(link('path.tmp'), '/app.log'),
];

String expression definition

We could have a string definition which allows to construct strings with expressions:

return [
    'path.application' => realpath(__DIR__ . '/..'),
    'path.tmp' => DI\string('{path.application}/tmp'),
    'log.file' => DI\string('{path.tmp}/app.log'),
];

Mix

We could also make DI\string() provide both behaviors. I don't like having a choice though, because it can be confusing, but maybe this can be an exception?

Other containers

  • Symfony:
log.file: "%path.tmp%/app.log"
  • Laravel: no support (except closures)
  • Aura: no support (except closures)
  • ZF2: no mention of string parameters in the Service Manager, maybe handled by another component?

@mnapoli mnapoli added the enhancement label Nov 12, 2014

@mnapoli mnapoli added this to the 5.0 milestone Nov 12, 2014

@diosmosis

This comment has been minimized.

Show comment
Hide comment
@diosmosis

diosmosis Nov 12, 2014

Is it possible to just do 'path.tmp' => '${path.application}/tmp', and have the container sort it out?

Is it possible to just do 'path.tmp' => '${path.application}/tmp', and have the container sort it out?

@mnapoli

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Nov 13, 2014

Member

@diosmosis Nope :( because then there is no difference between simple strings and those that need processing.

Member

mnapoli commented Nov 13, 2014

@diosmosis Nope :( because then there is no difference between simple strings and those that need processing.

@diosmosis

This comment has been minimized.

Show comment
Hide comment
@diosmosis

diosmosis Nov 17, 2014

Another idea: could combine the injectable string functionality w/ the factory function, eg, factory('${path.application}/tmp'), then you wouldn't have to define a new function at least. Of course, it's not really different from a new string function.

Another idea: could combine the injectable string functionality w/ the factory function, eg, factory('${path.application}/tmp'), then you wouldn't have to define a new function at least. Of course, it's not really different from a new string function.

@mnapoli mnapoli referenced this issue Dec 11, 2014

Merged

v5.0 #200

31 of 31 tasks complete

mnapoli added a commit that referenced this issue Jan 16, 2015

mnapoli added a commit that referenced this issue Jan 16, 2015

Merge pull request #224 from mnapoli/string-definitions
Implementation of #203: string definitions
@mnapoli

This comment has been minimized.

Show comment
Hide comment
@mnapoli

mnapoli Jan 16, 2015

Member

Implemented in #224

Yay!

Member

mnapoli commented Jan 16, 2015

Implemented in #224

Yay!

@mnapoli mnapoli closed this Jan 16, 2015

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